@angie_wales wrote:
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;
using System.Threading.Tasks;namespace Lab14
{///
/// class to find the maximum and average values
///class Program {/// <summary> /// /// </summary> /// <param name="args">command-line args</param> 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): "); int inputValue = int.Parse(Console.ReadLine()); while (inputValue != -1) { while (inputValue < -1) { Console.Write("WRONG INPUT! Enter the positive values (Enter -1 when you finish): "); inputValue = int.Parse(Console.ReadLine()); } valuesList.Add(inputValue); Console.Write("Enter the values (Enter -1 when you finish): "); inputValue = int.Parse(Console.ReadLine()); } 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.
Please, Help!
Posts: 3
Participants: 3