120 likes | 255 Views
Chapter 8. ARRAYS. Arrays!. An array is string of characters or line of numbers The salaries of the employees The names of the employees Students’ score a test Height and weight of team members Average score of class on test. Declare an array. Declare a regular variable: int age
E N D
Chapter 8 ARRAYS
Arrays! • An array is string of characters or line of numbers • The salaries of the employees • The names of the employees • Students’ score a test • Height and weight of team members • Average score of class on test
Declare an array • Declare a regular variable: int age • Declare an array named age: int [] age • Declare array for salary: • decimal [ ] salary • Declare array for name: • string [ ] name
Fill an array • name[0]=“Smith” • (put Smith in 1st slot of name) • salary[1]=24500.32 • (2nd slot of salary=24500.32) • height[0]=60.2 • empname[2]=“John Jones” • What if there 10,000 names…
Arrays and loops • Easy way to fill an array: • Ask names of Basketball team players: for (plyr=0;plyr<10;plyr++) { Console.WriteLine(“Enter Player’s name:”) team[plyr]=Console.ReadLine(); }
Fill Salary array decimal [] salary; int nn; string txt; salary=new decimal [25]; // get salary for each employee for (nn=0; nn<25; nn++) { Console.Write(“Enter salary: ”); salary[nn]=Convert.ToDecimal(Console.ReadLine()); }// end for
DANGERS • Addressing beyond dimension: • int nn[6]; then nn[7]=55 … Addressing what was not declared
What to do? • If you get “exception” at run-time: • Use F11 and step through the program until you find the error • Insert “console.writeline(s)” inside the loop, or just before the error occurs
//declare section string buf; int numemp; int looper; // loopcounter string [] name; // array declared decimal [] salary; decimal [] tax; decimal [] deductions; decimal [] net; decimal [] totals;
// set array size(s) Console.Write("Enter No. of employees "); numemp=Convert.ToInt32(Console.ReadLine()); // you could use a variable to set size! name=new string[numemp]; salary=new decimal[numemp]; tax=new decimal[numemp]; deductions=new decimal[numemp]; net=new decimal[numemp]; totals=new decimal[10]; // for totals
// Input section for (looper=0; looper<numemp; looper++) { Console.Write("Enter name: "); name[looper]=Console.ReadLine(); Console.Write("Enter Salary: "); salary[looper]=Convert.ToDecimal(Console.ReadLine()); totals[1]=totals[1]+salary[looper]; Console.Write("Enter tax: "); tax[looper]=Convert.ToDecimal(Console.ReadLine()); totals[2]=totals[2]+tax[looper]; Console.Write("Enter Deductions: "); deductions[looper]=Convert.ToDecimal(Console.ReadLine()); totals[3]=totals[3]+deductions[looper]; net[looper]=salary[looper] - tax[looper]+deductions[looper]); totals[4]=totals[4]+net[looper]; }// end for
// end of loop, now display header Console.WriteLine("Name\t\tGross\t\tTax\tDed\tNet"); // loop to display remaining detail lines for (looper=0;looper<numemp;looper++) { Console.WriteLine("{0}\t\t{1:N2}\t{2:N2}\t{3:N2}\t{4:N2}", name[looper],salary[looper],tax[looper],deductions[looper],net[looper]; }// end for // display total Console.WriteLine("Total\t\t{0:N2}\t{1:N2}\t{2:N2}\t{3:N2}", totals[1], totals[2], totals[3],totals[4]); Console.ReadLine(); }// end Main }// end Class