Example 2. midterm

advertisement
SURNAME, NAME:
FIRST ED. /
SECOND ED.
EEE145 PROGRAMMING IN C
SECOND MIDTERM EXAM
December 4th,2014
Duration: 90 minutes
Instructions: You have to show all your work on the exam paper, otherwise you don’t get any
credit.
1.(14 points) What is the output of the following C++ program?
#include <iostream>
using namespace std;
void rec(char str[], int n)
{
if(n < 1)
return;
cout<<str[n-1];
rec(str, n - 1);
}
int main()
{
char str[] = "0123456789";
rec(str, 5);
return 0;
}
2.(15 points) What is the output of the following C++ program?
#include <iostream>
using namespace std;
#define M 5
int main() {
int A[M][M]={{0,1,2,3,4},{5,6,7,8,9},{10,11,12,13,14},
{15,16,17,18,19},{20,21,22,23,24}};
int i,j,k,t,rc=2;
for (i=0;i<M;i++)
{
t=A[rc][i]; A[rc][i]=A[i][rc]; A[i][rc]=t;
}
for (i=0;i<M;i++){
for (j=0;j<M;j++)
cout<<A[i][j]<< "\t";
cout<<"\n";}
}
3. (14 points) What is the output of the following C++ program?
#include <iostream>
using namespace std;
int x = 10;
int y = 20;
int z = 30;
void swap(int *x, int *y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int acc(int x)
{
int sum = 0;
sum += x;
z = sum;
return sum;
}
int main()
{
double z = 1.0;
swap(&x, &y);
x = acc(y);
cout<< x <<" "<<z;
return 0;
}
4.(12 points) What is the output of the following C++ program?
#include <iostream>
using namespace std;
int main( ) {
char s[ ]="The name of the rose";
char *p, *r;
p=r=s;
*(p + 4) = 'F';
p += 9;
*p = 'i';
cout<< s <<endl ;
r += 12; (*r) = 'T';
*(r+4)='R';
cout<< r << endl;
cout<< (r - p)<<endl;
return 0;
}
5.(15 points) What is the output of the following C++ program?
#include <iostream>
#include <vector>
using namespace std;
void PrintVector(vector<int> &qrVector) {
unsigned int i;
for (i=0;i<qrVector.size();i++) {
cout << qrVector [i] << " ";
}
cout<<endl;
}
int main(){
vector<int> qVector(3,-2);
qVector.push_back(45);
qVector.push_back(34);
qVector.push_back(2);
PrintVector(qVector);
int q= 0;
++q;
qVector.insert( qVector.begin()+q, 10);
q++;
qVector.erase(qVector.begin()+q, qVector.end());
PrintVector(qVector);
}
6.(15 points) Write a C++ function that removes an item from an array. The function searches the
first n elements of the array a for the item x. If x is found, its first occurrence is removed, all the
elements above that position are shifted down, n is decremented, and true is returned to indicate a
successful removal. If x is not found, the array is left unchanged and false is returned.
bool removeFirst(float a[],int& n,float x){
7.(15 points) Write a C++ function to compute and return the secondary diagonal sum for any 2dimensional square integer matrix of size NxN. For instance; if the following 3x3 array is given
π‘Ž 𝑏 𝑐
𝐴 = [𝑑 𝑒 𝑓 ]
secondary_diagonal_sum= c+e+g
𝑔 β„Ž 𝑖
int sum(int A[][N],int N){
Download