CISC106-010, Fall 2007              12/7/2007

Answers for Practice Questions dated 12/7/2007

 

Contents

C++ arrays

Various topics in C++

A1.

A2.

A3.

A4.

A5.

C++ vs. MATLAB

A1.

A2.

A3.

A3.

A4.

 

 

C++ arrays

 

Look at the practice questions at http://www.udel.edu/CIS/106/iaydin/07F/practice/practice_2007_12_3.txt

The answers are in the link above also.

 

Various topics in C++

 

A1.

#include <iostream.h>

using namespace std;

 

int main(){

     // error1: declare the integer i. Remember, C++ you

     // have to declare a variable or function prototype

     // before using it.

     int i;

     i = 55;

 

     // error2: you need to use << operator with cout

     // error3: you need to end your executable statements

     // in C++ with a semi-colon

     cout << endl << "my number is: " << i << endl;

  

     return 0;

}

 

A2.

//

// isSeven() function returns true if the number i is 7

// inputs:

//   i: the number to test

// outputs:

//   true if i is 7 else return false.

//

bool isSeven(int i)

{

     bool result;

 

     result = false;

  

     // run-time error: you need to use == sign to

     // test equality!

     if (i == 7)

           result = true;

 

     // compile-time error: Since the return type of this

     // function is not void,but a bool, you need to

     // return a boolean before exiting the function.

     // In this case, you need to return result.

     return result; 

     

}

 

A3.

 

#include <iostream.h>

using namespace std;

 

// declaring the function prototype

bool isEven(int i);

 

int main(){

 

   int i;

 

   cout << “Please enter a number: ” ;

   cin >> i;   

 

 

   if ( isEven(i) )

   {

      cout << endl << "number " << i << " is an even number" << endl ;

    }

   else

   {

      cout << endl << "number " << i << " is an odd number" << endl ;

   }

 

   return 0;

}

 

A4.

 

bool isOdd(int i)

{

     bool result;

 

     if ( isEven(i) )

     {

           result = false;

     }

     else

     {

           result = true;      

     }

 

     return result;

 

}

 

A5.

 

for (int k = 0 ;  k <10 ; k++)

{

      cout << k << endl;

}

 

C++ vs. MATLAB

 

A1.

// addElements() function: returns the sum of the integers

// in integer array x

// inputs:

//   x: the integer array

//   len: the  number of elements in the array

// outputs:

//   sum of the integers in integer array x

//

int addElements(int x[], int len)

{

    

     // important note: make sure you initialize sum to zero,

     // so that if len is 0, then your function returns

     // the right sum (which is zero).

 

     int sum = 0;

 

     // important note: make sure j starts from zero and

     // ends at len-1, so that you can add up all the elements

     // of array x. Remember, (1) C++ array indices start from

     // zero (2) the last element of the array x is x[len-1].

 

         

     for (int j = 0; j < len; j++)

     {

           sum = sum + x[j];

     }

 

     // important note: once you calculate  the sum,

     // you have to explicitly return it in

     // the return statement.

 

     return sum;

 

 

}

 

A2.

>> clear

>> v = [15 0 -5 10]

>> mysum(v)               

 

A3.

 

int main(){

     const int SIZE = 4;

     int A[SIZE] = {5, 60, -10}

     int sum;

    

     // write the function call below

     sum =  addElements(A, len);    

 

     cout << endl << "Sum of the elements of array A is: " << sum << endl;

     return 0;

}

 

A3.

The screen output for the MATLAB code piece is:

 

oooox

 

A C++ while loop doing the same can be

// declare your loop variable

int  i = 1;

 

// count 4 times in the loop

while (i <= 4)

{

     // remember C++ strings are written using double-quotes

     cout << “o”;

}

//print x to the screen and endl to print a line-break

cout << “x” << endl;

 

A4.

The screen output for the MATLAB code piece is:

 

o

o

o

o

o

x

 

A C++ for loop doing the same can be

 

// declare i in the for loop

// use i to count 5 times, with the for loop

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

{

     // use double-quotes for printing a string to the

     // screen and endl to print a line-break

     cout << “o” << endl;

}

cout << “x” << endl;