1 / 12

Chapter 8

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

maire
Download Presentation

Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8 ARRAYS

  2. 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

  3. 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

  4. 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…

  5. 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(); }

  6. 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

  7. DANGERS • Addressing beyond dimension: • int nn[6]; then nn[7]=55 … Addressing what was not declared

  8. 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

  9. //declare section string buf; int numemp; int looper; // loopcounter string [] name; // array declared decimal [] salary; decimal [] tax; decimal [] deductions; decimal [] net; decimal [] totals;

  10. // 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

  11. // 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

  12. // 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

More Related