Unformatted text preview:

Objectives Understand the one dimensional data structure Declare and initialize a onedimensional array Refer to individual elements of an array Understand the use of arrays to store a list of values Pass arrays to functions 1 One Dimensional Arrays int n 5 32 27 64 18 95 32 27 64 18 95 n 0 n 1 n 2 n 3 n 4 n 0 n sizeof n would equal 20 2 One Dimensional Arrays int x 5 32 27 32 27 32 27 0 0 0 3 One Dimensional Arrays int y 5 0 0 0 0 0 0 4 One Dimensional Arrays int z 5 z 0 5 5 z 4 3 5 3 z 5 10 5 3 10 Overlaying code data 5 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl 6 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl 7 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl 9 cout x 1 endl cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl 8 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl 9 11 cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl 9 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl 9 11 13 cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl 10 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 11 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 0 12 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl 9 cout x 1 endl cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 0 13 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl 9 cout x 1 endl cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 0 1 14 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl 9 11 cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 1 15 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl 9 11 cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 12 16 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl 9 11 13 cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 2 17 One Dimensional Arrays int x 3 9 11 13 cout x 0 endl cout x 1 endl 9 11 13 cout x 2 endl int x 3 9 11 13 for int i 0 i 3 i cout x i endl Value of i 2 3 18 Write a for loop to initialize all the elements in a 20 integer array values to zero Begin for loop Initialize counter Set limit for counter Increment counter int values 20 for int i 0 i 20 i Initialize element in array values values i 0 19 Write a program that asks the user for the number of hours worked by 6 employees Save these hours in an array const int NUM 6 int hours NUM cout enter hours for six employees Initialize counter for int i 0 i NUM i Set limit for counter cin Hours i Increment counter Begin for loop Read hours into next element of array 20 Write a program that initializes an array to the days of each month and displays those days const int NUM 12 int Days NUM 31 28 31 30 31 30 31 31 Initialize array Begin for loop 30 31 30 31 Initialize counter for int i 0 i 12 i Set limit for counter cout Month Increment counter i 1 Display each next has element of array Days i 21 One Dimensional Arrays OR char n 7 Warren char Name1 W A R R E N 0 W n 0 A R R E N 0 n 1 n 2 n 3 n 4 n 5 n 6 n 0 n sizeof n would equal 7 22 One Dimensional Arrays char n 7 WARREN ONE WAY TO DISPLAY WARREN cout n ANOTHER WAY TO DISPLAY WARREN for int x 0 x 7 x cout n x 23 Passing Arrays to Functions void ShowValue int int main const int SIZE 8 int Collection SIZE 5 10 15 20 25 30 35 40 for int Cycle 0 Cycle SIZE Cycle ShowValue Collection Cycle pass by return 0 value void ShowValue int Num cout Num 24 Passing Arrays to Functions void ShowValues int int int main const int SIZE 8 int Collection SIZE 5 10 15 20 25 30 35 40 ShowValues Collection SIZE pass by return 0 reference void ShowValues int Nums int size for int Index 0 Index size Index cout Nums Index 25 Passing Arrays to Functions int main const int SIZE 7 int Set SIZE 1 2 3 4 5 6 7 DoubleArray Set SIZE return 0 void DoubleArray int Nums int Size for int Index 0 Index Size Index Nums Index 2 Set 1 2 3 4 5 6 7 26 Passing Arrays to Functions int main const int SIZE 7 int Set 7 1 2 3 4 5 6 7 DoubleArray Set SIZE Set 0 Set return 0 void DoubleArray int Nums int Size for int Index 0 Index Size Index Nums Index 2 Set 12 2 4 63 8 4 10 5 12 6 14 7 27 Passing Arrays to Functions int main int a 5 0 1 2 3 4 modifyElement a 3 return 0 pass 1 element by value void modifyElement int e e 5 cout e a 0 3 e No change in array in main 1 2 3 4 28 Passing Arrays to Functions int main int a …


View Full Document

SMU CSE 2341 - One Dimensional Arrays

Loading Unlocking...
Login

Join to view One Dimensional Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view One Dimensional Arrays and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?