Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. True/ False: you can treat C++ strings as an array. 2. Given the following de

ID: 3680345 • Letter: 1

Question

1. True/ False: you can treat C++ strings as an array.

2. Given the following declaration, where is 77 stored in the Scores array?

int Scores[ ] = {83, 62, 77, 97};

a. Scores[ 0]    b. Scores[1 ]       c. Scores[2]      d. Scores[3]

3. which statement should be used to declare a 10 element integer array c?

a. Array c = int[10];    b. c = int[10];      c. int Array c[10];        d. int c[10];

4. Given an integer array defined by

int table [3][4] = {

{1, 3, 5, 7},

{2, 4, 6, 8},

{20, 30, 40, 50}

};

Write the function prototype for a function which is used to display the array. Note(you do not need to write the entire function)

Answer:

5. Define a character string "Cincinnati is a good city" using C-string and C++ string class(object), respectively

Answer:

6. Given the following structured data type:

struct watch{

string brand;

double price;

int yearMade;

};

Define an array, called watchList, with 20 elements and each element is a watch structure.

Answer:

7. Having two functions with the same name, but different parameter lists is called: (     )

8. True/ False : The main difference between C++ structures and C++ classes is that all variables in a structure are public and by default all variabes in a class are private.

9. An (        ) is automatically called when an object is created.

10. Which one of the statements is correct?

a. constructor functions can be given any name.       b. Normally constructors exist in the public class area.     

c. if three objects are declared from class T, only one copy of the memory needed for the member functions is reserved, and the objects share this memory for member functions.

d. Constructor functions can receive values.          e. All are correct.

11. what is the output of the following program?

# include <iostream>

using namesapce std;

class TestClass{

public:

TestClass (int x){

cout << x << endl;

}

TestClass (    ){

cout << “Hello!” << endl; }

};

int main ( ){

TestClass test (77);

return 0;

}

12. What is the output of this code segment?

class Foo{

   private:

   int x;

   int y;

   public:

   Foo ( ) { }

};

int main( ){

Foo Fum;

Fum.x =32;

Fum.y = 64;

cout << Fum.x << “ ” << Fum.y;

return 0;

}

a. 32   64        b. 64 32        c. will not compile          d. runtime error

13. What is the output of the following code fragment?

int twoDArray[3][5];  

for(int row=0; row<3; row++)  

for(int col = 0; col<5; col++)  

     twoDArray[row][col] =col;  

cout << twoDArray[1][4] << endl;  

cout << twoDArray[2][3] << endl;

Answer:  

14. What is the output of the following program?

void foo(int list[ ], int first, int last){   

int temp;

while (first < last){      

temp = list [first];  

list[first] = list[last];       

list[last] = temp;      

first++;    last--;

}

}     

int main{      

int array[10] = {0,1,2,3,4,5,6,7,8,9};    

foo(array, 0 , 9);       

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

cout << array[index] << ", ";     

return 0;

}  

Answer:   

15. What is the output of the following program execution?    

class Phrase{  

public:

   Phrase( ) { }

   Phrase(string in_string1, string, in_string2, string, in_string3, string, in_string4 ){  

string1 = in_string1;

   string2=in_string2;

   string3 = in_string3;

   string4 = in_string4;

}      

~Phrase( ){   

cout << string3 + "   " + string4 + " "string2 + ReverseString (string4) << endl;

}   

private:  

string ReverseString; // This method will reverse the order of a string .

string string1, string2, string3, string4;

};     

int main( ){   

          Phrase ThatsAllFolks( " ! kaerb gnirps ", " great ", " have ", " a" );   

}

16. What is the output of the following program if string_to_fix = "C++lsAGrandOldLanguage." ?

string CorrectSentence(string string_to_fix, int &number_of_fixes){     

    string corrected_string = " ";    

     number_of_fixes = 0;  

     bool first_cap_found = false;   

for(int i = 0; i <string_to_fix.length( ); i++){    

   if(isupper(string_to_fix[ i ] ) ) {      

       if (! first_cap_found ) {    

           first_cap_found = true;  

          corrected_string += string_to_fix [ i ];    

      }else{

             corrected_string += "    ";     

           corrected_string += tolower (string_to_fix [i ] );

           number_of_fixes += 2;      

        }       

       }else{    

        corrected_string += string_to_fix [ i ];   

   }  

  }  

    return corrected_string;  

}

17. Write a function that acceptes an integer source array, an integer destination array, an integer value, the source array size, the destination array size. The function should do the following; (a) The first five elements of the destination array should be set to the value of the integer value parameter listed above.

(b)Next, element 0 of the source array through the end should be copied into the destination array beginning at location 5.

18. write a short, complete program that does the following;

(a) It declares a class named sphere, with a private member variable named radius. Write set_radius( ) and get_radius( ) functions to access the radius variable, and a function called get_volume( ) that returns the volume of the sphere. Finally, write an overloaded operator - to compute the absolute volume difference between two spheres.

(b) It defines an array of 10 objects of sphere class.

(c) The main function uses a loop to ask the user to enter the radius value for each sphere object in the array. After all the values are entered, the prpgram displays the volume if each sphere object.

(d) Use the overload opperator - to compute the absolute volume difference between the first tow objects (out of the 10).

19. (a) Write a function called RemoveEveryOther which is passed a C++ string and it returns another C++ string with every other character removed (i.e. "thing " becomes "tig " and " bobwhite " becomes "bbht" ).

(b) write 5 unit tests to verify the proper operation of RemoveEveryOther above.

20. Correct at least 10 syntax errors in the following program.

   #include <iostream>

   using namespace std;  

   class point{   

                friend point operator- (const point&, const point&);    

       public:  

           point( );

           point (double, double);   

            point operator+ ( );   

            point operator= (const point);      

            void show_data( );     

        private:    

           double x, y;  

       }     

        int main( ){     

                point p1 (10, 3), p2 (4, 8);    

                p3 = p1 -p2;   

                ++p3;        

                show_data( );       

                return 0;  

           }       

       void point : : point( ) : x(0), y(0) { }     

       point point(double x_var, double y_var) : x(x_var) , y(y_var) { }    

        point point :operator++( ) {  

          x++;     

          y++;    r

         return * this ;  

     }       

        point point :: operator = (const point& pointb) {         

        x = pointb. x ;    

        y = pointbb. y;     

        return this;   

     }  

    void show_data ( ) {    

    cout << "x = " << x << "y = " << y << endl;   

     }  

        void operator- (const point & pointa, const point & pointb) {   

         point difference;     

         difference. x = pointa. x - pointb. x;   

         difference.y = pointa. y - pointb. y;    

         return difference;    

      }

21. Assume the main () program uses the following while loop to read a file myInputFile into an array (words) of strings using the string class, The switchCase( ) is used to switch all chars to upper or lower case. Identify at least 5 logical errors (including the while loop and the function.)

while (getline( myInputFile, words [lines] ) ) {      

      lines++;  

     cout << " Line " << lines << "is " << words[lines] <<endl;  

}    

    void switchCases (string words[ ], int lines) {    

          int cases;   

         ofstream myOutFile;  

         myOutFile. open( "switch-cases. txt " );   

       cout << " Do you want to make all upper case or lower case?" << endl;   

        cout << "1 : for upper and 2 : for lower " ;   

        cin >> cases;   

         for(int n = 0, n< lines; n++) {     

            for(int m = 0; m <= words [n].length( ); m++){   

                 if(cases == 2)    

                 words[n][m] = toupper (words [n][m] );

                 else     

                 words[n][m] = tolower (words [n][m] );  

           }    

          myOutFile << words [n];   

     }    

       myOutFile. close ( );  

}

Explanation / Answer

1. True: you can treat C++ strings as an array.

2. Given the following declaration, where is 77 stored in the Scores array?

int Scores[ ] = {83, 62, 77, 97};

c. Scores[2]

3. which statement should be used to declare a 10 element integer array c?

b. c = int[10];

4. Given an integer array defined by

int table [3][4] = {

{1, 3, 5, 7},

{2, 4, 6, 8},

{20, 30, 40, 50}

};

Write the function prototype for a function which is used to display the array. Note(you do not need to write the entire function)

Answer:

5. Define a character string "Cincinnati is a good city" using C-string and C++ string class(object), respectively

Answer:

6. Given the following structured data type:

struct watch{

string brand;

double price;

int yearMade;

};

Define an array, called watchList, with 20 elements and each element is a watch structure.

Answer: struct watch watchList[20];

7. Having two functions with the same name, but different parameter lists is called: ( function overloading )

8. True : The main difference between C++ structures and C++ classes is that all variables in a structure are public and by default all variabes in a class are private.

9. An ( constructor ) is automatically called when an object is created.

10. Which one of the statements is correct?

c. if three objects are declared from class T, only one copy of the memory needed for the member functions is reserved, and the objects share this memory for member functions.

11. what is the output of the following program?

# include <iostream>

using namesapce std;

class TestClass{

public:

TestClass (int x){

cout << x << endl;

}

TestClass (    ){

cout << “Hello!” << endl; }

};

int main ( ){

TestClass test (77);

return 0;

}

Output : 77

12. What is the output of this code segment?

class Foo{

   private:

   int x;

   int y;

   public:

   Foo ( ) { }

};

int main( ){

Foo Fum;

Fum.x =32;

Fum.y = 64;

cout << Fum.x << “ ” << Fum.y;

return 0;

}

d. runtime error

13. What is the output of the following code fragment?

int twoDArray[3][5];  

for(int row=0; row<3; row++)  

for(int col = 0; col<5; col++)  

     twoDArray[row][col] =col;  

cout << twoDArray[1][4] << endl;  

cout << twoDArray[2][3] << endl;

Answer: 4

3