Hello guys......Cindy here

I am trying to get my program working and I am getting really frustrated. I am writing this program to familiarise myself with arrays, methods & loops so i can improve my Java skills.

Im not lazy guys and have tried searching yahoo ,answers, forums and google for some answers but cant find what im looking for.

what i'm trying to do is write a program that given a set of numbers to be stored in an array, calculates and outputs the following to the screen:

* the total of the numbers entered((i know this bit works))
* the largest number
* the smallest number
* the average of the numbers
* the middle value

here is what i have so far

class javaprogram
	{
	public static void main(String []args)
	{
		int total=0;		//used to store the total of the array item.
		System.out.println("How many numbers do you wish to enter?");
		int n = EasyIn.getInt();

		//declare array called numbers
		int[] numbers = new int[n];


		//fill the array with numbers read in by the user.
		for(int i = 0; i< numbers.length; i++)
		{
			System.out.println("Enter number " + (i+1));
			numbers[i] = EasyIn.getInt();
		}


		//call the method 'totalMethod'
		//passing the array 'numbers' as an arument

		System.out.println("Total is " + totalMethod(numbers));
		System.out.println("The biggest number is " + biggest(numbers));
		System.out.println("The smallest number entered is " + smallest(numbers));
		System.out.println("The average value entered is " + average(numbers));
		System.out.println("The middle value entered is " + middle(numbers));

		}
		// end main

		public static int totalMethod(int[] nums)
		{
		// variable total used to accumulate the totals in the array.
			int total = 0;

			for (int i=0; i < nums.length; i++)
			{
				total = total + nums[i];
			}

			return total;
		}// end totalMethod



		public static int biggest(int[] nums)
		{
		//variable biggest used to calculate biggest number.

			int biggest = (int [] nums)
			{
			int largest = nums[0];

			for (int count =0; count > nums.length; count ++) {
			if (intArray [count] > largest)
			largest = nums [count];


			}

			return largest;





		public static int smallest(int[] nums)
		{
		//variable smallest used to calculate smallest number

			int smallest (int [] nums)
			{
			 int least = nums[0];

			 for (int count =0; count < nums.length; count ++) {
			 if (nums [count] < least)
			 least = nums [count];
			}

			return least;

			}
			
			
			
		public static double average(int[] nums)
		//variable average used to calculate the average value
		
			int average (int [] nums)
			{



			//end main

am i correct in thinking that i have to create a different method for each of the calculations i want to return?

Hope someone can help or even point me in the right direction, i have no idea how to calculate the average or the middle value entered and need some advice

Thanks guys

Cindy

Recommended Answers

All 5 Replies

Well I have created most of your program for you, except the method that calculates the middle value of the array.
I didn't want to do this because: you can try implementing this method yourself for the challenge :P, I don't want to do it at this time as I'm lazy, and finally its more complex than the others so enjoy lol

My code isn't the most efficient but it works and that's most important. As for your code, your code seemed too complicated to me so i decided to simplify it greatly. Each calculation is in its own method (average, greatest & smallest, total) except how the user enter's their values into the array, which is placed in the main method.

So here's the code and I hope it points you in the right direction:

class usingArrays
{
	
	public static void main(String []args)
	{
		Scanner in = new Scanner(System.in);
		System.out.println("How many numbers do you wish to enter?");
		int n = in.nextInt();
 
		//declare array called numbers
		int[] numbers = new int[n];
 
 
		//fill the array with numbers read in by the user.
		for(int i = 0; i< numbers.length; i++)
		{
			System.out.println("Enter number " + (i+1));
			numbers[i] = in.nextInt();
		}
		
		totalMethod(numbers);
		smallandLarge(numbers);
		findAverage(numbers);
		

	}// end main

		public static int totalMethod(int[] nums)
		{
		// Variable total used to accumulate the totals in the array. 
		// If we enter numbers that have a decimal place then we must change the type of total
			
			int total = 0;
 
			for (int i=0; i < nums.length; i++)
			{
				total = total + nums[i];
			}
			System.out.println("Total: " +total);
			return total;
		}// end total method
 
 
 
		public static void smallandLarge(int[] nums)	// Determine the largest and smallest value in the array
		{	
			int largest_num = nums[0]; 		// Initially assign largest number value to value of array position 0
			int smallest_num = nums[0];
			
			for(int i=1;i< nums.length;i++)	// Work through the array
			{
				if(largest_num < nums[i])
					largest_num = nums[i];		// Assign the value to largest if number is greater than previous number
				else if(smallest_num > nums[i])
					smallest_num = nums[i];
			} 
			
			System.out.print("\nLargest Number : "+largest_num);
			System.out.print("\nSmallest Number : "+smallest_num);
		
		}// end smallandLarge method
		
		public static void findAverage(int [] nums)
		{
			float total = 0;
			float average;		// Variable type is float because we cannot assume that the value returned will whole number
			
			for(int i = 0; i < nums.length; i++)
				total = total + nums[i]; 
			
			average = total/nums.length;
			
			System.out.printf("\nAverage: %.2f",average);		// Print the output in a certain format
		}
		
	
		
}//end class
commented: extremely helpfull and informative post +0

wow.....thank you so much Katana for taking the time to help me. when i try to compile that code im getting 2 errors related to the Scanner in (cannot find symbol)

Should i have a file called 'scanner in' in the directory where i have my file saved? I have not used scanner in yet and have only used easyIn at this stage. Is scanner in totally something different than easyIn?

once again thank you for taking the time to help me out, really really appreciated

Cindy

Well I have created most of your program for you, except the method that calculates the middle value of the array.
I didn't want to do this because: you can try implementing this method yourself for the challenge :P, I don't want to do it at this time as I'm lazy, and finally its more complex than the others so enjoy lol

My code isn't the most efficient but it works and that's most important. As for your code, your code seemed too complicated to me so i decided to simplify it greatly. Each calculation is in its own method (average, greatest & smallest, total) except how the user enter's their values into the array, which is placed in the main method.

So here's the code and I hope it points you in the right direction:

class usingArrays
{
	
	public static void main(String []args)
	{
		Scanner in = new Scanner(System.in);
		System.out.println("How many numbers do you wish to enter?");
		int n = in.nextInt();
 
		//declare array called numbers
		int[] numbers = new int[n];
 
 
		//fill the array with numbers read in by the user.
		for(int i = 0; i< numbers.length; i++)
		{
			System.out.println("Enter number " + (i+1));
			numbers[i] = in.nextInt();
		}
		
		totalMethod(numbers);
		smallandLarge(numbers);
		findAverage(numbers);
		

	}// end main

		public static int totalMethod(int[] nums)
		{
		// Variable total used to accumulate the totals in the array. 
		// If we enter numbers that have a decimal place then we must change the type of total
			
			int total = 0;
 
			for (int i=0; i < nums.length; i++)
			{
				total = total + nums[i];
			}
			System.out.println("Total: " +total);
			return total;
		}// end total method
 
 
 
		public static void smallandLarge(int[] nums)	// Determine the largest and smallest value in the array
		{	
			int largest_num = nums[0]; 		// Initially assign largest number value to value of array position 0
			int smallest_num = nums[0];
			
			for(int i=1;i< nums.length;i++)	// Work through the array
			{
				if(largest_num < nums[i])
					largest_num = nums[i];		// Assign the value to largest if number is greater than previous number
				else if(smallest_num > nums[i])
					smallest_num = nums[i];
			} 
			
			System.out.print("\nLargest Number : "+largest_num);
			System.out.print("\nSmallest Number : "+smallest_num);
		
		}// end smallandLarge method
		
		public static void findAverage(int [] nums)
		{
			float total = 0;
			float average;		// Variable type is float because we cannot assume that the value returned will whole number
			
			for(int i = 0; i < nums.length; i++)
				total = total + nums[i]; 
			
			average = total/nums.length;
			
			System.out.printf("\nAverage: %.2f",average);		// Print the output in a certain format
		}
		
	
		
}//end class

wow.....thank you so much Katana for taking the time to help me. when i try to compile that code im getting 2 errors related to the Scanner in (cannot find symbol)

Should i have a file called 'scanner in' in the directory where i have my file saved? I have not used scanner in yet and have only used easyIn at this stage. Is scanner in totally something different than easyIn?

once again thank you for taking the time to help me out, really really appreciated

Cindy

That's no problem Cindy. My bad, I left out one essential line of the code. It imports the Scanner class which allows you to accept input . This is it:

import java.util.Scanner;

And it that doesn't work use:

import java.util.*;

Guys i still cant get the middle value, i have declared a method called middle. lets say i store 5 numbers in the array which are 11,22,33,44,55. i wish the program to return 33 as the middle value. Or if i store 6 numbers in the array i would like it to return the middle value + 1. Heres what i have so far, i think i am quite close

public static double middle(int[] m)
int middle = m.length/2;  
if (m.length/2 == 1)

{
// Odd number of elements -- return the middle one.
		 return m[middle];
}

else
{
// Even number -- return average of middle two
// Must cast the numbers to double before dividing.

System.out.println("The middle value  is " + middle);
			
return (m[middle-1] + m[middle])/ 2.0;

}



}//end method

Can someone please advise me where i cam going wrong?

thanks in advance guys

Cindy

Guys i still cant get the middle value, i have declared a method called middle. lets say i store 5 numbers in the array which are 11,22,33,44,55. i wish the program to return 33 as the middle value. Or if i store 6 numbers in the array i would like it to return the middle value + 1. Heres what i have so far, i think i am quite close

public static double middle(int[] m)
int middle = m.length/2;  
if (m.length/2 == 1)

{
// Odd number of elements -- return the middle one.
		 return m[middle];
}

else
{
// Even number -- return average of middle two
// Must cast the numbers to double before dividing.

System.out.println("The middle value  is " + middle);
			
return (m[middle-1] + m[middle])/ 2.0;

}



}//end method

Can someone please advise me where i cam going wrong?

thanks in advance guys

Cindy

Hi there cindy, you were along the right lines but I think you just over complicated it. Its quite simple to do. Here's my code:

public static void middle(int[] m)			// Find the middle value of the array
	{
		int middle = m.length/2; 
		
	if (m.length % 2 == 1)						// Determines whether the total number's in array are odd or even
	 
	{
	
		System.out.println("\nOdd Numbered Array\nMiddle value: " + m[middle]);	
	}
	 
	else
	{
	
		System.out.println("Even Numbered Array\nMiddle value: " + m[middle] );
	 
	}
	 
	 
	 
	}//end method

Btw how where you calling your method? I apologise for not pointing out all your mistakes but im in a rush. Just remember K.I.S.S - keep it simple stupid! lol I mean no disrespect :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.