0

Write a program, when given an array of integers, it returns the biggest integer.

Can you please tell me what's wrong in this program??

import java.util.Scanner;

public class Arrayint {

	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int i,index,max;
		int x;
		
		
		int[] list=new int[x];
		max=list[0];
		
		System.out.println("Enter length of array: ");
		x=input.nextInt();
		
		System.out.println ("Enter values for Array:");	
		for (i=0;i<x;i++)
			list[i]=input.nextInt();
		
		for (i=1;i<list.length;i++)
		{
			if (max < list[i])
			{
				max=list[i];
			}
				
		}
		index=max;
		System.out.println("largest: "+index);
	}
}

public class Arrayint {

	public static void main(String[] args)
	{
		int list[]={3,7,6,1,9,96,2};
		int i,index,max;
		
		max=list[0];
		for (i=1;i<list.length;i++)
		{
			if (max < list[i])
			{
				max=list[i];
			}
				
		}
		index=max;
		System.out.println("largest: "+index);
	}
}
2
Contributors
14
Replies
15
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

Is this a quiz?
If you have a problem with your program please explain exactly what the problem is - if there is an error message post the whole message, if the output is wrong explain what it should be and what it is.

0

it does not give any error.simply underline these two lines.(highlight in red)

int[] list=new int[x];

public class Arrayint {

Edited by techyworld: n/a

0
import java.util.Scanner;

public class Arrayint 
{

	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int i,index,max;
		int x=0;
		
		
		int[] list=new int[x];
		max=list[0];
		
		System.out.println("Enter length of array: ");
		x=input.nextInt();
		
		System.out.println ("Enter values for Array:");	
		for (i=0;i<x;i++)
			list[i]=input.nextInt();
		
		for (i=1;i<list.length;i++)
		{
			if (max < list[i])
			{
				max=list[i];
			}
				
		}
		index=max;
		System.out.println("largest: "+index);
	}
}

now am having this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Arrayint.main(Arrayint.java:15)

0

huh.. i wanted to initialise the first element in the array to the variable max.

Edited by techyworld: n/a

0

Can you help me with this question:
Input two sets of arrays A and B(integer). Use a function to calculate the sum of the square of corresponding values of A and B. These values should then be displayed.
Again, its not giving me any error but instead underlining the one in red!

import java.util.Scanner;

public class Array {
	
	public static void main(String [] args)
	{
		
		Scanner input = new Scanner(System.in);
		int x,i;
		
		System.out.println("Enter length of array: ");
		x=input.nextInt();
		
		int[] A=new int [x];
		int[] B=new int [x];
		int[] C=new int [x];
		
		
		
		System.out.println ("Enter values for Array A:");	
		for (i=0;i<x;i++)
			A[i]=input.nextInt();
		System.out.println("Enter values for Array B:");
		for (i=0;i<x;i++)
			B[i]=input.nextInt();
		
		C=arraySum(A,B);
		System.out.print("Sum of corresponding values:");
		for (i=0;i<5;i++)
			System.out.print(C[i] + " ");
		
	}
	
	public static int[] arraySum(int[] X,int[] Y)
	{
		int i=0;
		int[] sum1= [U]new int [];[/U]
		for (i=0; i<X.length; i++)
		{
			sum1[i]=(X[i]*X[i]) + (Y[i]*Y[i]);
		}
		
		return sum1;
		
	}

}

Edited by techyworld: n/a

0

1. If your original question is solved then please mark the thread as "solved" and start a new one for a new question.
2. I don't know what development tools you are using, but there will be a proper compiler error message somewhere explaining why the underlined code is wrong. It's going to save us all a lot of time if you find out where it is.
3. When you create an array you must specify its size. So new int[] is saying "please create an array but I won't tell you how big" and, of course, that doesn't work. If it's intended to be the same size as the two arrays that are passed as parameters then you can use the length of those arrays to specify the size of your new array...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.