Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 28th, 2005, 9:47 PM   #1
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Finding Average

How would I make a program that finds the average from a bunch of numbers the user inputs. The user would also input first how many numbers he/she will put in.
crawforddavid2006 is offline   Reply With Quote
Old Oct 28th, 2005, 10:16 PM   #2
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
An average, or mean, is defined as the sum of all the input divided by how many inputs there were.

Just count how many inputs you get, then make a loop to add them all up, then divide. Easy.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Oct 29th, 2005, 12:36 AM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
well, I assume you know how to find the average of numbers

eg:

1,
2,
3,
4

10
----
4

2.5 = average.

So, just apply this to the program. Get this inputs, add them up, and divide by the number of inputs you get.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 29th, 2005, 12:54 AM   #4
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
ok thanks for the help that seems a lot easier than i thought
crawforddavid2006 is offline   Reply With Quote
Old Oct 29th, 2005, 1:44 AM   #5
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
I still cant figure out how it is supposed to find the average. Could some one please help me figure out the loop that I need to use so that i can find the average.
crawforddavid2006 is offline   Reply With Quote
Old Oct 29th, 2005, 2:27 AM   #6
pal
Programmer
 
pal's Avatar
 
Join Date: Mar 2005
Location: Washington
Posts: 91
Rep Power: 4 pal is on a distinguished road
Try importing scanner, to receive input from the user. If you're trying to get the complete source from here, mind as well you can search through open source code sites for the average calculating program. I think it will be prtty easy to find, but if you don't get it I can post one too.
pal is offline   Reply With Quote
Old Oct 29th, 2005, 11:39 AM   #7
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Post some code, and we can go from there.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 29th, 2005, 2:47 PM   #8
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Just for a heads up I use something called the keyboard class. It works the same way as importing the scanner.
import cs1.Keyboard;

public class Average
{
    public static void main(String[] args)
    {
        System.out.println("Enter the number of numbers that you would like to find the average of: ");
        double num1 = Keyboard.readInt();
        for(double i = 1; i <= num1; i++)
        {
            int newNum = 0;
            System.out.println("Enter a number: ");
            double num2 = Keyboard.readInt();
            double average = 0;
            average = (average + num2) / i;
            if(i == num1 + 1)
            {
                System.out.println("The average is: " + average);
            }
        }
    }
}
crawforddavid2006 is offline   Reply With Quote
Old Oct 29th, 2005, 3:00 PM   #9
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
You don't have to "average" with every new number. You just find the sum of all the numbers and then when the loop is done, you divide by the total number of inputs and that's your average.

import cs1.Keyboard;

public class Average
{
    public static void main(String[] args)
    {
        double sum = 0.0, average;

        System.out.println("Enter the number of numbers that you would like to find the average of: ");
        double num1 = Keyboard.readInt();
        for(double i = 1; i <= num1; i++)
        {
            System.out.println("Enter a number: ");
            double num2 = Keyboard.readInt();
            sum += num2;
        }

         average = sum / num1;
         System.out.println("The average is: " + average);
    }
}
Kaja Fumei is offline   Reply With Quote
Old Oct 29th, 2005, 6:38 PM   #10
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Thankyou so much that helped a lot.
crawforddavid2006 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC