This is the second in class example. I will post the first one soon
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace parallelArrays
{
class Program
{
///
/// This program shows how to use
/// parallel arrays and methods.
/// Parallel arrays are arrays in which
/// related values are kept on identical
/// indexes. [0] =[0], [1]=[1] etc.
/// the methods are broken into
/// CreateArrays which declares the arrays
/// Populate arrays which loops through the
/// arrays and lets the user enter values
/// CalculateArea multiplies the parallel values
/// (values with the same index in the two arrays)
/// Each area is passed to the display method
///
///
static void Main(string[] args)
{
//make the program new (load into memory)
Program p = new Program();
p.CreateArrays(); //call the CreateArrays program
p.EndProgram(); //call end program
}
private void CreateArrays()
{
//declare the arrays
int[] height = new int[5];
int[] width = new int[5];
//call PopulateArrays and pass the two arrays to it
PopulateArrays(height, width);
}
private void PopulateArrays(int[] height, int[] width)
{
//loop through the arrays and prompt
//the user to provide values
for(int i = 0; i < height.Length; i++)
{
Console.WriteLine("Please enter Height");
height[i] = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter Width");
width[i] = int.Parse(Console.ReadLine());
}
//call CalculateAreas and pass the arrays
CalculateAreas(height, width);
}
private void CalculateAreas(int[] height, int[]width)
{
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.Clear();
//this loop multiplys the parallel values
//from the arrays to get the area
//then passes each area to the Display method()
for(int i = 0; i<height.Length;i++)
{
int area=height[i] * width[i];
DisplayArea(area);
}
}
private void DisplayArea(int area)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("the area is : " + area.ToString());
}
private void EndProgram()
{
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}
No comments:
Post a Comment