Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (christos):

d) Ask the user to enter values only at the items with an EVEN position.

OpenStudy (christos):

Where is my mistake? #include <iostream> using namespace std; int num[15]; int u=0; int main() { for(int i=0; i<15; i++) { num[i]= i + 1; if (num[i]%2 == 0) { cin >> num[i]; cout << num[i]; cout << " "; } else { cout << num[i]; cout << " "; } } return 0; }

OpenStudy (christos):

ignore the int u=0 it's just an unused variable.

OpenStudy (anonymous):

What exactly are you trying to do here? you are using the mod operator to decide if there is a remainder after dividing by 2 So you are looking for only numbers that can be divided without a remainder?

OpenStudy (anonymous):

What does even items mean? are you speaking of positions in the array?

OpenStudy (anonymous):

try to comment out this line //num[i]= i + 1;

OpenStudy (anonymous):

your for loop increments and your cin moves the user input into the array.

OpenStudy (anonymous):

The problem is in the body of your if statement if (num[i]%2 == 0)

OpenStudy (anonymous):

here is the correct code #include <iostream> using namespace std; int num[15]; int main() { for (int i=0; i < 15; i++) { if (i % 2 == 0) { cin >> num[i]; cout << num[i] << endl; } } return 0; } put a break point on the cout line to check the results

OpenStudy (anonymous):

The way you had it you were checking the value inside the array rather then the index of the array.

OpenStudy (anonymous):

really you don't need the cout statement at all according to the question. since the question is only asking for input.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!