Lucky number!

Help me with that!

Page 1 of 1

6 Replies - 2475 Views - Last Post: 25 February 2010 - 04:54 PM Rate Topic: -----

#1 rasmus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-February 10

Lucky number!

Posted 24 February 2010 - 06:54 AM

Hello! I've been stuck on how to do: search for the lucky number 7. If the array contains the
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


Is This A Good Question/Topic? 0
  • +

Replies To: Lucky number!

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,277
  • Joined: 27-December 08

Re: Lucky number!

Posted 24 February 2010 - 07:07 AM

Please, :code:

To search, just iterate through your array using a for loop. Like so:
for(int i = 0; i < arrayName.length; i++){
   if(arrayName[i] == 7){
      ..do stuff..
   }
}


Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Lucky number!

Posted 24 February 2010 - 07:52 PM

// 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;
}


Why looping through the array again ? What about
medel = sum / arr.length;
Was This Post Helpful? 0
  • +
  • -

#4 Guest_rasmus07*


Reputation:

Re: Lucky number!

Posted 25 February 2010 - 06:12 AM

I donīt now pbl. It was the only time I got a mean value. What am I doing wrong?
I do not know how to calculate the average of an array
Was This Post Helpful? 0

#5 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 180
  • Joined: 10-January 10

Re: Lucky number!

Posted 25 February 2010 - 06:49 AM

He gave you the code.
int sum=0;
for(int i = 0;i<arr.length;i++)
sum+=arr[i];

int ave = sum/arr.length;

System.out.println("Your average is "+ave);



- Zach
Was This Post Helpful? 0
  • +
  • -

#6 Guest_rasmus07*


Reputation:

Re: Lucky number!

Posted 25 February 2010 - 08:01 AM

ok, I did not see that he had given me a code. I saw it now.

But thanks anyway!
Was This Post Helpful? 0

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Lucky number!

Posted 25 February 2010 - 04:54 PM

View Postrasmus07, on 25 February 2010 - 07:12 AM, said:

I donīt now pbl. It was the only time I got a mean value. What am I doing wrong?
I do not know how to calculate the average of an array

The average of an array is the sum of all its elements divided by the number of elements
And you have just calculated the sum a few lines before
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1