Question on C++

Dec 20, 2016 at 1:07am
Given the following program fragment, what happens to variable two if the letter ‘a’ is entered from the keyboard:


1
2
3
4
  float one;
char two;
cin >> one; //at this point, the letter ‘a’ is entered
cin >> two; 


My answer is that nothing happens because the variable two has not been assigned any value. Correct?
Please help
Dec 20, 2016 at 1:20am
You could always compile it and see!
Dec 20, 2016 at 1:20am
Correct
Dec 20, 2016 at 1:23am
Thank you. How about this question-

Given the array int x [ ] = {1,2,3,4,5}, write a loop that replaces every other element with the value 2, starting with the first element.
Ans:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int x[] ={1,2,3,4,5};
for(int i=0;i < 5; i++) cout << x[i]/i*2 <<endl;
system("Pause");
return 0;
}
Please help.
Last edited on Dec 20, 2016 at 1:25am
Dec 20, 2016 at 1:29am
It would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	const int size = 5;
	int x[size] = { 1,2,3,4,5 };

	//make everyother value 2
	for (int i = 0; i < size; i += 2)
	{
		x[i] = 2;
	}

	//output array
	for (int i = 0; i < size; i++)
	{
		cout << x[i] << endl;
	}

	return 0;
}
Dec 20, 2016 at 4:04am
you're program will go to the infinite state
Topic archived. No new replies allowed.