473,396 Members | 2,089 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,396 software developers and data experts.

Print number of times an input was given, and take the average of the inputs

Hello, and thanks for any help in advance!

Basically, as the title says, I am looking for a way to print the number of times the user gave the program an input, and then also print the average of all the inputs.

So far, I have initialized three variables as follows:

Expand|Select|Wrap|Line Numbers
  1. int counter=0;
  2. int sum=0;
  3. float average=0;
  4.  
Do I need to use a loop to do this? I have also written the expressions for the three variables:

Expand|Select|Wrap|Line Numbers
  1. sum = sum + size;
  2. counter = counter + 1;
  3. average = sum/counter;
  4.  
I have tried printing the values of counter and average, and I get a negative counter value and a nonense average value.

So, in summary, do I need a loop to print the number of inputs and their average? Also, using a loop or not, how do I get the second three lines of code to work correctly?

Thank you in advance for any help!!
Apr 6 '08 #1
3 1980
whodgson
542 512MB
Yes you need to declare size and since you are keeping track of sum, count and average you need to control with a loop. You also need to input something with cin>>.At the moment the three variable = 0 and you are also dividing by 0
Apr 6 '08 #2
I already have declared size, and have an input already set for size, and the program, which prints squares, works perfectly except for this part. I just didn't want to post too much code.

But how do I actually set up the loop using them? That is the problem I am having.
Apr 6 '08 #3
whodgson
542 512MB
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. cout<<"Enter an integer";
  2. for(int i=0;i<20;i++)
  3. {cin>>integer;
  4. numberIntegersEntered+=1;
  5. sum+=integer;
  6. averageSum=sum / numberIntegersEntered;
  7. cout<<"The average of the integers entered is: "<<averageSum<<endl;
  8. //if this was a function you could add:
  9. return numberIntegersEntered;//and display this value
  10.  
Apr 10 '08 #4

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

Similar topics

12
by: xeys_00 | last post by:
#include<iostream> #include<cstdlib> //This program will be a number guessing game int main() { //variables for the game
7
by: Jean-David Beyer | last post by:
I have six hard drives (4 SCSI and 2 EIDE) on my main machine with parts of a database on each drive. The main index is on one SCSI drive all to itself. The main data are on the other three SCSI...
8
by: Ronen Kfir | last post by:
I need to take as an input matrix N*M from stdin & print it as a matrix shape, something like: |6 8 2 4 7| |4 0 1 8 2| |3 1 5 2 6| |9 3 8 4 0| I don't have any idea how...
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
13
by: amykimber | last post by:
Hi all, I know I'm doign something really daft, but I can't get this to work... I have a form with a bunch of inputs called ship_owner - why the ? Because I'm submitting this page though php...
3
by: rwise5 | last post by:
I have been tasked with finishing the following C program. I need to develop the sort by birthday function and the print grade function. I have been working on the print function for the last week...
1
by: brnkstyle | last post by:
I figured out how to calculate the work day given two values but all of my records have dates and i want to be able to calcuate the average time it takes to do a job per month so basically take all...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole number and if the template parameter is an...
5
by: totomalas | last post by:
I have developed a report in Access 2007 that runs on three queries, each promting an input from the user...this report is used for a meeting and it should be printed nine times...what I do now is...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.