3 Replies - 777 Views - Last Post: 24 November 2011 - 03:40 AM Rate Topic: -----

#1 Beginner Sarah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
about C# Codes

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();
            }


help me please !

Is This A Good Question/Topic? 0
  • +

Replies To: Minimum Value in An Array

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • 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();


Was This Post Helpful? 1
  • +
  • -

#3 Beginner Sarah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 .
Was This Post Helpful? 0
  • +
  • -

#4 Beginner Sarah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1