# Problems with foreach and for to find average number

I’m trying to finish my Lab for the game programming course but I’m stuck with the last problem.
I need to solve few problems using simple console application.
This is lab text:

Problem 1 – Collect user data
Start up the IDE and create a new console application project named Lab14. Save the
project in a reasonable location on the computer.
While the user has not entered -1, prompt for and get a positive integer (including 0)
from the user. Valid user inputs are therefore -1, 0, and all positive integers. Use a
second while loop (nested inside the first while loop) to validate the user input. Add valid
positive integers to a list.
You can assume the user will only enter values that the int.Parse method can parse.
Make sure you don’t add -1 to the list of data.
Problem 2 – Find the maximum value in the data
Use a for or foreach loop to find the maximum value in the list. You are NOT allowed to
keep track of the maximum value in the while loops from Problem 1, you have to do it
separately here. Print out the maximum value after you find it.
Problem 3 – Calculate the average of the values
Use a for or foreach loop to calculate the average of the values in the list. Print out the
average.

And this is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lab14
{///

/// class to find the maximum and average values
///

class Program
{///
///
///

/// command-line args
static void Main(string[] args)
{

``````        // create values list
List<int> valuesList = new List<int>();

// get users input
Console.Write("Enter the values (Enter -1 when you finish): ");

while (inputValue != -1)
{
while (inputValue < -1)
{

Console.Write("WRONG INPUT! Enter the positive values (Enter -1 when you finish): ");
}

Console.Write("Enter the values (Enter -1 when you finish): ");

}
Console.WriteLine();

// find the maximum
for (int i = valuesList.Max(); i < valuesList.Max()+1; i++)
{

Console.WriteLine("MAX Value: " + i);

}
Console.WriteLine();

// find the average

double average = valuesList.Average();
Console.WriteLine("Average Value: " + average);

}
}
``````

}

So, I’m really stuck with the Problem 3 - find the average of all values. It’s easy to use List.Average, but I need to use for or foreach. I tried everything with both and it didn’t work well.

Why do u do a for on values.MAX() ? There can be only one max.

Pb 3 : you create a var before the loop and use this to sum the valid values. After this you can do the average computation without the Average() method.

1 Like
``````double average = 0;

//needs to be double/float or we divide int/int which yields an int instead of double
double length = valuesList.Count;

foreach(int i in valuesList)
{
average += i / length;
}
``````
1 Like

I don’t think these “here’s my homework and I can’t solve it, please help” kind of questions should be on this forum. This question is in no way related to MonoGame…

Thanks! That exactly what I need.

I study game development course for beginners using MomoGame and we use it 90% of times, but this week mentor gave us lab with console application to understand for, foreach and while loops. By the way, nobody check this homework, I’m doing it for myself for better understanding and extra experience.
But you 100% right - this kind of questions it’s better to ask at the C# forum, I’ll do it next time.

Pb with dividing in the loop is the increasing loss of precision. It is better to make it after.