Why this code can't print on a straight line ? C++
what code?
#include <iostream> using namespace std; int num[15]; int u=0; int main() { for(int i=0; i<15; i++) { num[i]= i; if (num[i]%2 == 0) { cin >> num[i]; cout << num[i]; cout << " "; } else { cout << num[i]; cout << " "; } } return 0; }
I already answered this question.
I tried the new code but it doesn't seem to pring odd number sequence correctly , if you try it you will see
but the question said to only allow the array to except values in it's even positions.
your instructor will most likely set a break point on the cin line to see if it work correctly.
The exersise says to "ask the user to enter values only at even positions" which is like saying " odd positions should be a default sequence from 1 to 15 (picking only the odds) since the first part of the exersise is this a) Declare an array called “Num” with 15 integers.
Another example which I solved: c) Change all the items of the array with an ODD position to -1
But the even positions stay intact regardless
#include <iostream> using namespace std; int num[15]; int main() { for (int i=0; i < 15; i++) { //this line checks the index of the array //because i is used as the index if (i % 2 == 0) { cin >> num[i]; cout << num[i] << endl; } } return 0; } This works I have checked the code myself
for the odd part you would just check for numbers with remainders then assign -1 to the spots with the remainders num[i] = -1;
Also all code can be put on a straight line as long as the syntax is correct.
a compiler ignores blank space during the scanning process.
Although it would be highly irrational to put all code on one line.
If you put ``` above and below a code block, it does code high lighing and allows it to be copied properly. There are also code pasting services, like http://dpaste.com These two things can make it easier for people to help you.
Join our real-time social learning platform and learn together with your friends!