Forumchem - Forum with AI(ALICE BOT & HAL9000) and TTS

More dificult for us, more easy for you
It is currently Fri Jul 30, 2010 5:30 am

All times are UTC





Post new topic Reply to topic  Page 1 of 1
 [ 6 posts ] 
Author Message
 Post subject: My C++ code is not working how it supposed please HELP..!!!
PostPosted: Thu Mar 04, 2010 2:19 pm 
Offline
User avatar

Joined: Sat Oct 17, 2009 5:27 pm
Posts: 7
include
#include
using namespace std;


int main()

int Hmany,Enumber,sum,Lnumber,Snumber;
double average;

cout << How many numbers do you wish to enter ;
cin >> Hmany;

for (int i=; i
cout << Enter a Number <cin >> Enumber;

sum = sum + Enumber;

if (Lnumber=NULL)
Lnumber=Enumber;
else;

if (Lnumber>Enumber)
Lnumber = Enumber;
else

if (Snumber = NULL)
Snumber = Enumber;
else

if (SnumberSnumber = Enumber;




cout << the sum is <
average = sum/Enumber;
cout<< the average is << average;

cout << the largest is <
cout << the smallest is << Snumber;

return 0;






and this is how my output supposed to look.


How many number do you wish to enter 6
Enter a number: 4
Enter a number: 11
Enter a number: 2
Enter a number: 15
Enter a number: 7
Enter a number: 12

The sum is 51
The average is 8.50
The largest is 15
The smallest is 2


Top
 Profile  
 
 Post subject: My C++ code is not working how it supposed please HELP..!!!
PostPosted: Thu Mar 04, 2010 4:06 pm 
Online
User avatar

Joined: Thu Apr 02, 2009 6:54 am
Posts: 347
The guy above me is exactly right, you should have set the variable called sum to 0 first, otherwise the program will use whatever junk value is already stored within it.
When you are finished using a particular bit of memory and delete the contents, you aren"t actually getting rid of the content, the memory is just flagged as available for writing to and the contents remain until they are written over.
The memory allocated to sum will have been used for something else previously, this could be anything at all: a variable in another one of your programs or something completely unrelated in some other software It"s always a good idea to initialise your variables on declaration if possible.

Another thing I"ve just noticed is the classic =/== error in your if statements. = operator is assignment, which will evaluate to true, you need == to test for equality. It"s nothing to be ashamed of as every programmer has and still does make this mistake from time to time, no exceptions.

Hope this helps.


Top
 Profile  
 
 Post subject: My C++ code is not working how it supposed please HELP..!!!
PostPosted: Thu Mar 04, 2010 4:38 pm 
Offline
User avatar

Joined: Thu Apr 02, 2009 6:23 pm
Posts: 263
I am a beginner myself but I think you need to set sum to 0 to start.

int sum = 0


Top
 Profile  
 
 Post subject: My C++ code is not working how it supposed please HELP..!!!
PostPosted: Thu Mar 04, 2010 5:37 pm 
Online
User avatar

Joined: Fri Apr 03, 2009 12:31 am
Posts: 334
1. on the first line i replaced the int with double, because the result of an integer division is different from the result of a double division.
2. all variables became inititalized to zero with exception of Lnumber and Snumber
3. Lnumber is initialized to an arbitrary value defined in limits.h LONG_MIN
4. Snumber is initialized to an arbitrary value defined in limits.h LONG_MAX.
5. the average calculated by your formula is different from the average output in your sample screen.
6. In case the first number entered is smaller than LONG_MIN or larger than LONG_MAX this code won"t work.
7. In front of the cout section (where you output your results) I closed the for loop.

#include
#include
#include
using namespace std;

int main()

double Hmany = 0.0,
Enumber = 0.0,
sum = 0.0,
Lnumber=LONG_MIN,
Snumber = LONG_MAX;
double average = 0.0;

cout << How many numbers do you wish to enter ;
cin >> Hmany;

for (int i=0; i cout << Enter a Number: ;
cin >> Enumber;
sum = sum + Enumber;

if (Lnumber Lnumber = Enumber;

if (Snumber>Enumber)
Snumber = Enumber;


cout << the sum is < average = sum/Enumber;
cout<< the average is << average << endl;
cout << the largest is < cout << the smallest is << Snumber << endl;
system(Pause);
return 0;


Top
 Profile  
 
 Post subject: My C++ code is not working how it supposed please HELP..!!!
PostPosted: Thu Mar 04, 2010 6:04 pm 
Online
User avatar

Joined: Thu Apr 02, 2009 7:58 pm
Posts: 475
A couple minor things first:
- Initialize your variables to 0 first
- NULL is for pointers, check if the number is 0
- In the if( ) statements, you want == not =, assignments are always true
- you never figure out the average, but you print it anyway

but your biggest logic problem is probably this:

if (Lnumber=NULL)
Lnumber=Enumber;
else;

I don"t think you want the semicolon after the else, it makes for a null else clause


Top
 Profile  
 
 Post subject: My C++ code is not working how it supposed please HELP..!!!
PostPosted: Thu Mar 04, 2010 9:32 pm 
Online
User avatar

Joined: Thu Apr 02, 2009 6:59 am
Posts: 349
You have a number of problems here:
- not initializing variables
- missing on your "for" statement
- using = instead of == for equality check ( = is assignment )
- using the last number entered for your avg caculation, instead of Hmany
- using integer arithmetic for your avg calculation

Your code should look something like this:

#iinclude
#include
#include

using namespace std;

int main(int argc, char *argv[])
int Hmany, Enumber,sum = 0,
Lnumber = INT_MIN, Snumber = INT_MAX;
double average;

cout << How many numbers do you wish to enter ;
cin >> Hmany;

for (int i = 0; i < Hmany; i++)
cout << Enter a Number : ;
cin >> Enumber;
sum += Enumber;
Lnumber = max(Enumber, Lnumber);
Snumber = min(Enumber, Snumber);


cout << endl << the sum is << sum << endl;
average = static_cast(sum) / Hmany;
cout << the average is << average << endl;
cout << the largest is << Lnumber << endl;
cout << the smallest is << Snumber << endl;

return 0;


#if 0

Sample run:

How many numbers do you wish to enter 6
Enter a Number : 4
Enter a Number : 11
Enter a Number : 2
Enter a Number : 15
Enter a Number : 7
Enter a Number : 12

the sum is 51
the average is 8.5
the largest is 15
the smallest is 2

#endif



Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  Page 1 of 1
 [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 9 guests


 
Search for:
 
Jump to:  

cron
Click me:
Powered by phpBB © 2000, 2002, 2005, 2007, 2008, 2009 phpBB Group
Chronicles phpBB3 theme by Jakob Persson. Stone textures by Patty Herford.
With special thanks to RuneVillage

This site have 4 type of tecnology in order to convert text to speech. By default you use the vozme tecnology. In order to know the other you need to sign for.