"Wrire a program that asks the user for a series of integers one at a time. When the user enters the integer 0, the program displays the following information.
1) the number of integers in the series (not including zero)
2) the average of the integers
3) the largest integer in the series
4) the smallest integer in the series
5) the range (difference between largest and smallest integer)
All I have is this so far, i'm stuck.
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
main()
{
int number;
int number2;
int number3;
int total;
int average;
int counter;
int largest;
int smallest = 130000;
int difference;
int addition;
do
{
cout << "Enter a series of integers, enter 0 when you're done." << '\n';
cin >> number;
if (number != 0)
{
17 2622
Sure You must be stuck since your first line in the program is wrong !!
... cout << "Enter a series of integers, enter 0 when you're done." << '\n';
cin >> number;
how can you request a series of integers from a user and then expects only one !!
you can't say cin>>number ; coz this will accepts only the first number that will be entered and ignores the rest ??
instead you should say cout<<"how many numbers ?"<<endl;
cin>>n;
cout<<"plz enter them "<<endl;
For (int i=0;i<n;i++)
cin>>a[i] ;
where n is their number ( 12 number 4 example) and a[i] is the array at which you will store these numbers ...
i hope you get that small part ...if u need more help .......post !
Are you sure it will only accept one number? I saw someone else do it like that, and they finished the program.. and it worked!
By the way, im writing this program using a do while loop.
Of course Using a do while is a different matter ...
using any loop will do the job ... for ,while , do..while ,...
so what is your problem now ??
"Wrire a program that asks the user for a series of integers one at a time. When the user enters the integer 0, the program displays the following information.
1) the number of integers in the series (not including zero)
2) the average of the integers
3) the largest integer in the series
4) the smallest integer in the series
5) the range (difference between largest and smallest integer)
All I have is this so far, i'm stuck. - #include <iostream.h>
-
#include <string.h>
-
#include <iomanip.h>
-
-
main()
-
{
-
int number;
-
//int number2; I see no reason for these two variables from your problem specification.
-
//int number3;
-
int total;
-
int average; // This should probably be a double, so you can get an accurate average
-
int counter;
-
int largest;
-
int smallest = 130000; // Why do you initialize smallest, but not largest? And why 130000?
-
int difference;
-
//int addition; Again, I don't see a use for this.
-
-
do
-
{
-
cout << "Enter a series of integers, enter 0 when you're done." << '\n';
-
cin >> number;
-
-
if (number != 0)
-
{
Actually, your code looks somewhat OK from what I can see. The best way to solve this problem is with a while loop - the condition will be (number != 0). In order for this to work, you will have to prompt the user for input once, before the loop begins, and at the very end of your loop.
Before the loop, it might be a good idea to set largest AND smallest to the value of number. This way, while inside your loop, it will be easy to set new values to smallest and largest with a pair of if statements: - if (number > largest) largest = number;
-
if (number < smallest) smallest = number;
Finally, make sure you initialize all remaining variables, so that you won't get garbage values from them.
Good luck!
Actually, your code looks somewhat OK from what I can see. The best way to solve this problem is with a while loop - the condition will be (number != 0). In order for this to work, you will have to prompt the user for input once, before the loop begins, and at the very end of your loop.
Before the loop, it might be a good idea to set largest AND smallest to the value of number. This way, while inside your loop, it will be easy to set new values to smallest and largest with a pair of if statements: - if (number > largest) largest = number;
-
if (number < smallest) smallest = number;
Finally, make sure you initialize all remaining variables, so that you won't get garbage values from them.
Good luck!
I initiliazed the smallest to 130,000 because its unlikely someone will enter 130,000.
I'm just having problems with figuring out how to do all this, how to figure out how to make it know the largest number, the average, etc.
I got this so far, I don't know what else to do!
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
main()
{
int number;
int total;
double average;
int counter;
int largest;
int smallest = 130000;
int range;
do
{
cout << "Enter a series of integers, enter 0 when you're done." << '\n';
cin >> number;
if (number != 0);
if (number > largest) largest = number;
if (number < smallest) smallest = number;
cout << "The number of integers you entered was << number << '\n';
cout << "The average of the integers is << average << '\n';
cout << "The largest integer in the series is << largest << '\n';
cout << "The smallest integer in the series is << smallest << '\n';
} while (number != 0);
return 0;
}
You want to know how to make it know the largest and the average ??!!
this can't be done unless you have all the numbers the user entered in an array so as to compare each number with the previous one and get the smallest .... that's the idea snd try to figure it out yourself ...i know its not easy bs you have to work a bit hard to reach
hint : use the array and assume that the largest number is the first one for example and then compare it with all other numbers , if there exist ine larger then that is the largest up till now
ex. Max=a[0] where a[0] is the first number
if (a[i] >max ) then max=a[i];
try it and post if u face troubles
You want to know how to make it know the largest and the average ??!!
this can't be done unless you have all the numbers the user entered in an array so as to compare each number with the previous one and get the smallest .... that's the idea snd try to figure it out yourself ...i know its not easy bs you have to work a bit hard to reach
hint : use the array and assume that the largest number is the first one for example and then compare it with all other numbers , if there exist ine larger then that is the largest up till now
ex. Max=a[0] where a[0] is the first number
if (a[i] >max ) then max=a[i];
try it and post if u face troubles
I'm not supposed to use arrays at all. This is how im supposed to do it.
I gotta figure out what to initialize all the int's to.
what a very very strange assignment !!!
what's your point of this strange program .......
since you assigned the smallest to 130000 or whatever ...you can assign the largest to one miliion then or 3*smallest ( 3 times the smallest) and the average (smallest+largest /2) but what the hell is that ????
plz if you got the answer from anywhere plz tell me coz im very curious to know what is going on
what a very very strange assignment !!!
what's your point of this strange program .......
since you assigned the smallest to 130000 or whatever ...you can assign the largest to one miliion then or 3*smallest ( 3 times the smallest) and the average (smallest+largest /2) but what the hell is that ????
plz if you got the answer from anywhere plz tell me coz im very curious to know what is going on
It's in a book, introduction to computer science using C++
"Wrire a program that asks the user for a series of integers one at a time. When the user enters the integer 0, the program displays the following information.
1) the number of integers in the series (not including zero)
2) the average of the integers
3) the largest integer in the series
4) the smallest integer in the series
5) the range (difference between largest and smallest integer)
From your problem specification, here's the pseudocode I imagine: - #include <iostream>
-
using namespace std;
-
-
int main() {
-
int num;
-
double ave;
-
int count = 0, sum = 0, smallest, largest;
-
-
cout << "Please enter a number, or 0 to quit: ";
-
cin >> num;
-
-
if (num != 0) {
-
largest = num;
-
smallest = num;
-
}
-
-
while (num != 0) {
-
count++;
-
if (num is larger than largest) largest = num;
-
if (num is smaller than smallest) smallest = num;
-
add num to sum;
-
cout << "Please enter a number, or 0 to quit: ";
-
cin >> num;
-
}
-
-
//Display relevant information here
-
//Calculate average by dividing sum by count
-
//You will have to cast either sum or count to a double type in order to get
-
// a decimal average.
-
//Range can be calculated by subtracting smallest from largest
-
-
return 0;
-
}
Okay, I got this. But this is a while loop, I need to write the program in a do while. And my problem with this is, it only lets you enter ONE number, and after you enter a number nothing happens! - #include <iostream>
-
#include <string.h>
-
#include <iomanip.h>
-
-
int main() {
-
int number;
-
double average;
-
double counter = 0;
-
int total = 0;
-
int smallest = 130000;
-
int largest;
-
int range;
-
-
cout << "Please enter a series of numbers, enter 0 to display the information " << '\n';
-
cin >> number;
-
-
if (number != 0) {
-
largest = number;
-
smallest = number;
-
}
-
-
while (number != 0) {
-
counter++;
-
average = total/counter;
-
if (number > largest) largest = number;
-
if (number < smallest) smallest = number;
-
range = largest - smallest;
-
total += number;
-
}
-
-
cout << "The number of integers you entered was" << counter << '\n';
-
cout << "The average of the integers is" << average << '\n';
-
cout << "The largest integer in the series is" << largest << '\n';
-
cout << "The smallest integer in the series is" << smallest << '\n';
-
cout << "The difference between the largest integer and smallest integer is" << range << '\n';
-
return 0;
-
}
TRY THIS - #include <iostream>
-
#include <string.h>
-
#include <iomanip.h>
-
-
int main() {
-
int number;
-
double average;
-
double counter = 0;
-
int total = 0;
-
int smallest = 130000;
-
int largest;
-
int range;
-
DO{
-
cout << "Please enter a series of numbers, enter 0 to display the information " << '\n';
-
cin >> number;
-
-
-
if (number != 0) {
-
largest = number;
-
smallest = number;
-
}
-
}
-
while (number != 0) {
-
counter++;
-
average = total/counter;
-
if (number > largest) largest = number;
-
if (number < smallest) smallest = number;
-
range = largest - smallest;
-
total += number;
-
}
-
-
cout << "The number of integers you entered was" << counter << '\n';
-
cout << "The average of the integers is" << average << '\n';
-
cout << "The largest integer in the series is" << largest << '\n';
-
cout << "The smallest integer in the series is" << smallest << '\n';
-
cout << "The difference between the largest integer and smallest integer is" << range << '\n';
-
return 0;
-
}
[/quote]
Well, it didn't work.
1. It repeats the statement everytime you enter a number
2. When you enter 0 the program closes
Well, the reason nothing happens is because you only ask for user input once. You have to ask for it again at the end of your while loop - otherwise, the end condition will never be satisfied!
Now, in order to use a do...while loop...that'll be a little weird. But apply the following changes: - #include <iostream>
-
#include <string.h>
-
#include <iomanip.h>
-
-
int main() {
-
int number;
-
double average;
-
double counter = 0;
-
int total = 0;
-
int smallest; // Does not need to be initialized.
-
int largest;
-
int range;
-
-
cout << "Please enter a number, or 0 to display the information: ";
-
cin >> number;
-
-
if (number != 0) {
-
largest = number;
-
smallest = number;
-
do {
-
counter++;
-
if (number > largest) largest = number;
-
if (number < smallest) smallest = number;
-
total += number;
-
cout << "Please enter a number, or 0 to display the information: ";
-
cin >> number;
-
} while (number != 0);
-
}
-
-
average = static_cast<double>(total) / count;
-
range = largest - smallest;
-
cout << "The number of integers you entered was" << counter << '\n';
-
cout << "The average of the integers is" << average << '\n';
-
cout << "The largest integer in the series is" << largest << '\n';
-
cout << "The smallest integer in the series is" << smallest << '\n';
-
cout << "The difference between the largest integer and smallest integer is" << range << '\n';
-
return 0;
-
}
Still the same problems, it repeats the same statement over and over everytime you enter a number.. and when you enter 0 the program closes!
Well, the repetition is expected and good - it means the program is taking these values and actually calculating them. The reason it is exiting...well...I didn't think you needed it, since you weren't including it with all your other samples, but put this statement in the line right before return 0;
This will pause the program so you can view the output, displaying a message like, "Press any key to continue . . ."
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Spamtrap |
last post by:
I only do occasional Perl programming and most things I write are
short processes. I have something I'm working on that is scanning a
text file with about 15 million lines and trying to extract...
|
by: aj902 |
last post by:
Hello ,
I am trying to create a program
where all detail,
http://www.albany.edu/~csi333/projects.htm
|
by: vgame64 |
last post by:
Hi, I have been struggling with writing a program for a few hours. The
requirements are that: """You will be writing a program which will
determine whether a date is valid in
terms of days in that...
|
by: robinsand |
last post by:
My apologies to those of you who are more advanced Visual C++ .NET
programmers, but I am working on a project for an MBA course that is
condensed into an eight-week schedule, and I need help...
|
by: pamelafluente |
last post by:
I am still working with no success on that client/server
problem. I need your help. I will submit simplified versions
of my problem so we can see clearly what is going on.
My model:
A client...
|
by: skumar434 |
last post by:
i need to store the data from a data base in to structure
.............the problem is like this ....suppose there is a data base
which stores the sequence no and item type etc ...but i need only...
|
by: naknak4 |
last post by:
Introduction
This assignment requires you to develop solutions to the given problem
using several different approaches (which actually involves using three
different STL containers). You will...
|
by: naknak |
last post by:
Introduction
This assignment requires you to develop solutions to the given problem
using several different approaches (which actually involves using three
different STL containers). You will...
|
by: peterggmss |
last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for...
|
by: raghavshastri |
last post by:
You are to write a C++ program to perform a statistical analysis of the blobs in an image. The image will be a grayscale image in PGM format for simplicity.
Here is a sample PGM image with 10...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
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...
| |