Jump to content

Java code assistance

Sdot

Hello guys, From line 41 to line 50 is something I tried to make that will give me the average of an array with 10 elements. The first element in the array is always doubled and I cannot understand why. Anyone know what is wrong? I have attached my .java file and I will also put it below.


        // Find the average of the values in the array
        int sum = values[0];
        for (int i = 0; i < currentSize; i++)
    {
        if (values >= 0)
        {
         sum += values;
        }
        average = sum/10;
        System.out.println("This is the sum " + sum);
    } 

L02ICEP4_5.java

Link to comment
Share on other sites

Link to post
Share on other sites

Change int sum= values[0] to int sum = 0

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SCHISCHKA said:

Change int sum= values[0] to int sum = 0

OH I GET IT, It was doubling because it was making sum also equal whatever I typed in to the console. You're right, I am an idiot. Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

I think you want to also check the way you are using values in the for loop. No index?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, SCHISCHKA said:

I think you want to also check the way you are using values in the for loop. No index?

Not sure what you mean

Link to comment
Share on other sites

Link to post
Share on other sites

The code block you have in the OP doesn't make use of your loop index (i.e. you're not saying "values[ i ]")

 

Pretty sure it's because [ i ] without spaces is interpreted as an italics tag in the forum posts, but then since you have it set to a code block, anyway, the italics doesn't take effect

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, mackncheesiest said:

The code block you have in the OP doesn't make use of your loop index (i.e. you're not saying "values[ i ]")

 

Pretty sure it's because [ i ] without spaces is interpreted as an italics tag in the forum posts, but then since you have it set to a code block, anyway, the italics doesn't take effect

I posted it, then edited in the code in block, not sure if that matters.

Edit: it definitely matters.

Here is what it looks like in my code.


        // Find the average of the values
        int sum = 0;
        for (int i = 0; i < currentSize; i++)
    {
        if (values[i] >= 0)
        {
         sum += values[i];
        }
        average = sum/currentSize;
        System.out.println("This is the sum " + sum);
    }

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, SCHISCHKA said:

I think you want to also check the way you are using values in the for loop. No index?

I had the index, just an error because I posted the code then added the code brackets afterwards because I didn't know about them until after I posted and saw something that said "please use code tags" in the announcements lol

Link to comment
Share on other sites

Link to post
Share on other sites

there are multiple things wrong with your code. you also won't get an accurate average with integers so i used doubles

double sum = 0;
for (int x : values) {
    sum += (double)x;
}
double average = sum/10;
System.out.println("This is the sum " + sum);

 

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, SCHISCHKA said:

there are multiple things wrong with your code. you also won't get an accurate average with integers so i used doubles


double sum = 0;
for (int x : values) {
    sum += (double)x;
}
double average = sum/10;
System.out.println("This is the sum " + sum);

 

You're right, I tried to change everything to doubles earlier but I got 10 errors saying I couldn't convert double to int. Couldn't find anything int in my code though so I changed everything back just so that it would compile and I could keep going. I just wanted to get the core assignment complete. then change everything to double or float when done.

 

The assignment says:

Declare an array of 10 floats or doubles

Write a program that reads a set of floating-point values or double values, ask the user to enter the values, then print

  • The average of the values
  • The smallest of the Values
  • The largest of the values
  • The range of the values

The range seems pretty easy, I think I can just subtract index 9 from index 0.

 

also I changed average = sum/10 to double average = sum/currentSize; just in case she tries a run without 10 numbers in the array.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Sdot said:

You're right, I tried to change everything to doubles earlier but I got 10 errors saying I couldn't convert double to int. Couldn't find anything int in my code though so I changed everything back just so that it would compile and I could keep going. I just wanted to get the core assignment complete. then change everything to double or float when done.

 

The assignment says:

Declare an array of 10 floats or doubles

Write a program that reads a set of floating-point values or double values, ask the user to enter the values, then print

  • The average of the values
  • The smallest of the Values
  • The largest of the values
  • The range of the values

The range seems pretty easy, I think I can just subtract index 9 from index 0.

 

 

If the assignmt says to declare an array of float or double why have you declared an array of integer?

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, SCHISCHKA said:

If the assignmt says to declare an array of float or double why have you declared an array of integer?

Some of our assignments piggyback off of a previous assignment. The previous assignment needed me to find the largest number in the array, and how many times it occurred in the array.

 

This new assignment added the float or double rule, getting the average value, smallest value, and range value.

Basically I used the code from the previous assignment and just continued with it.

 

Thanks for asking instead of just assuming I was an idiot, at least now you know I am just lazy lol.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×