# Minimum Value in An Array

Page 1 of 1

## 3 Replies - 777 Views - Last Post: 24 November 2011 - 03:40 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=257005&amp;s=88a8883a6845c933f1c8b45b6f898b76&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Beginner Sarah

Reputation: 0
• Posts: 3
• Joined: 24-November 11

# Minimum Value in An Array

Posted 24 November 2011 - 12:53 AM

Hello all

i am a beginner in programming and i have some Questions

Now i am trying to Find the Minimum Value an an Array
and i used this Code and it doesnt work !

```   int i;

double min= x[0];

for(i=1;i<x.Length;i++)
{
if (x[i] < min)
{
min= x[i];
textBox10.Text = min.ToString();

}
}
return;
{

```

and i used this Code for the Average and doesn't work
```int average = 0;
int i;
double sum = 0;
int temp;

for (i = 0; i < x.Length; i++)
sum += x[i];
{
average=(sum / x[i]);
textBox8.Text = average.ToString();
}
```

Is This A Good Question/Topic? 0

## Replies To: Minimum Value in An Array

### #2 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

## Re: Minimum Value in An Array

Posted 24 November 2011 - 02:20 AM

In the first code your problem could be that the last brace points to wrong direction, or that your smallest number is the first number in the array (x[0]). If it's the first number, then your code will never go inside the if statement and it never gets printed out.

Here is a fix:
```            int i;

double min = x[0];

for (i = 1; i < x.Length; i++)
{
if (x[i] < min)
{
min = x[i];
}
}
// do printing after for-loop.
textBox10.Text = min.ToString();

```

In second code again your issue could be the braces. Or that your formula for calculating average is wrong. Average = sum / count. There is also a need for casting double to int when calculating average. This can be avoided by declaring average as double instead of int. Here is a working sample:
```            int average = 0;
int i;
double sum = 0;

for (i = 0; i < x.Length; i++)
{
sum += x[i];
}
average = (int)(sum / x.Length);
textBox8.Text = average.ToString();

```

### #3 Beginner Sarah

Reputation: 0
• Posts: 3
• Joined: 24-November 11

## Re: Minimum Value in An Array

Posted 24 November 2011 - 03:14 AM

i want to thank you so much for your help
the average code is fixed!
thank you!

but the minmum code still give the answer is zero ! even there is no
zero in tha array .

### #4 Beginner Sarah

Reputation: 0
• Posts: 3
• Joined: 24-November 11

## Re: Minimum Value in An Array

Posted 24 November 2011 - 03:40 AM

I solved the problem
Thanks ALOT !<3 <3 <3