![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New to the CF scene Join Date: Sep 2009
Location: atlanta
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
mean and standard deviation.
mean and standard deviation. write a program that reads a set of floating-point data values from the input. when the user indicates the end of input, print out the count of the values, the average, and the standard deviation. the average of a data set the sum of the terms / the number of terms.
the standard deviation is: the square root of the sum minus.. blah blah, no big deal, i can handle that. -------------------------- i don't know how to read an unknown number of values and still account for all of the values. i don't know how to save the number of values. can you give me some tips on my homework assignment? |
|
|
|
|
|
PM User | #2 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
Use a loop while !invalid input. Define what you consider invalid, -1, a letter, nothing, etc. You can also prompt fist to ask how many entries there will be, and loop with that number.
Store the values in a vector or another type of collection. When it comes to sequential access and insertion where random access isn't necessary, I'd use a LinkedList. You could also extend this class just for the purpose of handling just floats with the sum, mean, std deviation and whatever else you need in it. Any collection is templatable so you can force it to only allow floats. This can actually save you a step since you can try/catch just the insertion directly into the collection instead of at the input. Kinda handy. I assume you know how to handle input? Scanner is a good class to look at if not. Oh and bookmark this one if you don't have it: http://java.sun.com/j2se/1.5.0/docs/api/ Collections are a part of the java.util package if you need to lookup some info.
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
|
|
PM User | #5 |
|
God Emperor ![]() ![]() Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
![]() ![]() ![]() |
Got to be careful here or it will be considered cheating.
A for loop can be used, but remember that its a controlled loop based on a numerical value. It can be used only if you previously declared a value to loop until. That being said, this is technically legal: for (;;) which will create infinite loops. We rarely see this, if we need to create infinite listeners more often you'll see a while(true).Validation can happen for datatypes during the scanner process. The next* methods throw three potential exceptions: InputMismatchException if the type cannot be retrieved using the next* method (you expect a number you get a string), NoSuchElementException if there is no token in the scanner, and IllegalStateException if the scanner has been closed. Should you catch the InputMismatchException, chomp the input with nextLine() and reprompt for input (otherwise, since you'll be in a loop it will likely generate an infinite prompt - this is because a failure on the next* will leave the token on the stack which will continuously evaluate as illegal). Now, if you're at a point where exception handling is not handled it is likely that you will not be expected to handle input validation at this time. If you still want to, you can use the hasNext* methods to handle validation without taking off of the stack and actually throwing an exception. In all honesty, this is the more 'correct' approach to scanner handling. I think thats about the most information I can give you without any code. Take a look at this tutorial from sun that shows some basic scanner handling: http://java.sun.com/docs/books/tutor.../scanning.html
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL? Lol
|
|
|
|
|
|
PM User | #6 |
|
New Coder ![]() Join Date: Oct 2009
Location: ~/
Posts: 61
Thanks: 1
Thanked 8 Times in 8 Posts
![]() |
You should have a look at the Scanner class.
Have a look at the next, nextLine, and nextDouble methods as they may all be of help to you. There are a few different approaches you could take. One way I would point out is: You could scan in the data as a string and try to use the static Double method parseDouble to see if the input is a double, then deal with it accordingly. Try and implement what you can and post if you have problems. cs_student Edit: Fou-Lu must of posted his while I was typing mine. He is much more experienced and I would use the procedures he layed out.
__________________
Last edited by cs_student; 11-01-2009 at 03:27 AM.. |
|
|
|
|
|
PM User | #7 |
|
New to the CF scene Join Date: Sep 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Here are the brief steps to store n integers in an array of numbers where n is variable.
- Declare an integer : int n; -use Scanner class to input value of n from keyboard using stmt Scanner input = new Scanner(System.in); - use a for loop to store n valuesin an array, for further use. import java.io.*; import java.util.Scanner; public class inputvalues{ // Create a single shared Scanner for keyboard input int ARRAY1[10]; private static Scanner scanner = new Scanner( System.in ); public static void main(String[] args) { // Prompt the user System.out.println( "Enter number of values: " ); // Read n from the keyboard. int number = scanner.nextInt(); for (int i=0;i<number;i++) ARRAY[i] = scanner.nextInt(); } Now use the values stored in array1for your calculations of std dev etc. |
|
|
|
|
|
PM User | #8 |
|
New Coder ![]() Join Date: Oct 2009
Location: ~/
Posts: 61
Thanks: 1
Thanked 8 Times in 8 Posts
![]() |
Please use code tags next time you post.
Also, it looks like you just posted your assignment. Now we know what you need to do, but how do we help you? We don't know what your having trouble with. What have you tried? Make sure to post (with code) the approaches you have taken to solve the problem. Also, do you understand what the code you posted does? What do you think it does. cs_student
__________________
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|