Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Calculating the average

A very simple piece of code for the beginner in C++. It demonstrates how to calculate the average of some numbers. Uses the ‘for’ loop.

On Saturday, March 20th 2004 at 11:48 PM
By Andrew Pociu (View Profile)
****-   (Rated 3.9 with 8 votes)
Contextual Ads
More C++ Resources
Advertisement


// Calculating the average

#include <iostream>
using namespace std;

int main()
{
    cout << "Enter how many values you want to add: ";
    int items;
    cin >> items;
    double num;
    double sum;
    sum = 0.0;
    for (int x = 1; x <= items; x++)
    {
        cout << "#" << x << " = ";
        cin >> num;
        sum += num;        // equal to sum = sum + num
    }
    cout << "Items = " << items << "\nSum = " << sum << "\nAverage = " << sum / items << "\n";
    return 0;
}




cout << "Enter how many values you want to add: ";



We prompt the user to enter the number of values he wants to calculate.


int items;
cin >> items;



Create a variable that stores the number of values that should be calculated and using "cin >>" to enter the input from the user's keyboard into our new variable. Let's suppose you wanted 3 values.


double num;
double sum;
sum = 0.0;



The 'num' variable will store the numbers the user will input.
We won't use an 'int' because some of the numbers the user will enter will probably be of the 'double' type (6.4, 3.2, 145.1).

The 'sum' variable will store the sum of all the numbers entered by the user. Also we initialize the variable to '0.0'.


    for (int x = 1; x <= items; x++)



We start the loop by creating a variable and setting it to the value '1'. We check if the variable is smaller than the number of items the user has entered. If it is the loop ends and displays the result. We also want to increment the variable by '1' (x++) every loop.


    cout << "#" << x << " = ";
    cin >> num;
    sum += num;        // equal to sum = sum + num



On the first line of the loop, we display the current number the user must enter. If you wanted to calculate 3 numbers you will get:


#1 = <here you enter the number that you want to calculate in the average>
#2 = <here you enter the number that you want to calculate in the average>
#3 = <here you enter the number that you want to calculate in the average>



After you enter all 3 numbers, the loop ends because 'x <= items' returns false.


    cin >> num;
    sum += num;        // equal to sum = sum + num



Using "cin >> num;" we store the input from the user's keyboard in the variable 'num'.
Then we assign to 'sum' the result of the current value stored in sum plus the new value entered by the user.

You should already know that 'sum += num' is the short way to write 'sum = num + sum'.


cout << "Items = " << items << "\nSum = " << sum << "\nAverage = " << sum / items << "\n";


We output the number of items the user choosed to enter, the sum of them and finally we divide the sum of the numbers entered by the user to the number of items.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Louisa on Wednesday, March 24th 2004 at 06:33 PM

I tried the code in an excel worksheet but I keep getting a syntax error. What gives?

by dustin on Thursday, March 25th 2004 at 06:22 AM

This is the worst thing i've ever seen i mean u didnt explain anything come on i want to know what the commands mean good lord!

by Andrei Pociu on Tuesday, April 6th 2004 at 03:58 PM

I said this script is for the C++ beginner, but not the 'total beginner'. You should have a little knowledge about C++ loops and other basic syntax.
Anyway, I updated the tutorial and explained the code.

by Jasen Penn on Saturday, October 9th 2004 at 12:01 AM

I think it was straight forward. I am a newby and was trying to write a code that calculates and print the average rainfall over a period of days terminated by 9999.

by chizengwe on Sunday, July 10th 2005 at 08:23 AM

all programs run, i just hope to keep on relying on you as a source for code trouble shooting. keep up the good work

by clueless loser on Wednesday, October 19th 2005 at 10:00 AM

I know this sounds dumb, but hey, I'm a blonde! Just kidding, what I was going to say is when you calculate the mean or average of a set of numbers, do you add them and then divide. I'm having one of those blank mided blonde moments.

by Andrei Pociu on Wednesday, October 19th 2005 at 10:58 AM

You add them and then divide them by their number. For example you have 3 numbers: 6, 7 and 8. Add them and you get 21. Divide 21 by 3 (we had three numbers) and you get the average: 7.
If you had 4 numbers, we would divide by 4 instead.

by Lost-Visual Basic Le on Monday, May 28th 2007 at 05:13 PM

I'm writing a program that requires inputting only 25 numbers that can be anywhere from 0-100. Now, how would you write a code to calculate the mean and standard deviation for this?

Lost

by kevin on Tuesday, April 22nd 2008 at 10:43 AM

please i will need some help on how to write a code in C to calculate the mean of numbers arranged in colunms,accessing these numbers from a text file. thanks

by Jeremee Jade Lee on Sunday, June 7th 2009 at 12:03 PM

It's FANTASTIC!!! You did a great job! Thank you for your help... You have a good logic...=.)

by Hammad Ansari on Friday, February 5th 2010 at 09:57 AM

Using do-while loop, make a program that continuously ask for the numbers

by allan on Monday, July 12th 2010 at 10:00 AM

nd ko maintindihan ang gawa mo




gawa uli kayo ng bago

ang layo ng sagot nyo!!!!! pde


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources