Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ICA13
- {
- class Program
- {
- static void Main(string[] args)
- {
- do
- {
- int size = 0; //input
- int[] array;
- //reset
- Console.Clear();
- //title
- Console.WriteLine("\t\t\tDale Chen-Song Assignment 13");
- //get value for size of array
- GetValue("Enter the size of the array: ", 4, 10, out size);
- //make arrays from user input
- array = MakeArray(size);
- //display the contents of the integer array
- Show(array);
- //display the contents of the integer array in reverse
- ShowReverse(array);
- //display the average of the integer array
- Console.WriteLine($"\nThe average is {Average(array)}");
- //display the largest value of the integer array
- //display message asking user if they want to do it again
- Console.Write("Run again? y/n: ");
- //if user inputs yes, loop
- } while (Console.ReadLine().ToLower() == "y");
- }
- //***********************************************************************************
- //Method: static private int GetValue(string prompt, int min, int max)
- //Purpose: get integer value from user with error and range checking
- //Parameters: string prompt- prompt to display to the user
- // int min- the minimum value of range for value
- // int max- the maximum value of range for value
- // out int input- input out the valid integer
- //***********************************************************************************
- static private void GetValue(string prompt, int min, int max, out int input)
- {
- bool success = false; //success flag, true if success
- //repeat until input is right
- do
- {
- //user input value for integer
- Console.Write(prompt);
- success = int.TryParse(Console.ReadLine(), out input);
- //check if the value is valid
- if (!success)
- {
- //display error message if invalid number, prompt user to try again
- Console.WriteLine("An invalid number was entered, please try again.");
- }
- //check if the value is within the range
- else if (input < min || input > max)
- {
- //display error message if out of range, prompt user to try again
- Console.WriteLine("An out of range number was entered, please try again.");
- }
- }//continue looping until the input is correct
- while (!success || input < min || input > max);
- }
- static int[] MakeArray(int input)
- {
- Random generator = new Random(); //random number generator
- //create the array of integers to the specified size from user
- int[] integers = new int[input];
- //randomly generate numbers into the integer array
- for (int i = 0; i < integers.Length; ++i)
- {
- integers[i] = generator.Next(0, 100);
- }
- //return the integers to the main program
- return integers;
- }
- static private void Show(int[] array)
- {
- Console.WriteLine("\nThe array contents... ");
- //displays the contents of the integer array
- for (int i = 0; i < array.Length; ++i)
- {
- Console.WriteLine($"array[{i}] = {array[i]}");
- }
- }
- static private void ShowReverse(int[] array)
- {
- Console.WriteLine("\nThe array in reverse...");
- // displays the contents of the integer array
- for (int i = array.Length-1; i > 0; --i)
- {
- Console.WriteLine($"array[{i}] = {array[i]}");
- }
- }
- static private double Average(int[] array)
- {
- double average = 0; //average of the values in the array
- int total = 0; //sum of the values in the array
- //sum up the total value within the array
- foreach (int value in array)
- {
- total += value;
- }
- //calculating the average of the array
- average = (double)total / array.Length;
- //returns the average to the main console
- return average;
- }
- static private void Largest()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement