Forums → Programming Forum → Quick Use of Finding an Average
5 | 3232 |
- 5 Replies
Showing 1-5 of 5
5 | 3232 |
Well, this is going back to my high school days of Java Programming, and I just now remembered all the little tricks of finding averages and other useful mathematical equations. Here goes nothing, I guess...
package Listings;
public class Average
import java.util.Scanner;
{
public static void main (String[] Args)
{
int num1;
int num2;
int num3;
double average; //Prints three numbers and inputs their average.
Scannerscan = new Scanner (System.in);
System.out.println ("Enter the first number: "
num1 = scan.nextInt();
System.out.println ("Enter the second number: "
num2 = scan.nextInt();
System.out.println ("Enter the third number: "
num3 = scan.nextInt();
average = num1 + num2 + num3 / 3.0;
System.out.println ("Your average is... " + average);
}
}
So...what do you think? Surprised that I know JavaScript? LOL
If you want, you can add more numbers to it, just remember to add int4, int5, int6, and so on, while adding to the average equation. Now, I'm trying to remember how to do the Quadratic Formula...
You could query an input from the user asking how many numbers you want then loop through the input x number of times.
It also could be a good idea to implement try catches in there as it is sure to throw exceptions out the wazoo and kill it if the user gives input that is something other than ideal.
Note this is Java, not JavaScript.
I find it easier to re-run the input, whenever you need another average. Oh yeah, it is Java, I just accidentally put Script in there as well. XD
I have this written in C++
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
int inta;
int into;
int intus;
string help;
cout << "Step 1: Enter the first, btw you suck : ";
cin >> inta;
cout << "Step 2: Enter the second, still suck : ";
cin >> into;
cout << "Step 3: Enter the third: ";
cin >> intus;
double average = inta + into + intus/3;
cout << average;
system("PAUSE"
return 0;
}
Ehh nvm, did something wrong
perl -ne 'chomp;next if !/^-?\\d*\\.?\\d+$/ush @a,$_;END{print "Average of ", join(", ", @a), " = ", eval(join("+",@a))/@a,"\
";}'
Wow. The forum's parser is really broken. It completely messed up the code I pasted. Those double backslashes in the regexp were single backslashes, and the backslash-letter n towards the end of the code got turned into a backslash followed by an actual newline.
You must be logged in to post a reply!