# array calculations with user input

• (2 Pages)
• 1
• 2

## 20 Replies - 12276 Views - Last Post: 10 March 2012 - 08:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=270339&amp;s=9d5685eac6581a316c4a98792da26758&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 computer_sorry

• New D.I.C Head

Reputation: 0
• Posts: 43
• Joined: 10-February 12

# array calculations with user input

Posted 10 March 2012 - 02:02 PM

The assignment is I'm supposed to

Write a program in java using arrays to calculate mean, variance and standard deviation of numbers. You should prompt ask the user how many numbers he/she wants to enter. The user should not be able to enter a negative number. When user provided you with a positive number, you should prompt as many times as the given number to enter numbers and store them all in an array. Your program should then show the mean, variance and standard deviation of these numbers.

I started it out, this is my first array program, but I don't really understand how to do the for loop and the method for all three of those calculations. Any guidance please, or a head-start would be really useful. I'm a young female, hoping to advance in the world of men, not for computer programming, but this class is a requirement. Any help would be much appreciated. Thanks.

```import java.util.Scanner;
public class computer_sorry {

public static void main(String args[]) {
Scanner input = new.Scanner(System.in);
System.out.print("Please enter the number you want to calculate.");
int n=input.nextInt();

double [] mathProb= new double[n];

for (int i=0; i<mathProb.length; i++) {

mathProb[i]=
//This is where I'm having trouble continuing..I see the examples for basic arrays, I don't know how to go further//

```

This post has been edited by computer_sorry: 10 March 2012 - 02:03 PM

Is This A Good Question/Topic? 0

## Replies To: array calculations with user input

### #2 jon.kiparsky

• Pancakes!

Reputation: 8824
• Posts: 15,234
• Joined: 19-March 11

## Re: array calculations with user input

Posted 10 March 2012 - 02:12 PM

```import java.util.Scanner;
public class AlqudA3Q1 {

public static void main(String args[]) {
Scanner input = new.Scanner(System.in);
System.out.print("Please enter the number you want to calculate.");
int n=input.nextInt();

// It would be good to check here whether the number is in the acceptable range

double [] mathProb= new double[n];

for (int i=0; i<mathProb.length; i++) {

mathProb[i]=
```

So what is it that you're going to do with each double in the array? The first thing is to put values in the array, which you already know how to do.

You then need to do the calculations on the values entered. You can write that using a dummy array, for example

```double[] testValues = {1.1, 2.2, 3.3, 4.4}
```

Calculate the required values manually, and then write the code to calculate them mechanically, and you have an easy way to see whether you're on the right track without having to enter values each time. (and without having to enter the same values each time, which would get boring, or to guess whether you're getting the right results, which would require more calculation than you want to do)

This post has been edited by jon.kiparsky: 10 March 2012 - 02:13 PM

### #3 tlhIn`toq

• Freelance developer

Reputation: 6094
• Posts: 13,445
• Joined: 02-June 10

## Re: array calculations with user input

Posted 10 March 2012 - 02:27 PM

I'm going to stop ya right there.
Typing before knowing where you are heading never turns out well.

Start by organizing your thoughts - then that becomes organizing your code. Even if all you do is write down the steps and stub out some empty methods for those.

There is an article in my blog for how to layout your homework (link in my signature block below)

The 'Double Pyramid' problem shows the same sort of thing you are looking for here: How to keep asking the user for input, using a loop.

### #4 jon.kiparsky

• Pancakes!

Reputation: 8824
• Posts: 15,234
• Joined: 19-March 11

## Re: array calculations with user input

Posted 10 March 2012 - 02:29 PM

tlhIn`toq, on 10 March 2012 - 04:27 PM, said:

I'm going to stop ya right there.
Typing before knowing where you are heading never turns out well.

A-freakin'-men to that. "Throwing code against the wall" is what I call it.

### #5 computer_sorry

• New D.I.C Head

Reputation: 0
• Posts: 43
• Joined: 10-February 12

## Re: array calculations with user input

Posted 10 March 2012 - 02:33 PM

Okay, I played a little more with it. I don't need help with the rest of the code, as in calculating the average and standard deviance--whatever. I just need to know if I actually 'established' an array with values using the user input? That's all I need to know!

Thanks to anyone and all.

```import java.util.Scanner;
public class computer_sorry {

public static void main(String args[]) {
Scanner input = new.Scanner(System.in);
System.out.print("Please enter the number you want to calculate.");
int num=input.nextInt();

do{
System.out.println("Please enter a positive number.");
}while (num !<=0);

double [] mathProb= new double[num];

for (int i=0; i<mathProb.length; i++) {
double[] mathProb = {num};

```

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8363
• Posts: 31,955
• Joined: 06-March 08

## Re: array calculations with user input

Posted 10 March 2012 - 02:48 PM

```       for (int i=0; i<mathProb.length; i++) {
System.out.print("Enter number " + (i+1) + ":");
mathProb[i]=  input.nextDouble();

```

### #7 tlhIn`toq

• Freelance developer

Reputation: 6094
• Posts: 13,445
• Joined: 02-June 10

## Re: array calculations with user input

Posted 10 March 2012 - 03:05 PM

Read this line out loud, and determine if this is really for POSITIVE numbers

11 }while (num !<=0);

Quote

Keep asking the user for a positive number..
While num (entered) is not less then or equal to 0

This post has been edited by tlhIn`toq: 10 March 2012 - 03:05 PM

### #8 computer_sorry

• New D.I.C Head

Reputation: 0
• Posts: 43
• Joined: 10-February 12

## Re: array calculations with user input

Posted 10 March 2012 - 03:27 PM

Ok I changed it. I know I'm still missing something..

I don't know how to use the do while loop and a for loop, in order to make sure the input is not negative. Is there a simpler way? Also, am I establishing the array in preparation for the calculations?

Thanks

```public static void main(String args[]) {
Scanner input = new Scanner (System.in);
int num=input.nextInt();

do{
System.out.print("Please enter the number you want to calculate.");
}while (num >0);

System.out.println("Please enter a positive number.");

double [] mathProb= new double[num];
for (int i=0; i<mathProb.length; i++) {
System.out.print("Enter number " + (i+1) + ":");
mathProb[i]=  input.nextDouble();
}
}

```

### #9 computer_sorry

• New D.I.C Head

Reputation: 0
• Posts: 43
• Joined: 10-February 12

## Re: array calculations with user input

Posted 10 March 2012 - 03:36 PM

Also, I am currently having issues trying to run my java programs on NetBeans..its keeps saying running and I've been waiting like a few minutes with nothing. Look forward to some help with my code and thanks so much guys, need to know what I'm missing.

### #10 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11390
• Posts: 42,919
• Joined: 27-December 08

## Re: array calculations with user input

Posted 10 March 2012 - 04:04 PM

Look at your code. Your loop will either execute once or infinitely depending on the value you input for num. Also, you aren't reading in new values for num nor are you storing them anywhere. So the array you create after the loop is useless.
```
int num=input.nextInt();

do{
System.out.print("Please enter the number you want to calculate.");
}while (num >0);

```

### #11 computer_sorry

• New D.I.C Head

Reputation: 0
• Posts: 43
• Joined: 10-February 12

## Re: array calculations with user input

Posted 10 March 2012 - 04:16 PM

@ mac Nerd,

Ok I see what you're saying. So I need to establish another int like data=0 for the actual values the user inputs. I don't quite know how to go about after that. If you could give me an example code? It doesn't have to be the same variables, just something to start for an array accessed with the for loop based off what I already have?

I am also working off a Mac and it sucks because it won't run the program. I know it's wrong, but I like getting the feedback to know what I"m doing wrong.

Thanks for help, very needed.

Not an assignment due anytime soon, but I'm not very good at programming and will soon be done with this class! I try and I know the info, just can't do any programming.

### #12 jon.kiparsky

• Pancakes!

Reputation: 8824
• Posts: 15,234
• Joined: 19-March 11

## Re: array calculations with user input

Posted 10 March 2012 - 05:18 PM

I would like to hear less about what's broken about your environment and more about what your specific questions are. I work on a mac, and it's not a problem. I can run NetBeans just fine, and also Eclipse, though I mostly work from the command line because it's easier. The macintosh is not your problem.
If you're getting compiler errors that you don't understand, you should post them. Someone will help you with them.

Checking the input:
You know that a while loop repeats as long as a given condition is true. So to ensure that the input is negative, you need to check whether the value is less than zero. If it is less than zero, ask for an array length. The loop I see above loops while the value is greater than zero - so if I tell you I want to enter seven numbers, you say "no, try again". You see how that's a problem?

Quote

I try and I know the info, just can't do any programming.

Nonsense. Utter balderdash.

### #13 computer_sorry

• New D.I.C Head

Reputation: 0
• Posts: 43
• Joined: 10-February 12

## Re: array calculations with user input

Posted 10 March 2012 - 05:30 PM

Yeah I realize I'm trying to end the loop now. Will post in another 30

THanks everyone

jon.kiparsky, on 10 March 2012 - 05:18 PM, said:

I would like to hear less about what's broken about your environment and more about what your specific questions are. I work on a mac, and it's not a problem. I can run NetBeans just fine, and also Eclipse, though I mostly work from the command line because it's easier. The macintosh is not your problem.
If you're getting compiler errors that you don't understand, you should post them. Someone will help you with them.

Checking the input:
You know that a while loop repeats as long as a given condition is true. So to ensure that the input is negative, you need to check whether the value is less than zero. If it is less than zero, ask for an array length. The loop I see above loops while the value is greater than zero - so if I tell you I want to enter seven numbers, you say "no, try again". You see how that's a problem?

Quote

I try and I know the info, just can't do any programming.

Nonsense. Utter balderdash.

### #14 computer_sorry

• New D.I.C Head

Reputation: 0
• Posts: 43
• Joined: 10-February 12

## Re: array calculations with user input

Posted 10 March 2012 - 06:17 PM

OK I Got IT!!!

now I have the following loop:

```double [] mathProb= new double[num];
for (int i=0; i<mathProb.length; i++) {
System.out.print("Enter number " + (i+1) + ":");
mathProb[i]=  input.nextDouble();
sum +=mathProb[i];
}
double average=sum/num;
System.out.println("The mean of the values is\n" +average);
double variance=sum-1/num;
System.out.println("The variation of the values is\n" + variance);

```

And I have to calculate the variance which is three numbers n1, n2 and n3= N then variance ‘V’=[ (n1-N)2 + (n2-N)2 + (n3-N)2 ] /3

I've never done that before..? In Calculus can someone please give me tips or show me how to do that in java?
Thanks everyone so much!

### #15 pbl

• There is nothing you can't do with a JTable

Reputation: 8363
• Posts: 31,955
• Joined: 06-March 08

## Re: array calculations with user input

Posted 10 March 2012 - 06:23 PM

computer_sorry, on 10 March 2012 - 08:17 PM, said:

I've never done that before..? In Calculus can someone please give me tips or show me how to do that in java?
Thanks everyone so much!

The same way you do it by hand in your Calculus courses