Here's the 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. Please help.
Your loop does start by looking at the first element. It does not replace a value, but prints something. That something happens to be 1*2, which is 2 and 2 is the value you were kind of looking for.
Then the loop looks at the second element. Why? You are sopposed to look only "every other element". Skip the second.
The third iteration looks at third element. Nice, but this was supposed to happen on the second iteration.
What is the value of 'i' on the first iteration?
What should the value of 'i' be on the second iteration?
What should the value of 'i' be on the third iteration?
Do you notice a pattern?
You are supposed to replace value(s) of array element(s). Not to show anything. How do you set a value to specific element of an array?
You're genius. That worked. Yayyyy!! Thank you Joe. Now here's another question that I'm battling with.
Given the function:
1 2 3
void example (int x)
{ x = x+1;
}
explain why the following would result in an error:
cout << example(7);
NOTE: assume the call is part of a program where everything else is correct. In other words, the error is confined to the code shown here.
OK so cout << example(7); calls the function and passes the value 7 to the integer x in the function. In the function you then add 1 to x so your would have x= 8 but then the function ends and the cout statment has nothing to out put. Two solutions to this would to either change the function from a void to an int and return x or pass x via reference.
Thank you very much. Here's my last problem that I'm struggling with.
11. Given the following program fragment, what happens to variable two if the letter ‘a’ is entered from the keyboard:
float one;
char two;
cin >> one; //at this point, the letter ‘a’ is entered
cin >> two;
Ans:
If the letter a is entered from the keyboard nothing happens to the variable unless other wise stated as cin >> one >> ch >> a then it would have changed.
Correct?
I'm not exactly sure what this question is trying to teach you but I tested it and if you enter a for the first cin statement it skips the second and one equals -1.07374e+08 and two equals ╠
std::cin is expecting a float datatype for variable one but when it receives a char it's bad input flag is set due to the input type being mismatched. So you need to (a) clear that flag and (b) discard the bad input from the input buffer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <limits>
int main()
{
float one;
char two;
//cin >> one; //at this point, the letter ‘a’ is entered
while (std::cout << "Enter a number" && !(std::cin >> one)) {
std::cin.clear(); //clear bad input flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input
std::cout << "Invalid input; please re-enter.\n";
}
cin >> two;
}
ps: there have been more detailed discussions on this forum about these matters, have a search