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 ICA15
- {
- class Program
- {
- static void Main(string[] args)
- {
- do
- {
- int size = 0; //user input for size of array
- double[] marks; //mark array
- string[] strings; //string array
- //reset console
- Console.Clear();
- //title
- Console.WriteLine("\t\t\tDale Chen-Song Assignment 15\n");
- //get value for size of array
- GetValue("Enter the number of students in the class: ", 4, 10, out size);
- //get array of marks
- marks = MakeRecords(size);
- //get array of strings
- strings = MakeRecords(size, 5, 12);
- //display the contents of the arrays
- Show(marks, strings);
- //display average of the marks
- Average(marks, strings);
- //display those who have failed
- Fails(marks, strings);
- //display message asking user if they want to do it again
- Console.Write("\nRun again? y/n: ");
- //if user inputs yes, loop
- } while (Console.ReadLine().ToLower() == "y");
- }
- //***********************************************************************************
- //Method: static private void GetValue(string prompt, int min, int max, out int input)
- //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 private double[] MakeRecords(int input)
- {
- Random generator = new Random(); //random number generator
- //create the array of doubles to the specified size from user
- double[] array = new double[input];
- //randomly generate doubles numbers into the doubles array
- for (int i = 0; i < array.Length; ++i)
- {
- array[i] = (generator.NextDouble() * 100);
- }
- //return the double array to the main program
- return array;
- }
- static private string[] MakeRecords(int input, int min, int max)
- {
- Random generator = new Random(); //random number generator
- string name =""; //random string holder
- //create the array of strings to the specified size from user
- string[] array = new string[input];
- //randomly generate strings into the string array
- for (int i = 0; i < array.Length; ++i)
- {
- //reset string for name
- name = "";
- //
- for (int j = 0; j < generator.Next(5, 12); j++)
- {
- //if it is the first letter, make it a random uppercase
- if (j == 0)
- {
- //generate a random uppercase letter
- char upperCase = (char)(generator.Next(65, 90));
- //add that character to the string
- name += upperCase.ToString();
- }
- //otherwise make it a random lowercase letter
- else
- {
- //generate a random lowercase letter
- char letter = (char)(generator.Next(97, 122));
- //add that character to the string
- name += letter.ToString();
- }
- }
- //have the generated random string in the array
- array[i] = name;
- }
- //return the string array to the main program
- return array;
- }
- //***********************************************************************************
- //Method: static private void Show(double[] arrayMarks, string[] arrayStrings)
- //Purpose: display the contents of both the marks array and strings array
- //Parameters: double[] arrayMarks- the randomly generated double array to display
- // string[] arrayStrings- randomly generated string array to display
- //***********************************************************************************
- static private void Show(double[] arrayMarks, string[] arrayStrings)
- {
- //displays the contents of both arrays
- Console.WriteLine($"\n{"Name",15} {"Mark",-15}");
- Console.WriteLine($"{"----",15} {"---",-15}");
- for (int i = 0; i < arrayMarks.Length; ++i)
- {
- Console.WriteLine($"{arrayStrings[i], 15:F1} : {arrayMarks[i], -20:F1}");
- }
- }
- //***********************************************************************************
- //Method: static private double Average(int[] array)
- //Purpose: caculate the average of the integers in the array
- //Parameters: int[] array- randomnly generated integer array
- //Returns: double average- the calculated average of the array
- //***********************************************************************************
- static private void Average(double[] arrayMarks, string[] arrayStrings)
- {
- 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 arrayMarks)
- {
- total += value;
- }
- //calculating the average of the array
- average = (double)total / arrayMarks.Length;
- //display average of the marks
- Console.WriteLine($"\nThe average of the marks is {average:F1} percent.");
- }
- //***********************************************************************************
- //Method: static private double Average(int[] array)
- //Purpose: caculate the average of the integers in the array
- //Parameters: int[] array- randomnly generated integer array
- //Returns: double average- the calculated average of the array
- //***********************************************************************************
- static private void Fails(double[] arrayMarks, string[] arrayStrings)
- {
- //display the list of failing students
- Console.WriteLine("\nHere is a list of failing students...\n");
- Console.WriteLine($"{"Name",15} {"Mark",-15}");
- Console.WriteLine($"{"----",15} {"---",-15}");
- for (int i = 0; i < arrayMarks.Length; ++i)
- {
- if (arrayMarks[i] <50.0)
- {
- Console.WriteLine($"{arrayStrings[i],15:F1} : {arrayMarks[i],-20:F1}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement