locked
using arrays RRS feed

  • Question

  • User1135614484 posted

    im first year college studying coding as part of my course and i have to write up the game hammurabi, im trying to use arrays to store data baasedon results of calculations from data entered by the user and implenebted on a click of a button.

                int[] starved = new int[10];
    
                starved[0] = died;
                starved[1] = died;
                starved[2] = died;
                starved[3] = died;
                starved[4] = died;
                starved[5] = died;
                starved[6] = died;
                starved[7] = died;
                starved[8] = died;
                starved[9] = died;
    
                if (year == 11)
                {
                    int average, sum = 0;
                    for (int i = 0; i < 10; i++)
                    {
    
                        sum = sum + starved[i];
    
                    }
                    average = sum / 10;
    
                    label11.Text = average + " on average died peer year";
                }
                

    died the is the amount of people who have died for what ever reason in that specific year, what imtrying to do is run the game for 10 years and each year save the number of people died and then at the end of year 10/start of year 11 calculate the avergae amount of people who died and display it in a label

    not currently working atm so any advice would be appreciated

    Sunday, February 3, 2019 12:28 PM

All replies

  • User1520731567 posted

    Hi rowlandsfc,

    According to your descriptions,I make a demo,you could refer to it:

     public static void Main()
            {
    
                int[] starved= new int[50];
                int average, sum = 0;
                for (int i = 0; i < 50; i++)
                {
                    Console.WriteLine("Please enter the number:");
                    starved[i] =Convert.ToInt32(Console.ReadLine());
                    sum = sum + starved[i];
                    if (i % 10 == 0&&i>0)//when year11 or year21 ...
                    {
                        average = sum / i;
                        Console.WriteLine("This is year {0},average is {1}",i+1,average);
                    }
    
                }
           
                Console.ReadKey();
    
            }

    How it works:

    Best Regards.

    Yuki Tao

    Monday, February 4, 2019 8:21 AM
  • User-1885736663 posted

    Since you are doing division like below

    average = sum / 10;

    The average variable should be double or float data type.

    The calculation should be

    average = (double) sum / 10;

    Tuesday, February 5, 2019 6:52 PM