d) Ask the user to enter values only at the items with an EVEN position.
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; }
ignore the int u=0 it's just an unused variable.
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?
What does even items mean? are you speaking of positions in the array?
try to comment out this line //num[i]= i + 1;
your for loop increments and your cin moves the user input into the array.
The problem is in the body of your if statement if (num[i]%2 == 0)
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
The way you had it you were checking the value inside the array rather then the index of the array.
really you don't need the cout statement at all according to the question. since the question is only asking for input.
Join our real-time social learning platform and learn together with your friends!