473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

what's wrong with my code ?

hi plz i have a problem i have a h.w to be done & my code have something wrong so plz try to help me
the program wants the user to enter numbers until the user user enters a negative number then the program stops & output : max. no. ,min no. & avg .
& here is the code :

#include<iostream>
using namespace std;
void main()

{
float n=1;
float x;
float max;
float min;


cout<<"Enter the numbers separated by spaces: ";
cin>>x;
if(x>=0)
{max=x;
min=x;}

cin>>x;
while(x>=0)
{
if(x>max)
max=x;

if(x<min)
min=x;

cin>>x;

n++;
}

float avg=1;
float sum=0;
sum=sum+x;
avg=sum/n;



cout <<"Maximum:"<<max<<endl<<"Minimum:"<<min<<endl<<"A verage:"<<avg<<endl;


}



so what's wrong with it plz answer me as soon as u can
Mar 7 '08 #1
3 1668
satch
23
hi plz i have a problem i have a h.w to be done & my code have something wrong so plz try to help me
the program wants the user to enter numbers until the user user enters a negative number then the program stops & output : max. no. ,min no. & avg .
& here is the code :
Expand|Select|Wrap|Line Numbers
  1. #include<iostream> 
  2. using namespace std; 
  3. void main() 
  4.  
  5.     float n=1; 
  6.     float x; 
  7.     float max; 
  8.     float min; 
  9.  
  10.  
  11. cout<<"Enter the numbers separated by spaces: "; 
  12. cin>>x; 
  13. if(x>=0) 
  14. {max=x; 
  15.   min=x;} 
  16.  
  17. cin>>x; 
  18. while(x>=0) 
  19. if(x>max) 
  20. max=x; 
  21.  
  22. if(x<min) 
  23. min=x; 
  24.  
  25. cin>>x; 
  26.  
  27. n++; 
  28.  
  29. float avg=1; 
  30. float sum=0; 
  31. sum=sum+x; 
  32. avg=sum/n; 
  33.  
  34.  
  35.  
  36. cout <<"Maximum:"<<max<<endl<<"Minimum:"<<min<<endl<<"Average:"<<avg<<endl; 
  37.  
  38.  

so what's wrong with it plz answer me as soon as u can
Please add [code] tags around your code. Also read the following :
http://www.thescripts.com/forum/faq.php?faq=posting_guidelines#faq_how_to_ask_a_qu estion

Coming to errors in your code :
1. The value of variable 'n' is incorrect.
Lets see the flow of the code. By the time line 25 of your code gets executed you have read and analyzed two input values. As the value of n is 1 at this point of time, you should first increment the value of 'n' and then read the next input. So n++ should be before cin>>x(line 27)
2. Value of sum will be incorrect.
The variable 'sum' is being used to add up all the inputs except the last one. So the statement sum = sum + x should be executed whenever you have analyzed a valid value of 'x'. In you code the value of 'sum' will be the last value input for 'x' which will also be a negative value(resulting in a negative average)
I'll also tell you that you should declare and initialize 'sum' before reading the first input.
With this you should be able to figure out at what all places you should add the sum = sum + x statement.
Mar 7 '08 #2
Please add [code] tags around your code. Also read the following :
http://www.thescripts.com/forum/faq.php?faq=posting_guidelines#faq_how_to_ask_a_qu estion

Coming to errors in your code :
1. The value of variable 'n' is incorrect.
Lets see the flow of the code. By the time line 25 of your code gets executed you have read and analyzed two input values. As the value of n is 1 at this point of time, you should first increment the value of 'n' and then read the next input. So n++ should be before cin>>x(line 27)
2. Value of sum will be incorrect.
The variable 'sum' is being used to add up all the inputs except the last one. So the statement sum = sum + x should be executed whenever you have analyzed a valid value of 'x'. In you code the value of 'sum' will be the last value input for 'x' which will also be a negative value(resulting in a negative average)
I'll also tell you that you should declare and initialize 'sum' before reading the first input.
With this you should be able to figure out at what all places you should add the sum = sum + x statement.


thank you so much
but can i know where exactly should i put (n++) ?
Mar 7 '08 #3
satch
23
thank you so much
but can i know where exactly should i put (n++) ?
Hey i had mentioned that. Before cin>>x(line 27 in the code in my post). Basically cin>>x should be the last statement in the 'while' block.
while(<condition>){
//check if max is to be updated
//check if min is to be updated
//update count 'n'
//cin
}
Mar 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.