Mark Hesser
Nov 3, 2017
Intro to P&L
Programming Problems
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Programming_Problems
{
class Program
{
static void Main(string[] args)
{
bool Quit = false;
//Declarations
bool[] program = new bool[8] { true, false, false, false, false, false, false, false };
bool programSelectError = false, endprogram = false;
int i, programSelect = 0; string programKey;
ConsoleKeyInfo kb;
while (!Quit)
{
while (program[0]) //Program Selection Menu, Enter #1-7 to enter a program, 9 to Quit
{
Console.Title = "Program Selection Menu"; Console.Clear();
Console.WriteLine("Please Choose from the Following Options:"); Console.WriteLine("");
Console.WriteLine("1. Sum 5 numbers"); Console.WriteLine("2. Sum many numbers");
Console.WriteLine("3. Average many numbers"); Console.WriteLine("4. Quarterback statistics");
Console.WriteLine("5. 7 Day Sale"); Console.WriteLine("6. Bank Interest Calulator");
Console.WriteLine("7. Rent Manager"); Console.WriteLine(""); Console.WriteLine("9. Quit");
Console.WriteLine("");
if (programSelectError)
{
Console.WriteLine("That is not a Valid Choice, Enter # 1-7."); programSelectError = false;
}
//Select Program
Console.Write("Enter the # of the program you want to run: ");
kb = Console.ReadKey(); //Listens for Key
programKey = kb.KeyChar.ToString();
Int32.TryParse(programKey, out programSelect);
switch (programSelect) //Program
{
case 1: program[0] = false; program[1] = true; break;
case 2: program[0] = false; program[2] = true; break;
case 3: program[0] = false; program[3] = true; break;
case 4: program[0] = false; program[4] = true; break;
case 5: program[0] = false; program[5] = true; break;
case 6: program[0] = false; program[6] = true; break;
case 7: program[0] = false; program[7] = true; break;
case 9: program[0] = false; Quit = true; break;
default: programSelectError = true; break;
}
}
while (program[1])
{
Console.Clear(); Console.Title = "Sum of 5 numbers";
Console.WriteLine("Enter 5 numbers and I will give you the sum"); Console.WriteLine("");
double[] number = new double[5]; double sum = 0;
for (i = 0; i < number.Length; i++)
{
Console.Write("Number {0}: ", i + 1);
Double.TryParse(Console.ReadLine(), out number[i]);
if (number[i] == 0)
{
Console.WriteLine("Invalid"); i–;
}
}
for (i = 0; i < number.Length; i++)
{
sum += number[i];
}
Console.WriteLine(""); Console.WriteLine("The sum is {0}", sum);
//Restart Program or Naw
Console.WriteLine(""); Console.Write("Do you wish to continue? (y/n): ");
program[1] = false; endprogram = true;
}
while (program[2])
{
Console.Clear(); Console.Title = "Sum of many numbers";
Console.WriteLine("Enter some numbers and I will give you the sum, enter 999 to end input"); Console.WriteLine("");
bool loop = true;
i = 0; double input, sum;
List<double> numbers = new List<double>(); //creates new list
while (loop) //data entry, garbage and zero are invalid, 999 quits
{
Console.Write("Number: ");
Double.TryParse(Console.ReadLine(), out input); //garbage data counts as zero
if (input == 0) //garage is invalid
{
Console.WriteLine("Zeros and Garbage Data will not be added");
}
else if (input == 999) //Quit
{
Console.WriteLine("Ending Input… 999 will not be added");
loop = false;
}
else //Add to list
{
numbers.Add(input);
}
}
sum = numbers.Sum();
Console.WriteLine(""); Console.WriteLine("The sum is {0}", sum);
//Restart Program or Naw
Console.WriteLine(""); Console.Write("Do you wish to continue? (y/n): ");
program[2] = false; endprogram = true;
}
while (program[3])
{
Console.Clear(); Console.Title = "Average and Sum of numbers";
Console.Write("Enter the count of numbers to follow: ");
Int32.TryParse(Console.ReadLine(), out int count);
Console.WriteLine("");
double[] number = new double[count]; double sum = 0, average;
for (i = 0; i < number.Length; i++)
{
Console.Write("Number {0}: ", i + 1);
Double.TryParse(Console.ReadLine(), out number[i]);
if (number[i] == 0)
{
Console.WriteLine("Invalid"); i–;
}
}
for (i = 0; i < number.Length; i++)
{
sum += number[i]; //Calculates Sum
}
average = sum / count; //Calculates Average
Console.WriteLine("The sum is {0} and the average is {1}", sum, average);
//Restart Program or Naw
Console.WriteLine(""); Console.Write("Would you like to start over? (y/n): ");
program[3] = false; endprogram = true;
}
while (program[4])
{
//Declarations
int numGames = 12, changeGame;
double passYardsSum = 0, passAttemptsSum = 0, passCompleteSum = 0;
double averageYards, completePercent;
bool changes = true;
//Begin
Console.Clear(); Console.Title = "Quarterback statistics";
Console.Write("How many Games: "); Int32.TryParse(Console.ReadLine(), out numGames); Console.WriteLine("");
Console.WriteLine("Enter the Statistics for Each Game"); Console.WriteLine("");
double[] passYards = new double[numGames];
double[] passAttempts = new double[numGames];
double[] passComplete = new double[numGames];
for (i = 0; i < numGames; i++)
{
Console.Write("Game {0} Passing Yards: ", i + 1);
Double.TryParse(Console.ReadLine(), out passYards[i]);
Console.Write("Game {0} Pass Attempts: ", i + 1);
Double.TryParse(Console.ReadLine(), out passAttempts[i]);
Console.Write("Game {0} Pass Complete: ", i + 1);
Double.TryParse(Console.ReadLine(), out passComplete[i]);
Console.WriteLine("");
}
while (changes)
{
Console.WriteLine("Was all the data entered Correctly? Press Enter (zero) to Calculate Results.");
Console.Write("To make a change, Enter the Game #: ");
Int32.TryParse(Console.ReadLine(), out changeGame);
if (changeGame > 0 && changeGame <= numGames)
{
Console.WriteLine("");
Console.Write("Game {0} Passing Yards: ", changeGame);
Double.TryParse(Console.ReadLine(), out passYards[changeGame – 1]);
Console.Write("Game {0} Pass Attempts: ", changeGame);
Double.TryParse(Console.ReadLine(), out passAttempts[changeGame – 1]);
Console.Write("Game {0} Pass Complete: ", changeGame);
Double.TryParse(Console.ReadLine(), out passComplete[changeGame – 1]);
Console.WriteLine("");
}
else
{
changes = false;
}
}
//Calculations
for (i = 0; i < numGames; i++)
{
passYardsSum += passYards[i]; //Calculates Sum
passAttemptsSum += passAttempts[i]; //Calculates Sum
passCompleteSum += passComplete[i]; //Calculates Sum
}
averageYards = passYardsSum / numGames;
completePercent = passCompleteSum / passAttemptsSum;
completePercent = completePercent * 100;
averageYards = Math.Round(averageYards, 2);
completePercent = Math.Round(completePercent, 2);
//Results
Console.WriteLine(""); Console.WriteLine("Quarterback Statistics");
Console.WriteLine("Average Yards per Game: {0}", averageYards);
Console.WriteLine("Completion Percentage: {0}", completePercent);
//Restart Program or Naw
Console.WriteLine(""); Console.Write("Would you like to enter new stats? (y/n): ");
program[4] = false; endprogram = true;
}
while (program[5])
{
//Declarations
double todaysPrice;
double[] salePrice = new double[7];
//Begin
Console.Clear(); Console.Title = "7 Day Sale";
Console.Write("Enter Todays Price: ");
Double.TryParse(Console.ReadLine(), out todaysPrice);
salePrice[0] = todaysPrice;
for (i = 1; i < salePrice.Length; i++)
{
salePrice[i] = salePrice[i-1] * .9;
}
Console.WriteLine("");
for (i = 1; i < salePrice.Length; i++)
{
Console.WriteLine("Day {0}: {1:C}", i + 1, salePrice[i]);
}
//Restart Program or Naw
Console.WriteLine(""); Console.Write("Would you like enter another Price? (y/n): ");
program[5] = false; endprogram = true;
}
while (program[6])
{
//Declarations
string firstName, lastName;
int accountNumber, years;
double accountBalance, interestRate, interestMultiplier;
//Input Settings
Console.Clear(); Console.Title = "Bank Interest Calculator";
Console.Write("Projections for how many years: "); Int32.TryParse(Console.ReadLine(), out years);
Console.Write("Interest Rate %: "); Double.TryParse(Console.ReadLine(), out interestRate);
interestMultiplier = interestRate / 100 + 1;
//Input Starting Values
Console.Clear();
Console.Write("Account Number: "); Int32.TryParse(Console.ReadLine(), out accountNumber);
Console.Write("First Name: "); firstName = Console.ReadLine();
Console.Write("Last Name: "); lastName = Console.ReadLine();
Console.Write("Account Balance: "); Double.TryParse(Console.ReadLine(), out accountBalance);
//Calculations
double[] projectedBalance = new double[years + 1];
projectedBalance[0] = accountBalance;
for (i = 1; i < projectedBalance.Length; i++)
{
projectedBalance[i] = projectedBalance[i – 1] * interestMultiplier;
}
Console.WriteLine("");
for (i = 1; i < projectedBalance.Length; i++)
{
Console.WriteLine("Year {0}: {1:C}", i, projectedBalance[i]);
}
//
//Restart Program or Naw
Console.WriteLine(""); Console.Write("Would you like project another Account? (y/n): ");
program[6] = false; endprogram = true;
}
while (program[7])
{
//Declarations
double rent;
int buildings = 20, apartments = 15, year, day, a, b;
string[] months = new string[12]
{ "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
//Year Input
Console.Clear(); Console.Title = "Rent Manager";
Console.WriteLine("Print Payment Coupons (Enter 0 or nothing for default)"); Console.WriteLine("");
Console.Write("Due Day (default 1): "); Int32.TryParse(Console.ReadLine(), out day); if (day == 0) { day = 1; }
Console.Write("Year (default 2017): "); Int32.TryParse(Console.ReadLine(), out year); if (year == 0) { year = 2017; }
Console.Write("Building (default all): "); Int32.TryParse(Console.ReadLine(), out b);
Console.Write("Apartment (default all): "); Int32.TryParse(Console.ReadLine(), out a);
Console.Write("Rent (default $800): "); Double.TryParse(Console.ReadLine(), out rent); if (rent == 0) { rent = 800; }
Console.Clear();
//Loops
if (b == 0 && a == 0)
{
for (b = 1; b <= buildings; b++)
{
for (a = 1; a <= apartments; a++)
{
for (int m = 0; m < months.Length; m++)
{
Console.WriteLine();
Console.WriteLine("Building {0}: Apartment {1}", b, a);
Console.WriteLine("Rent: {0:C}", rent);
Console.WriteLine("Due {0} {1}, {2}", months[m], day, year);
}
}
}
}
else if (b > 0 && a == 0)
{
for (a = 1; a <= apartments; a++)
{
for (int m = 0; m < months.Length; m++)
{
Console.WriteLine();
Console.WriteLine("Building {0}: Apartment {1}", b, a);
Console.WriteLine("Rent: {0:C}", rent);
Console.WriteLine("Due {0} {1}, {2}", months[m], day, year);
}
}
}
else if (b == 0 && a > 0)
{
for (b = 1; b <= apartments; b++)
{
for (int m = 0; m < months.Length; m++)
{
Console.WriteLine();
Console.WriteLine("Building {0}: Apartment {1}", b, a);
Console.WriteLine("Rent: {0:C}", rent);
Console.WriteLine("Due {0} {1}, {2}", months[m], day, year);
}
}
}
else
{
for (int m = 0; m < months.Length; m++)
{
Console.WriteLine();
Console.WriteLine("Building {0}: Apartment {1}", b, a);
Console.WriteLine("Rent: {0:C}", rent);
Console.WriteLine("Due {0} 21, {1}", months[m], year);
}
}
//Restart Program or Naw
Console.WriteLine(""); Console.Write("Would you like to print another apartment? (y/n): ");
program[7] = false; endprogram = true;
}
while (endprogram)
{
kb = Console.ReadKey(); //Listens for Key
if (kb.Key == ConsoleKey.N) // N Quits the Program
{
program[0] = true; /*Quits the program*/ endprogram = false;
}
else if (kb.Key == ConsoleKey.Y) // Y Restarts the Program
{
program[programSelect] = true; endprogram = false;
}
else //Still Quits
{
Console.WriteLine("");
Console.WriteLine("You didn’t hit Y or N."); Console.WriteLine("I’m going to assume you said No.");
Console.WriteLine("Press Any Key to Quit"); Console.ReadKey();
program[0] = true; /*Returns you to the Main Menu*/ endprogram = false;
}
}
}
}
}
}
[/code]