|
-
March 30th, 1999, 08:26 AM
#1
Whats wrong with this code????
This code is supposed to ask the user
How many numbers would you like to enter?
And then give the smallest, largest , average, and the difference
between each number and the average. But it wont work. And i cant find
why. I get no error but it still wont work.
Could you please help me fix this thing as I have no more hair left
thinking about it. I am new to programming so I need the most basic
code to fix it....
I thank you in advance..
Ercan
#include <iostream.h>
#include <conio.h>
main()
{
clrscr();
int maxentries,maxnumber,minnumber,number,totalaverage;
float average,sum,difference;
cout<<"How many Numbers would you like to Enter?\t";
cin>>maxentries;
for (int count = 0; count < maxentries; count++)
{
cout<<"\n\nEnter a number Please:\t";
cin>>"number";
sum += number;
average = ( sum / count );
cout<<"\nAverage of these numbers is\t"<<average;
difference = ( number - average );
cout<<"\nDifference between this number and the average is"<<difference;
if ( number < minnumber )
minnumber = number;
if ( number > maxnumber )
maxnumber = number;
}
cout<<"\nSmallest number is\t"<<minnumber;
cout<<"\nLargest number is\t"<<maxnumber;
totalaverage = sum / maxentries;
cout<<"\nTotal average is\t"<<totalaverage;
getch();
return 0;
}
-
March 30th, 1999, 08:37 AM
#2
Re: Whats wrong with this code????
Hello,Ercan.I have read your source code . You should initialize these variable such as sum ,maxentries,maxnumber,minnumber,number,totalaverage.Good Luck!
LiuDengKe 99.3.30
-
March 30th, 1999, 08:40 AM
#3
Re: Whats wrong with this code????
But they are initialised.
????????????????????????????????????????
-
March 30th, 1999, 08:43 AM
#4
Re: Whats wrong with this code????
Hey! Weren't you the guy who asked something similar yesterday?
1) change cin>>"number"; to cin>>number;
2) You need to initialize the minimum and maximum before testing.
Check for the first time throught the loop (ie. count == 0) and initialize them then.
if (count == 0)
{
minnumber = maxnumber = number;
}
else
{
if ( number < minnumber )
minnumber = number;
if ( number > maxnumber )
maxnumber = number;
}
There may be more, but try these suggestions first
-
March 30th, 1999, 08:45 AM
#5
Re: Whats wrong with this code????
He's right, you do need to initialize all of your variables. In C and C++, automatic variables are not initialized, so they contain garbage.
-
March 30th, 1999, 08:46 AM
#6
Re: Whats wrong with this code????
They are not initialised.
The int and float variables you use must be initialised to zero.
If you don't believe that are not initialised then try to debug your code
and watch the variables.
-
March 30th, 1999, 08:48 AM
#7
Re: Whats wrong with this code????
Yes it probably was me who asked similar question yesterday but this
code is giving me the s__ts.. heheheh but i am enjoying the challenge.
I am really new at this just 5 weeks into programming so please
accept my appologies if i am annoying....
I will get better as I learn more..
Thank You for responding
Ercan
-
March 30th, 1999, 08:49 AM
#8
Re: Whats wrong with this code????
The biggest problem I see is
cin >> "number";
At best, this is going to read user input into a six-byte text string, and not into the int called 'number.' At worst
it would cause your app to crash. You should get rid of the quotes:
cin >> number;
Next, you need to initialize variables before you use them. For example, in the for() loop:
sum += number;
'sum' didn't have a legitimate value to start with, so adding 'number' to it is not going to be meaningful for you.
Before the for() loop, add a line:
sum = 0;
And check your other variables to see whether you're initializing them before you use them.
Next, when computing your average inside the for() loop, you use:
average = (sum / count);
This will get you in trouble, because your for() loop starts with count = 0. And even the new Pentiums can't divide by
zero. I recommend that you change your for() loop:
for(count = 1; count <= maxentries; count++)
In general, start a for() with zero when you're accessing an array or something that's really based on zero.
Last, take a look at your minnumber processing. If you init minnumber to zero, then your app will claim that the
smallest number entered is zero, even if the user enters all positive numbers. So you'll have to create a mechanism
that tells you whether you've stored a user-entered value into minnumber before you check the value of number against
minnumber. Same thing for maxnumber. Good luck.
-
March 30th, 1999, 08:57 AM
#9
Re: Thank You Thank You
I had heaps of responses and i thank everyone of you. The more responses i
get the more silly i feel. heheheheh the answers are quite obvious when you
are told where the problems are hehehe. Thank you again to everyone who
helped me.
You guys are great for helping us poor newbies.
But oneday even us newbies will become pros. hehehehe
Thank You again
Ercan
-
March 31st, 1999, 05:09 AM
#10
Re: Whats wrong with this code????
Ercan,
yesterday I gave you code that worked, today you post almost the same code, but seriously broken! What happened?
Dave
-
March 31st, 1999, 06:10 AM
#11
Re: Whats wrong with this code????
Hi DAVE,
The problem with the code was that in one area you put
int minNumber = INT_MAX;
int maxNumber = INT_MIN;
I dont know what these are as i have seen it before and also my
compiler gives me an error message in the same spot. My compiler
is called Tclite. Not very good but i guess good for a beginner.
I didnt know hwat to do with that part of the code...
So I tried to use some of your code and go another way but i
guess i got lost and confused on the way. hehehehe.
So what is the INT_MAX and INT_MIN??
What do i do to fix it in the compiler so i can run it.
Or is there another way of solving this problem???
As you might have guessed by now I am absolutely new at this stuff.
.O/
/| Ercan
-^-
Thank You for writting back to me Dave....
-
March 31st, 1999, 06:34 AM
#12
Re: Whats wrong with this code????
heheheheh I FOUND ITTTTTTTTTTTT.
Limits.h
that is what was missing in the code you gave me. I didnt even know it
existed.
Would you be kind enough to explain exactly what it does and how it works.
I have never used it or seen it before.
But the code works perfectly now hehehe.
Now it is time to study the code to fully understand how it works for
future refference.
Thank You very very much. This was a great help to me...
O O
\_/ A big smile on my face.
Ercan
Thank You again.
-
March 31st, 1999, 07:06 AM
#13
Re: Whats wrong with this code????
Ercan,
glad you found it!
Limits.h is one of the standard header files supplied with C and C++ compilers. It contains definitions for the maximum and minimum values of various implementation dependent types. These are types whose actual size is not explicitly specified by the language standard, but basically depends on the kind of system it is running on.
For example, an int is intended to be the most suitable type for holding and manipulating integers on a given system, so on a 16-bit system like Windows 3.1, an int is likely to be 16 bits, but on a 32-bit system like Windows 95 or NT, it will be 32 bits. Obviously, a 32-bit int can have far higher values than a 16-bit int. When you're writing code that needs to check for values that might overflow the maximum or minimum possible, it's useful to have these maximum and minimum values available.
The values defined in limits.h give you the size limits for these types.
Here is a typical extract from limits.h:
#if !defined(_WIN32) && !defined(_MAC)
#error ERROR: Only Mac or Win32 targets supported!
#endif
#define SHRT_MIN (-32768) /* minimum (signed) short value */
#define SHRT_MAX 32767 /* maximum (signed) short value */
#define USHRT_MAX 0xffff /* maximum unsigned short value */
#define INT_MIN (-2147483647 - 1) /* minimum (signed) int value */
#define INT_MAX 2147483647 /* maximum (signed) int value */
#define UINT_MAX 0xffffffff /* maximum unsigned int value */
#define LONG_MIN (-2147483647L - 1) /* minimum (signed) long value */
#define LONG_MAX 2147483647L /* maximum (signed) long value */
#define ULONG_MAX 0xffffffffUL /* maximum unsigned long value */
I hope this helps,
Dave
-
March 31st, 1999, 07:30 AM
#14
Re: It sure does help
Thank you for time DAVE. You have been a great GREAT help.
I have learned quite abit from you. Thank you Thank you.
I UNDERSTAND now....... hehehehe
( Yeeeeeeeeeeee Haaaaaaaaaaa )
Thank You
Ercan
-
March 31st, 1999, 08:03 AM
#15
Re: It sure does help
Ercan,
I'm glad to hear you're making progress. It takes a while, but it gets better as you go on :-)
Good luck!
Dave
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|