PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Need help with simple program...

Code:

#include <iostream.h>
#include <stdio.h>

int main()
{
int amount;
int number;
int This[21] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int total;

cout << "Please enter the amount of numbers you wish to average (20 max): ";
cin >> amount;

for(number=0; number = amount; ++number)
{
cout << "\nNumber: ";
scanf(" %d", This);
}


total = /*edited to fix forum length
it just said this[0]+[1] and the whole array*/
cout << "\nAverage is: " << total;

return 0;
}


I know this is pretty confusing, but here is what I am trying to do.

This program is supposed to average the amount of numbers you want. By the looks of it, I probably did it the hard way, and the wrong way. My program pauses after I enter the amount of numbers. Any ideas? :?:

By the way, I put scanf because it wouldn't let me use cin...
in scanf, replace This with &This;[number]. Done.
your total line could be much smaller too.

Code:

total=0;
for(int i=0;i<20;i++)
{
total+=This[i];
}
Code:
#include <iostream.h>
#include <stdio.h>

int main()
{
int amount;
int number;
int This[21];
int total;
int i;

cout << "Please enter the amount of numbers you wish to average (20 max): ";
cin >> amount;

for(number=0; number = 5; ++number)
{
cout << "\nNumber: ";
cin >> This; //fix this loop!!!
}


total=0;
for(i=0;i<20;i++)
{
total+=This[i];
total=total/amount;
}

cout << "\nAverage is: " << total;

return 0;
}


Here is a cleaned up version, notice my comment; it never stops asking for number, it doesn't stop when it is supposed to.
Line 15 should read:
for(number=0; number <= amount; ++number)

It is a bad idea to name a variable "This". Lowercase "this" is a reserved keyword in C++.

Dan East
I know. I typed a lowercase this and it turned blue. No idea why I made it "This".
Code:
#include <iostream.h>
#include <stdio.h>

int main()
{
int amount;
int number;
int This[21];
int total;
int i;

cout << "Please enter the amount of numbers you wish to average (20 max): ";
cin >> amount;

for(number=0; number <= amount; ++number)
{
cout << "\nNumber: ";
cin >> This[0,1,2,3,4,5]; //fix this loop!!!
}


total=0;
for(i=0;i<20;i++)
{
total+=This[i];
total=total / amount;
}

cout << "\nAverage is: " << total;

return 0;
}


We are getting somewhere, but now, it prints the answer as -39460769850983745098 or of something similar.
first of all, it should be

Code:

for(number=0; number < amount; ++number)

(note the < instead of the <=; putting <= would add one more element than necessary)

second of all,

Code:

cin >> This[number];


And lastly
Code:

for(i=0;i<amount;i++)

Otherwise unless you put 20 as the amount it adds uninitialized elements.

Wait, I lied, one more thing:
Code:

int This[20] = {0};

No need to have 21 elements when you specified that 20 is the max. It's also a good idea to initialize the array.

Oops, still one more:
Code:

scanf(" %d", &This;[number]);

Otherwise you're just putting everything into the first element of This.
You can't have i < amount, it has to be i = amount so it divides the proper amount of times for the average.

The problem is, the program pauses with i = amount, and it won't do this and that, blah blah...

Argh! C++ isn't very specific of a language. cout << "gj" could make it say -50973409857430957 if you do one thing wrong.
You can't have i < amount, it has to be i = amount so it divides the proper amount of times for the average.

No. remember that the second part of the for loop determines whether the loop continues or not. If the expression evaluates as true, it continues; if it evaluates as false.

i = 5 (let's say amount is 5 for the sake of discussion) is ALWAYS true sine all it does is set i to 5 and return 5, which is nonzero and thus true. Thus, the loop will never exit since i is always set to 5.

i == 5 isn't true for the first time (when i is 0) and thus the loop never executes and just quits.

i < 5 is true UNTIL 5 is reached; then the for loop exits after being executed 5 times. Thus, it's the only right thing to use here. (yes, you can also do i <= (amount - 1) )

C++ is very specific, that's why you just have to watch out. Or at least read a competent C++ book.

(by the way, edit your first post to take out that long initialization, or at least put a linebreak or two in it. It's screwing up this thread)

Oh, and I caught one more mistake:
Code:

total=total / amount;

Put that OUTSIDE the for loop; when averaging, you only divide by the number of terms once.
Code:

#include <iostream.h>
#include <stdio.h>

int main()
{
int amount;
int number;
float This[20] = {0};;
float total;
int i;

cout << "Please enter the amount of numbers you wish to average (20 max): ";
cin >> amount;

for(number=0; number < amount; ++number)
{
cout << "\nNumber: ";
cin >> This[number];

}


total=0;
for(i=0;i < amount;i++)
{
total+=This[i];
}

total=total / amount;


cout << "\nAverage is: " << total << "\n";

return 0;
}


And done! FINALLY! Feast your eyes on THIS! hehe
That code smells of a homework assignment.
It smells of more than that. :mrgreen:
Heh. Yes, it does.
Hah, actually it isn't, my high school doesn't even offer a C++ class as far as I know.

It was one of the exercises from my book "Practical C++ Programming".

If I don't use what I learn, I won't learn it.