![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#2 |
|
Professional Programmer
|
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;} |
|
|
|
|
|
#3 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#4 |
|
Expert Programmer
|
ok thanks for the help that seems a lot easier than i thought
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Mar 2005
Location: Washington
Posts: 91
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#7 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Post some code, and we can go from there.
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#8 |
|
Expert Programmer
|
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);
}
}
}
} |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
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);
}
} |
|
|
|
|
|
#10 |
|
Expert Programmer
|
Thankyou so much that helped a lot.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|