0
woodpecker

JAVA arrays

Recommended Posts

I'm trying to write a program that will read a list of values, one per line, and outputs their sum. Then each value will be printed with its percentage of the sum.

I'm using a method that will take the entire array as one argument and return the sum of the array. And Now I'm stuck. Any ideas? I can post the coding I have so far if it will help.


here is what the program is supposed to look like running.

How many values? 4
Enter value: 4
Enter value: 3
Enter value: 5
Enter value: 6
The sum is 18

Percentages:

4 is 22.2% of 18
3 is 16.7% of 18
5 is 27.8% of 18
6 is 33.3% of 18



I HATE JAVA >:(
SONIC WOODY #146

There is a fine line between cockiness and confidence -- which side of the line are you on?

Share this post


Link to post
Share on other sites
import java.util.*;

public class value
{
public static void main(String [] args)
{

Scanner stdin = new Scanner(System.in);
int numberOfValues;
System.out.println("How many values? ");
numberOfValues = stdin.nextInt();

int [] numbers = new int [numberOfValues];

int sum;

for(int 1=0; i < numbers.length; i++)
{
System.out.println("Enter a value: ");
numbers = stdin.nextInt();
}

sum = getSum(numbers);
System.out.println("The sum is " + sum);
}

-------------------this is where I get lost. Not sure how to add the coding into the getSum method so the sum is calculated from the array.


public static int getSum(int [] list)
{
int s;
s = list[0];

???
???
???

return s;

}
}
SONIC WOODY #146

There is a fine line between cockiness and confidence -- which side of the line are you on?

Share this post


Link to post
Share on other sites
here's a quick solution. there might be some typos, but it should work fine.


void printData(int data[]) {
int sum=0;
for(int i=0; isum+=data[.i];

DecimalFormat df = new SimpleDecimalFormat("0.0");

for(int i=0; idouble percentage = (100.0*data[.i])/sum;
System.out.println(data[.i] + " is " + df.format(percentage) + "% of " + sum;
}
}


(Take the dots out after the brackets)
This ad space for sale.

Share this post


Link to post
Share on other sites
try this:

import java.util.*;

public class value
{
public static void main(String [] args) {

Scanner stdin = new Scanner(System.in);
int numberOfValues;
System.out.println("How many values? ");
numberOfValues = stdin.nextInt();

int [] numbers = new int [numberOfValues];

int sum;

for(int i=0; i < numbers.length; i++)
{
System.out.println("Enter a value: ");
numbers = stdin.nextInt();
}

sum = getSum(numbers);
System.out.println("The sum is " + sum);
}

public static int getSum(int [] list)
{
int iSum = 0;

for(int i=0; i < list.length; i++)
{
iSum = iSum + list;
}

return iSum;

}
}



weird, replying doesn't show the brackets [] ...

should be:

numbers BRACKET "i" BRACKET = stdin.nextInt();

and

iSum = iSum + list BRACKET "i" BRACKET;

Share this post


Link to post
Share on other sites
Quote


I HATE JAVA >:(



Then try Perl or another interpreted language sometime. This took about 3 minutes of coding:



#!/usr/bin/perl

print "Type in your numbers, hit CTRL-D when done:\n";
@numbers = ;

foreach $number(@numbers) {

chomp $number;
$total += $number;
}


print "Total sum is $total\n";

foreach $number(@numbers) {

chomp $number;
print "$number is " . $total / $number . "% of $total\n";
}


I do like Java though, even though I've rarely been able to use it for anything I've worked on. It was my first OO language and probably one of the better ones to learn on. I'm starting up next month as a Ruby on Rails developer and it'll be interesting to see how well that language works out.

Share this post


Link to post
Share on other sites
Thanks all for all the great help. Much appreciated and it now works. Thank god this is the only programming class I need and only have 4 more weeks of it. Unless other Object Orienting languages make more sense....I'm done. :ph34r:

Now off to the percents.

Thanks again all.

I owe beer at a dropzone near you!

v/r

Billy
SONIC WOODY #146

There is a fine line between cockiness and confidence -- which side of the line are you on?

Share this post


Link to post
Share on other sites
hahaha

Quote

Unless other Object Orienting languages make more sense....I'm done. :ph34r:




Yep, you're done. I won't bother pointing (get it, haha :|) you to C++, it'll make your head explode.

Good luck with the remaining 4 weeks! :)

Jen
Arianna Frances

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0