using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArraysAndLoops
{
class Program
{
/*This program will have three methods
* one to create an array
* one to populate it
* and one to display it
*/
static void Main(string[] args)
{
Program p = new Program();
string exit = "y";
while (exit != "n" && exit !="N")
{
p.CreateArray();
Console.WriteLine("Do you want to continue y/n");
exit = Console.ReadLine();
}
p.PauseIt();
}
//this method
//creates an array
private void CreateArray()
{
Console.WriteLine("How many payments do you want to enter?");
int number = int.Parse(Console.ReadLine());
double[] payments= new double[number];
FillArray(payments, number);
//double[] pmts = new double[] { 12.95, 34, 21.4, 43 };
//payments[0] = 255;
//payments[1] = 34.5;
//payments[2] = 44.5;
//payments[3] = 34;
//payments[4] = 34.5;
}
private void FillArray(double[] payArray, int size)
{
//for loop
for (int i=0; i < size; i++)
{
Console.WriteLine("Enter a payment");
payArray[i] = double.Parse(Console.ReadLine());
}//end for
DisplayArray(payArray, size); //call DisplayArrayMethod
}//end fill array
private void DisplayArray(double[] pmt, int size)
{
Console.WriteLine("***********************");
int x = 0;
while (x < size)
{
Console.WriteLine(pmt[x]);
x++;
/*
* += (number += 10) same as (number= number + 10);
-= minus (number -=10) same as (number=number-10)
-- decrement subtract one each time
*= (number *=10) same as (number=number*10)
%=(number %=10) same as (number=number%10)
/=(number /=10) same as (number=number/10)
*/
}//end while
Sum(pmt, size);
} //end display
private void Sum(double[] payment, int size)
{
double total = 0;
for (int i = 0; i < size; i++)
{
total += payment[i];
//weight = grades[i] * credits[i];
}
double average = total / size;
Console.WriteLine("the total is {0}", total);
Console.WriteLine("the Average is {0}",average);
}
private void PauseIt()
{
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}//end PauseIt
}//end class
}
Monday, October 17, 2011
Loops and Arrays
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment