Faculty of Engineering and Computer Science

Concordia University

October 12th, 2006

IMPORTANT NOTE: marks will be awarded for correctness of your algorithm, C++ syntax, code efficiency, and the clarity of your program.

I-

a)-Describe the exact output that is produced by the following program segment. Use an underscore (_) to indicate blank spaces. (10 points)

int k, max = 10;
for(k=1; k <= max; k++)
{
if( k == 3 )
continue;

if( k == 6 )

break;

cout < "k = " < k < endl;
}
cout < "All done!" < endl;

Solution:

k = _1

k = _2

k = _4

k = _5

All_done!

b)-Execute the following function for numbers between 1 and 10 (show all the steps); conclude what this function does. (15 points)

1- void function (int nb)

2- {

3- bool check = true;

4- int half = nb/2;

5- if (nb == 1||nb == 2)

6- cout<nb<endl;

7- else{

8- for(int i=2; i<=half; i++){

9- if (nb%i == 0){

10- check = false;

11- break;

12- }

13- }

14- if (check == true)

15- cout<nb<endl;

16- }

17- }

Solution:

For nb = 1:

check = true

the condition for the first “if” (line 5) is true, and the program will cout the number.

Output:

1

The same for nb = 2

Output:

2

nb = 3

check = true

half = nb/2 = 1

the condition of the if (line 5) is false, the “else” (line 7) is executed:

the “for” (line 8) will not execute since “i” is not less than half, then line 14 will execute. the value of “check” has not changed and line 15 will execute:

Output

3

nb = 4

check = true

half = nb/2 = 2

the condition of the if (line 5) is false, the “else” (line 7) is executed:

the condition for the for is true (i.e., i=2 <= half), then line 9 will execute; 4%2 == 0 is also true, line 10 executes (check = false) and the program will break the for loop and execute line 14. Here, the condition is false, and nothing will be output.

nb = 5

check = true

half = nb/2 = 2

the condition of the if (line 5) is false, the “else” (line 7) is executed:

the condition for the for is true (i.e., i=2 <= half), then line 9 will execute; 5%2 ==0 is false and “i” is incremented (i=3). The condition of the “for“ is false, the loop terminates and line 14 executes. The condition is true (since check = true), and line 15 executes.

Output

5

The same applies for other numbers.

nb = 6

no output

nb = 7

7

nb = 8

no output

nb = 9

no output

nb = 10

no output

The program prints the prime numbers between 1 and 10.

c)- What will be the output from the following C++ code? Be careful to show the output exactly as it would be printed: (10 points)

1- int j = 5;

2- int k = ++j;

3- int m = k++;

4- int n = (k == j + 1);

5- if (j = n) cout < "yes";

6- else cout < "no";

7- cout < " " < j < " " < k < " " < m < " " < n;

solution

1- j = 5

2- j = 6 and k = 6

3- m = 6 and k = 7

4- k is equal to j+1, then the condition is true and the results of the test is assigned to n: i.e., n = true or 1;

5- n is assigned to j à j=1 and the system ouput “yes”

6- does not execute

7- final result:

yes_1_7_6_1

II-

a)- “swap” is a function that takes two variables, and swap their values. Write the function prototype of “swap”, and the function; the function should print on the screen the values before and after the swap. (10 points)

solution

void swap (int, int); // function prototype

void swap (int a, int b)

{

int temp; // a temporary variable

cout < “the values of a and b before the swap are:”

<a<”, ”<b<endl;

temp = a;

a = b;

b = temp;

cout < “the values of a and b after the swap are:”

<a<”, ”<b<endl;

}

b)- Write a function and its prototype that evaluates the following series:

The function should take an integer x as input and return f(x). (15 points)

Note: you could use the power (a,b) function from the <cmath> library.

solution

#include <cmath>

#include <iostream>

#include <cmath>

using std::cout;

using std::cin;

using std::endl;

float factorial (unsigned ); // prototype

float fOfX(int ); // prototype

int LIMIT = 100;

int main()

{

int x;

float result;

cout<"Please enter an integer number: "<endl;

cin >x;

result = fOfX(x);

cout<"The sum is equal to: "<result<endl;

return 0;

}

float fOfX(int x)

{

float sum = 0;

float fract;

float fact;

for (int i = 0; i<=LIMIT; i++)

{

fact = factorial(i);

fract = pow(x,i)/fact;

sum = sum + fract;

}

return sum;

}

float factorial(unsigned nb)

{

float fact = 1;

if ((nb == 1)||(nb == 0))

return 1;

else

{

for (int i = 1; i<= nb; i++)

fact = fact*i;

return fact;

}

}

4

Mech215--Fall 2006 Midterm 1