number 7, the text “Lucky array” should be printed to the output and calculate the average of all values in the array. Here is my code
import java.util.*;
public class Program2
{
public static void main (String []arg)
{
Random generator=new Random();
// this create a empty array named arr
int [] arr = new int[10];
String myLuckyNumber = "7";
int sum;
double medel;
//a) this fills the array with value generated by the Random
for(int i=0;i <arr.length;i++)
arr[i]=generator.nextInt(100);
// B)/> this prints the array
for(int i=0;i <arr.length;i++)
System.out.println(arr[i]);
// c) calculate the sum of all values in the array
sum = 0;
for (int i=0; i<arr.length; i++)
{
sum += arr[i];
}
System.out.println("\nDen Totala summan är: " + sum);
// d) calculate the average of all values in the array
medel = 0;
for (int i=0; i<arr.length; i++)
{
medel += arr[i];
medel /= sum;
}
System.out.println("\nMedelvärdet är: " + medel/sum);
// e) search for the lucky number 7. If the array contains the
// number 7, the text “Lucky array” should be printed
// to the output.
System.out.println("My lucky number is: " + myLuckyNumber);
}
}
This post has been edited by no2pencil: 24 February 2010 - 08:20 PM
Reason for edit:: Added code tags

New Topic/Question
Reply




MultiQuote







|