# Lucky number!

Page 1 of 1

## 6 Replies - 3834 Views - Last Post: 25 February 2010 - 04:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=158025&amp;s=1d0fd43fb0c9f7eb64615a56fd283a5b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rasmus

Reputation: 0
• 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

• Games, Graphs, and Auctions

Reputation: 12316
• Posts: 45,416
• Joined: 27-December 08

## Re: Lucky number!

Posted 24 February 2010 - 07:07 AM

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..
}
}

```

### #3 pbl

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

Reputation: 8378
• Posts: 31,956
• 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;

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

### #5 Simple_Condolences

Reputation: 11
• 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;

```

- Zach

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!

### #7 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Lucky number!

Posted 25 February 2010 - 04:54 PM

rasmus07, 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