average

Posts 16–24 of 24 · Page 2 of 2
Why06 is right on the money on this one;
One change you could make just for kicks (and less typing ) is:

result += value;

just looks a little neater and does the same thing
this is the code i got now.
is it possible to make the code simpler by removing or replacing some pieces plz comment.

thanks for all the help

#include <iostream>
using namespace std;

int main ()
{
int amount;
double result, value, tellen;
result = 0;

cout << "put in the amount of values you want the average of: ";
cin >> amount;
cout << endl;

for(tellen = 1; tellen <= amount; tellen++)
{
cout << "Please enter number " << tellen << "/" << amount << ":";
cin >> value;
cout <<endl;
result+= value ;

}

cout << "The average is " << result/amount <<"." <<endl;

system ("pause");

return 0;
}
Ok comments:

I like your descriptive naming of the variables, that's very good.
A lot of people tend to be lazy and just call stuff a,b,c,d etc.. which
makes following the code much harder than it needs to be. The only place that's usually accepted practice is in a for loop as in:
for(int i = 0; i < some_value; i++) especially when working with arrays in that loop e.g. myArray[i] = value;

I always like variable names in english since the rest of the syntax is english-ish Tellen (dutch? Edit: doh just noticed the little flag under your name haha) I 'd probably rename to count or something like that. Also since it's a whole number, no need for it to be a double, an int16 or int32 would be just fine.

Now I think you've got the basics down, try this to make it harder
(yet simpler for the user):

Instead of having the user specify how many numbers they are going to enter and having them all entered one by one, have them enter them all seperate by spaces (and/or comma's if you wanted to be flexible) e.g:
it will say "Enter numbers:" and then you can type: 45 53 55 3 100
but it should also accept: 45,34,13,36,16,35,3,56 (with comma separated)
AND 46, 446, 460, 46 (command and space)
OR any combination of this like: 46 43,246 23, 59 (mixed)
You can do the spaces one first if you want, but once you have that, it's no work at all
really to do the others


Feel free to post your thoughts on how to approach the problem first so we can post some hints etc to steer you in the right direction.
lol thanks for the comment

but i don't understand what u mean about the for loop.

tellen = count in dutch (I'll change it later)

i don't know yet how to solve the problem.

i think of it tomorrow now it is getting late here so cant think.

the problems that i need to solve are:

1. counting the values
2. adding them together (with a loop i think)
3. see if it is a problem when there is a , behind a value.

dont give hints yet i need to figure it out for myself first
Lol; Basically I was saying that short (useless) variable names are not good, but accepted when using for loops. If for instance you use 'i' in combination with an array in a for loops (which you probably haven't gotten to yet) then 'i' basically stands for 'index'.. Don't worry about it for now.. it'll all become clear in time

Pretty good assessment of tackling the problem. First step is indeed figuring out how to take the input the user gave and get the values out
Yeh I was actually wondering too what tellen meant... dutch that's interesting forgot you were dutch.

Anyway B1ackAnge1, I think you might have thrown to much own him at once lol. I don't think lalakiljip is familiar with character arrays or strings yet :l If he solves this I'll be really impressed xD. I think this is good enough for now once he reads through his book a little more maybe we can worry about user interfaces and robustness.
yeah sorta figured, arrays, strings, etc were beyond the realm right now but at least that's a direction to explore.. nothing like dropping people in the deep-end
it is indeed kind of difficult. and i don't know what strings and arrays are yet.

i can see the problems i can see the solutions but don't know how to solve them with codes yet.

but that is just a matter of continuing with the tutorial.
Just keep moving forward and once you learn about those see if you can apply that to change the program as described Keep us posted of your progress!
Posts 16–24 of 24 · Page 2 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us