Practice Help

Have an exam coming up and was doing some practice questions. I don't have an answer key so I'd like for someone to check my work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
 1.[10] Consider the following code fragment:

            string s1 = "Hi", s2 = "Hello", s3;
            s3 = (s1 < s2) ? s1 : s2;
	    cout << s3;

       Write out what output this code generates. Name the operation
       on the second line of the fragment and explain how it operates.
       Rewrite the second line using either branching or looping
       statements.


2.[10] Explain the differences between prefix and suffix form of the increment
       operator. Consider the following code fragment:

            int a=3, b=4, c;
	    c = (a++) - (++b);
	    cout << a << " " << b << " " << c;

       What output does it generate? Explain your answer.


3.[10] Explain the difference between while and do-while looping
       constructs. Rewrite the following code with either while
       or do-while.

       	       for(int i=0; i < 10; i++)
       	       	  cout << i << ' ';


4.[15] Describe how "break" and "continue" statements work with
       looping constructs. Consider the following code fragment:

            int i=0;
            while (i < 10){
	        i++;
	        if (i > 5) break;
	        cout << i << " ";
            }		
	    cout << i;

        What out would this code generate? How would the output change
	if "break" is replaced by "continue"?  Explain your answers.
	For both cases write out the exact output.


5.[10] Define the notion of variable scope. Consider the following code
       fragment:

            int a = 3;
	    if (a > 0) {
	       double a = 10.0;
	       cout << a << " ";
	    }
	    cout << a;

      Write out what output this code generates. Explain your answer.

6.[15] Name and explain parameter-passing disciplines we studied.
       Consider the following code fragment:

       	       int function1(int);
       	       int function2(int&);

	       int main(){
               	   int a=3, b=4;
	  	   int c = function1(a);
	  	   int d = function2(b);
	  	   cout << a << " " << b << " " << c << " " << d;
               }

       	       int function1(int aa){
                   aa++;
	  	   return aa;
               }

       	       int function2(int &bb){
                   bb++;
	  	   return bb;
       	       }


	Write out what output this code generates.  Explain your
	answer.


7.[15] Consider the following code fragment:

            const int SIZE=10;
            int myarray[SIZE];

	    // here goes the code that assigns the array
	    // values to each element of the array
	    // assume it already exists do not write it

	    int number;	    
	    cout << "Input the number to search for: ";
            cin >> number;

	    //
	    // here goes the code you need to write
	    //

	Complete this fragment such that the resultant code checks
	whether the input number is in the array "myarray" and outputs
	the result. Specifically, your code needs to produce one of
	the two outputs

        example one:

       	     Input the number to search for:  60
	     FOUND

        example two

      	     Input the number to search for: 33
	     NOT FOUND

         That is, depending on the input your code needs to output
	 either "FOUND" or "NOT FOUND". Write out the code. Explain
	 how it works.


8.[15] Describe what structures in C++ are and what their purpose
       is. Explain what structure variable and member variable are.
       Assuming the following structure definition.

               struct exampleStruct {
       	          int a;
	          string b;
               };

       consider the below code:

                exampleStruct x[10];

       Write code that assigns a value to every member variable of the
       fifth element of the array "x". The value is of your choosing.
       Explain the operation of your code.

1. The code outputs – Hi
The operation compares s1 and s2 in terms of s1 is less than s2. Because this is true s1 : s2 means that s3 becomes “Hi” because of the “?”, or conditional operator.
1
2
3
4
If (s1 < s2){
	S3 = s1;}
else
	s3 = s2;

2. Prefix is used to increment a value before the line is completed, suffix is used to increment a value after the line is completed.
Output: 4 5 -2
3. The difference between while and do-while is with do-while, the code is completed first and then it continues until the condition is not met. With while, the condition is first checked and then the code is implemented until the condition is not met.
1
2
3
4
5
6
7
int i= 0;
	do{
		cout << i << " ";
		i++;
	}
	while (i < 10);
}

4. Break is used to terminate a loop and continue with the rest of the program. Continue is used ignore the rest of the statements in a loop and reset to the condition
The code would generate 0 1 2 3 4 5 6 If break were replaced 0 1 2 3 4 5 10
The code would follow the loop until I reached 6 where it would break. The second code would follow the loop until I reached 6 and then would no longer cout << I until it reached 10 where it exited the loop and was printed one last time at 10.
5. Variable scope deals with global and local variables, global variables are defined outside of a function while a local variable is defined inside a variable.
The output of the code is 10 3 because a is a global variable initialized in the main function and a different one in the for loop, resulting in int a staying at the value of 3 and the double a staying at the value of 10.0.
6. Not sure how to explain parameter passing disciplines
The output: 3 5 4 5 this is because function1 does not use call-by-reference while function2 does. Function1 gets the value of c by adding 1 to a but not changing the value of a. Function2 gets d by adding 1 to b and also changing the value of b in the main function.
7.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(){
	const int SIZE = 10;
	int myarray[SIZE];

	for (int i = 0; i < SIZE; ++i){
		int a = i;
		myarray[i] = 10 * a;
	}
	int number;
	cout << "Input the number to search for: ";
	cin >> number;
	auto result = std::find(std::begin(myarray), std::end(myarray), number);
	if (result != std::end(myarray)) {
		cout << "FOUND" << endl;
	}
	else {
		cout << "NOT FOUND" << endl;
	}
}

I just decided to write the extra code that matches the output of the question. But the code I wrote for the second part uses std::find to search myarray for the number the user input. If it is found the program outputs “FOUND” if not “NOT FOUND”
8. Confused on this one.
An easy way to check your answers is to compile and run the code. I have checked some, but not all, of your answers.
YungScrublord wrote:
The operation compares s1 and s2 in terms of s1 is less than s2. Because this is true s1 : s2 means that s3 becomes “Hi” because of the “?”, or conditional operator.
In alphabetical order, i does not come before e. http://ideone.com/TrEXn0
YungScrublord wrote:
Prefix is used to increment a value before the line is completed, suffix is used to increment a value after the line is completed.
Not quite - postfix/suffix increments the value right then and there, but returns a copy of the old value. Prefix increments the value and returns by reference. It has nothing to do with lines of code or semicolons.
YungScrublord wrote:
The difference between while and do-while is with do-while, the code is completed first and then it continues until the condition is not met. With while, the condition is first checked and then the code is implemented until the condition is not met.
You could phrase it better. The only real difference is that do-while runs the code once before checking the condition, whereas plain while checks the condition first.
YungScrublord wrote:
The code would generate 0 1 2 3 4 5 6 If break were replaced 0 1 2 3 4 5 10
0 can never be printed.
YungScrublord wrote:
Variable scope deals with global and local variables, global variables are defined outside of a function while a local variable is defined inside a variable.
Scope rules are a lot more intricate than that. It's not a black and white situation - it's colorful.
YungScrublord wrote:
because a is a global variable initialized in the main function
Global variables are variables which are defined outside of any function. If it's defined inside a function, it's not possible for it to be global.
YungScrublord wrote:
Not sure how to explain parameter passing disciplines
In C++ there are only two ways to pass a parameter: by value (copy), or by reference. (In C++11 there is another but I don't expect you or your professor to know about it). Sometimes passing by pointer is considered to be a third method, but really it is passing the pointer by value (the first method). You can also pass pointers by reference.
YungScrublord wrote:
Confused on this one.
This is a bad question - in C++ there is no difference between the class and struct keywords, other than default privacy setting. The question makes more sense in C - try looking at it from that perspective, if you can.

EDIT: Fixed some mistakes with quote tags.
Last edited on
Ah Thank you so much LB, this has been very insightful, I'm glad you could help me with the definitions for each of these!!
Topic archived. No new replies allowed.