Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Part-1 Armen Keshishian.

Similar presentations


Presentation on theme: "Arrays Part-1 Armen Keshishian."— Presentation transcript:

1 Arrays Part-1 Armen Keshishian

2 Concept An array allows you to store and work with multiple values of the same data type. An array works like a variable that can store a group of values, all of the same type. int days[6]; //size declarator must be constant integer greater than zero Element 0 Element 1 Element 2 Element 3 Element 4 Element 5

3 Accessing Array Elements
The individual elements of an array are assigned unique subscripts, these subscripts are used to access the elements. Subscript numbering in C++ always starts at zero. The subscript of the last element in an array is one less than the total number of elements in the array. For accessing an element of an array you should use its subscript then you can work with that as a individual variable with the same array’s data type. days[0] = 20; The expression days[0] is pronounced “days sub zero”

4 Example Write a program that asks user to insert number of working hours per day for a week and shows the total number of hours for that week.

5 #include "stdafx.h" #include <iostream> using namespace std; int main() { int days[7]; cout << "Please enter number of hours per day." << endl; cout << "Monday: "; cin >> days[0]; cout << "Tuesday: "; cin >> days[1]; cout << "Wednesday: "; cin >> days[2]; cout << "Thursday: "; cin >> days[3]; cout << "Friday: "; cin >> days[4]; cout << "Saturday: "; cin >> days[5]; cout << "Sunday: "; cin >> days[6]; //Display the values in the array cout << endl; cout << " Your schedule is " << endl; cout << "Monday: " << days[0] << endl; cout << "Tuesday: " << days[1] << endl; cout << "Wednesday: " << days[2] << endl; cout << "Thursday: " << days[3] << endl; cout << "Friday: " << days[4] << endl; cout << "Saturday: " << days[5] << endl; cout << "Sunday: " << days[6] << endl; //Calculate total number of hours int sum = 0; for (int i = 0; i < 7; i++) sum += days[i]; } cout << "Total number of hours: " << sum << endl; system("pause"); return 0;

6 No bounds checking in C++
C++ does not prevent you from overwriting an array’s bounds. This means that you can write programs with subscripts that go beyond the boundaries of a particular array.

7 Off-by-one error This is an easy and common mistake to make on working with arrays, because array subscripts start at 0 rather than 1. int numbers[10]; for (int i = 1; i <= 10; i++) { numbers[i] = 0; }

8 Array initialization Arrays may be initialized when they are defined.
int days[7] = { 8,7,6,5,4,6,0};

9 Number of days in each month
Write a program that shows number of days in each month by asking users to insert number of month. int main() { int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 31, 31 }; cout << "Please insert number of month: "; int month = 0; cin >> month; cout << "Number of days for month " << month << " is " << days[month] << endl; system("pause"); return 0; }

10 Examples Write a program that creates an array with 10 elements and asks user to fill that array out, then it shoud: 1- Find the max 2- Show the total 3- copy all elements into another array. 4- copy all elements into another array in reverse 5- compare with another array with the same size. HOMEWORK

11 Parallel Arrays Consider a company with 5 employees. Each employee has total hours and pay rate. Write a program for this company that creates two arrays one for hours and one for pay rates. Then asks user to insert hour and rate for each employee. Then calculates the gross pay for each employee.

12 Arrays and Functions To pass an array as an argument to a function, pass the same of the array. It is possible to pass a single value of an array to a function Write a program to use a function to show all numbers in an array

13 Display values of an array
#include <iostream> using namespace std; void displayValues(int numbers[], int size); int main(int argc, const char * argv[]) { int nums[5] = {5, 6, 4, 6, 20}; displayValues(nums, 5); return 0; } void displayValues(int numbers[], int size) { for (int i =0 ; i < size; i++) { cout << numbers[i] << " " ;

14 Homework arraysGeneralFunctions
Write a program that asks user to insert 10 numbers (integer) into an array and shows Min Average All negative numbers All Even numbers You should use functions for each item. Hint (functions prototypes): Min -> int GetMin(int numbers[], int size); Average -> float GetAverage(int numbers[], int size); Negative -> void ShowNegatives(int numbers[], int size); Even -> void ShowEvenNumbers(int numbers[], int size);

15 Homework employeePayChecks
Consider a company with 5 employees, each employee has total number of hours per month, pay rate, and number of dependents. Write a program that asks total hours for each employee per month and the pay rate. Then calculates the gross amount as well as net amount. To calculate net amount first you should deduct $300 for each dependent then uses the chart below to get percentage and deduct it from gross amount then add all deducted amount for dependents back to net salary. Gross amount Tax percentage Amount < 60,000 15% 60,000 <= Amount < 80,000 18% 80,000 <= Amount < 99,000 22% 99,000 <= Amount 25% Example: if an employee’s income is 61,000 and he/she has 4 dependents. First you should deduct $300 for each dependent which will be (300 * 4 = $1,200). 61000 – 1200 = Since 59,800 is less than 60,000 you should use 15% of which will be $8970. This amount is the tax that employee should pay off of $59,800 that means 59800 – 8970 = 50830 Now you should add dependents deductions back to net pay = 52030 This value is the final net.


Download ppt "Arrays Part-1 Armen Keshishian."

Similar presentations


Ads by Google