int numbers[10];
int sum = 0;
double average = 0;
cout << "Enter 10 numbers one by one: " << endl;
for(int i = 0; i > 10; i++){
cin >> numbers[i];
}
int i = 0;
while (i < 10)
{
sum += numbers[i];
i++;
}
// Calculate the average of the 10 numbers.
average = sum / 10.0;
cout << "The average is " << average << endl;
i really don't know what is the wrong in my code? especially the to get ten numbers
i can do it by using while loop but i want to use for loop
A1 - Don't really see anything wrong with the code itself. Note though that 12345 is one number. You want to enter 1 2 3 4 5. On the other hand, your approach is a little wrong here. A number is even if it is divisible by 2 = the remainder of division by two equals 0. That is:if(number % 2 == 0) cout << "even"; else cout << "odd";. What you do here is silly.
A2 - First loop i from 0 to n, then you have to loop j from 0 to something that gradually decreases. I wonder if that could be i (that is 5-i)?
A3 - line 8 : i > 10. That condition is never met, so you get no input.
Do you consider the number 0 to be even or odd? Do you allow leading zeroes?
(eg, should the user input 0123456 generate the same answer as 123456?)
xorebxebx gave you a quite overblown implementation in C++ so that he could
provide a comparison with other languages (an unfair comparison anyway, since the
scala implementation uses a different algorithm); I would ignore that post, it won't
be useful to you anyway, as your instructor is probably not looking for you to do
string processing.
When you read in an integer, say 123456, you want to process one digit at a time.
If you divide the integer by 10 and take the remainder, you get the last digit of the
integer. If you then take that quotient, you are left with the rest of the digits. So
for example, 123456 % 10 = 6 (the last digit) and 123456 / 10 = 12345 (all of
the remaining digits). Now you can check the last digit for evenness, and repeat
the algorithm on the remaining digits (12345).
So obviously I'm suggesting a loop when I say "repeat the algorithm...". The only
trick here, which I'll let you think about, is to figure out when the loop should
end.
@empror9,
Yo could change cin >> i;//which takes all chars up to the first non digit and converts them to int to i = cin.get()-'0';//which takes one char and converts it to integer . You'd have to handle all non digits then. For example, check if i is between 0 and 9 before checking if it's odd or even. If it is not in this range, ignore it.
@xorebxebx,
I see you find pleasure in writing long c++ apps and comparing them with short scala apps. Well, there programs approach the problem differently, so you can't really compare them. A more equivalent c++ code would use count_if (I'm not implying that it would be any shorter though since c++ doesn't have lambda functions yet).
You're missing the point here though. He is not writing an app to count even digits. He is writing this app because he needs to learn a bunch of things about how loops, arrays and streams work and your code doesn't say much about that.
Who the hell told you they use a different algorithm?
The algorithm is exactly the same. Both use linear scan to count. Both count only even numbers. They are only expressed differently - one is idiomatic C++, the second one is idiomatic Scala. If you feel so bad about my "overblown" C++ implementation, please provide a better one. I doubt it will be any shorter or simpler.
So obviously I'm suggesting a loop when I say "repeat the algorithm...". The only
trick here, which I'll let you think about, is to figure out when the loop should
end.
Hmm.. above is a good chance to ask the student to produce an iteration and a recursive version to understand recursion sometimes can be very "beautiful" and concise :P
And yes, it is not any shorter. As for simplicity, I find a loop + comparison + incrementing much more obvious than a function that somebody else wrote that does most of the job for you + 'x & 1 == 0'. Also, if I were a professor, I wouldn't be glad to see that kind of answer. As I said, it's missing the point.
You know, this isn't really the right example that shows how much scala is better than c++. It is way too trivial for that. This is more about how scala has a bunch of functions in it's standard lib. To prove your point you should probably find a thread dealing with something more complex..
So you have shown you can use functional style in C++ for this trivial example.
I agree it is cleaner (and from my other posts you would also know I'm generally a proponent of the functional paradigm), but it is also a little longer. I tried to write the shortest C++ code, so that you cannot claim I deliberately used longer constructs to make C++ look more verbose.
Anyway, the functional style and C++ simply don't play well together. Functional style is not idiomatic C++, because C++ is not a functional language (having some limited support for functional programming doesn't make it a functional language). But, this example is far too simple to show that.
To prove your point you should probably find a thread dealing with something more complex..
I was not proving anything. I even haven't posted any comment. Just pure code. The readers can judge on their own. Finding a different thread will take some time, because most of the threads in this forum concern C++ language problems (like: why this does not compile or I get segfault or what library to use, etc.), not domain problems.