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
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
1 Post
using arrays
Feb 03, 2019 12:28 PM|rowlandsfc|LINK
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.
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
Contributor
3710 Points
1431 Posts
Re: using arrays
Feb 04, 2019 08:21 AM|Yuki Tao|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
10 Points
4 Posts
Re: using arrays
Feb 05, 2019 06:52 PM|Nova Jacob|LINK
Since you are doing division like below
The average variable should be double or float data type.
The calculation should be