You are on page 1of 12

FINAL EXAM STRUCTURE

Output (15%) Debugging (15%)

Writing Simple Statements (20%)


Writing Simple Program x 2 (20%) Writing Program with Functions x 2 (30%)

120

Looking For Output.


int num = 1, sum = 0;

while ( num < 30 ) { sum += num; num += 4; //num= num + 4; } cout<<sum<<endl;

19

Looking For Output.


int beta =6; switch(beta) { case 1: case 3: beta = beta + 2; case 4: beta = 2 * beta; break; case 6: beta++; case 7: beta = 2*beta; case 8: beta = beta +5; break; default: beta--; } cout<<beta;

Function (2 Types)
return type
string getName(string N, string lN) { return fN + + lN; }

void type
void printName(string fN, string lN) { cout<< fN << << lN; }

Writing Simple Statements


Assume that all variables are declared. Write C++ statements to accomplish each of the following:
Assign the sum of the pre-decremented value of a and b to c.
c = --a + b;

Print out a message You are clever if mark is between 80 and 100 and mark is not 0 if( (mark>=80 && mark<=100) && mark!=0) { cout<<You are clever; }

Writing Simple Statements


Sum the even integers between 1 and 99 using a for statement and assign the result to sum.
int sum=0; for(int i=1;i<=99;i++) { if (i%2==0) sum+=i; // sum=sum+i } Set the value of the fifth element of the array alpha to the multiplication result of the first and third elements. alpha[4] = alpha[0] * alpha[2];

Writing Simple Statements


Compute the sum and average of all the five elements in the array alpha and output the sum, average and value of each element.
double average=0.0; int sum = 0; for (int i = 0; i<5;i++) { sum=sum+ alpha[i]; } average = sum/5;

cout<<Sum is<<sum<<endl;
cout<<Average is<<average<<endl; for (int i=0; i<5; i++) cout<<alpha[i];

Writing simple program


Write a C++ program that display table of conversion between feets and meters begins from 0 feet to 50 feet with every 5 feet increment using for loop. The conversion formula is: meter = 0.305 * foot cout<< setw(10)<<"Feet" << setw(10)<<"Meter"<<endl; for(int ft=0; ft<=50; ft+=5) cout<<setw(10)<<ft<<setw(10)<<0.305*ft<<endl;

Writing program with function


Write a program that prompts the user to enter two 3 x 3 matrices and displays their addition. Your program should contains function to add matrices a and b and save the result in c and to print the result of the addition as followed:

const int N = 3; /* The function for adding two matrices */ void addMatrix(const double a[][N], const double b[][N], double c[][N]) /* The function to print result of addition*/ void printResult(double m1[][N], double m2[][N], double m3[][N], char op)

Each element cij is aij + bij. Here is the sample run:

You might also like