Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Integers calculation

Hello guys,
I am trying to write a program that calculates 6 numbers which are (1,2,3,4,1,0).

Calculating the (1) Maximum, (2) The Count, (3) The Sum, (4) The numbers of '1's and (5) The Average.
I am am having problem with it, really do need some help please. This is what I have tried to do so far.
Thanks

public class StreamCalc
{
// instance variables
private int [] x=(1,2,3,4,1,0);

/**
* Constructor for objects of class StreamCalc
*/
public StreamCalc()
{
max = 4;
while(x != max){
return max;
System.out.println("The maximum of the numbers is " + max);
}

count = 0;
while(x < count){
count++;
return count;
System.out.println("The total integers is " + count);
}

sum = 0;
for (x < sum){
sum= x + x;
return sum;
System.out.println("The sum of all the numbers is" + sum);
}


if(x = 1){
x++;
return x;
System.out.println("The number of ones is" + x);
}

average= 0.0;
avearge= sum / count;
return average;
System.out.println("The average of the numbers is" + average);
}
}
[1318 byte] By [Aristo] at [2007-11-11 8:29:15]
# 1 Re: Integers calculation
Your methods are too specific to the data you think you're dealing with.

Think about how you are going to iterate through the array ONCE and perform the various calculations/comparisons you need.

so - what values do you need to track?
what calculations do you need to perform after you have those values?

usually for a "max", initialize your value with something that is lower than any expected value in your array. Here, I'd suggest -1.

set count, sum, num1s to 0.

Your processing will start with your reading of each element and incrementing your count, increasing your sum, comparing to reset max, and determining if you have a 1.

once those processes are done, outside of the step through your array loop, you do your sum / count as the output for the statement

System.out.println("The average of the values is " + sum/count)

import statements

public class StreamCalc
{
// instance variables
private int[] myArray;
private int sum = 0;
private int count = 0;
private int num1s = 0;
private int max = -1;

// are you going to have an array passed in or are you going to
// ask the user for input?
public StreamCalc()
{

}

private void processArray()
{
for( int i = 0; i < myArray.length; ++i )
{

do your processing here

}
}

private void outputValues()
{
System.out.println() statements
}

public void static main( String[] args )
{

}

}
nspils at 2007-11-11 22:35:19 >
# 2 Re: Integers calculation
usually for a "max", initialize your value with something that is lower than any expected value in your array. Here, I'd suggest -1.
well, that's not that safe and only should be used if there is no other possibility. usually start with setting max = array[0]. then iterate through the array starting at 1 (instead of 0) comparing with the max value. dito for min.
graviton at 2007-11-11 22:36:19 >
# 3 Re: Integers calculation
My suggestion for how to calculate "max" was intended to be generic - able to be used when you are dealing with a stream or a structure other than an array. That is also why I said that it should be something smaller than any expected input value.
nspils at 2007-11-11 22:37:23 >
# 4 Re: Integers calculation
hi nspils, this is going to be a debate on principles :)
in a stream you also have a first element, which can be set to be the max / min. i guess you will agree, that the approach i suggested is more generic, since it makes no assumptions on the range of the data. in a stream, your approach could be the faster way, since on my approach one would have to write a condition to check if the first element was read and the actual one is not the first. by the way, making assumptions on the range of the data can be very difficult and dangerous.
graviton at 2007-11-11 22:38:17 >
# 5 Re: Integers calculation
hey, grav -

I think that, most of the time, we know the data we are going to be dealing with. It is pretty tough to design accurately if we have no idea about the range of data. If stuff is totally unpredictable, make the "max" the totally smallest integer that can be represented in the system, or the smallest float possible (- infinity) if that is the data type of the values.

That said, I agree that taking the first value and setting it to max will work - no matter how data is entering - we just need to be sure we can read the first value at least once without disposing of it in order to accomplish that first assignment, before the program enters the controlled structure which is processing our data.
nspils at 2007-11-11 22:39:21 >