Need help with this array

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.

1
2
3
4
5
6
7
8
9
10
  #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]*2 <<endl;
system("Pause");
return 0;
} 


It's not doing what the question says and I don't know which other way.
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?
Hi Keskiverto. I think I need help with this. I don't know how to skip every other number.

The value of i on the first iteration should be 1
The second should be 2
The third, 3
The fourth, 2
The fifth should be 5.

Please help me as my time is limited :)
In your for statement you can use i+=2 so it would look like for (int i = 0; i < 5; i+=2)

Rember though you are asked to change every other number to 2 not multiply every other number by 2.

Your code should look something 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;
}
Last edited on
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.

int function:
1
2
3
4
5
int example(int x)
{
	x = x + 1;
	return x;
}


pass via reference
1
2
3
4
void example(int &x)
{
	x = x + 1;
}
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 ╠

Not sure what's going on. with this one.
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
#include <cstdlib>
using namespace std;
int main(){ 
int x[] ={1,2,3,4,5}; 
for(int i=0;i < 5; i++)
{
x[i]=2; // just give the value 2 to the whole array
}

for(int i=0;i < 5; i++)
{
cout<<x[i];
}
system("Pause");
return 0;
}
Topic archived. No new replies allowed.