Presentation is loading. Please wait.

Presentation is loading. Please wait.

STARTING OUT WITH STARTING OUT WITH Class 2 2 Review of course requirements: Homework due Thursday at beginning of class-- no lates accepted Programs.

Similar presentations


Presentation on theme: "STARTING OUT WITH STARTING OUT WITH Class 2 2 Review of course requirements: Homework due Thursday at beginning of class-- no lates accepted Programs."— Presentation transcript:

1

2 STARTING OUT WITH STARTING OUT WITH Class 2

3 2 Review of course requirements: Homework due Thursday at beginning of class-- no lates accepted Programs are due Monday at 6pm of the week listed in your outline (program1.cpp, program1.out, program1.ppt and program1.doc or.txt); see webct for dates

4 3 Review of course requirements: Homework due Thursday at beginning of class-- no lates accepted Programs are due Monday at 6pm of the week listed in your outline (program1.cpp, program1.out, program1.ppt and program1.txt) ; see webct for dates

5 4 Review of course requirements: Incentives for coming to class lecture on Tuesdays and Thursdays –Unannounced quizzes –Unannounced fun logical quizzes and bonus opportunities given for getting back points lost on turned in homework assignment for that week –Interactive Class notes for classes 4-28 distributed

6 5 Review of course requirements: Must enroll in a lab Lab sessions are mandatory Pop Quizzes cannot be made up Homework cannot be made up Programs can be turned in late for 10 points off with special permission Make-up exams only given if documented excuse has been submitted BEFORE exam

7 6 Communication Office hours via email: –Please identify yourself Webct address –http://courses.smu.edu (use your 8 digit smu id for your userid and password)

8 7 Synchronization of passwords You can change your password in webct by going to your homepage and clicking on “change password” in the upper right- hand corner of the window. In Unix, the command to change your password is passwd

9 8 Submitting assignments Print out instructions in webct under the link “instructions for submitting assignments” Before sending in first programming assignment, practice with your program1 folder Those folders will have to be reset by your instructor next week so don’t put your real program their yet. Caution: once a file has been placed in your folder and submitted, the link on the folder goes away and you cannot resubmit so DO NOT try to submit your 4 files at different times. Be sure what you submit is what you WANT to submit because you cannot correct it and resubmit.

10 9 Using Unix and the vi editor You must connect to a unix server Be sure to do the Unix tutorial located on webct ( lessons 1,2,3 and 13) by the end of next week’s lab. You can begin the vi tutorial if you have time Next week, there will be a unix and vi quiz icon on webct. You can take those quizzes any time you feel ready. They must be taken by the end of the lab of the third week. The icon will go away then. The quizzes are password protected; when you click on the quiz, you will need to ask your ta to type in the password for you.

11 10 Using Unix and the vi editor Additional tutorials available at: http://www.math.utah.edu/lab/unix/unix- tutorial.html AND http://heather.cs.ucdavis.edu/~matloff/UnixAndC/ Unix/UnixBareMn.html

12 11 Using Unix and the vi editor The command in unix to compile a c++ program: at the unix % prompt type g++ program1.cpp -o program1.out This is assuming that you have used an editor to create the program1.cpp file which has been saved in your unix home directory. To execute the program, at the unix prompt, type program1.out

13 12 Objectives Understand the one-dimensional data structure. Declare and initialize a one- dimensional array Refer to individual elements of an array. Understand the use of arrays to store a list of values. Pass arrays to functions.

14 13 int n[5] One-Dimensional Arrays = {32, n[0]n[1]n[2]n[3]n[4] 3227641895 27,64,18,95}; & n[0] n sizeof n would equal 20

15 14 One-Dimensional Arrays int x[5] = {32, 27}; 3227??? 3227000

16 15 One-Dimensional Arrays int y[5] = {0}; 00000

17 16 One-Dimensional Arrays int z[5]; ????? z[0] = 5; 5???? z[4] = 3; 5???3 z[5] = 10; 5???3 10 Overlaying code / data

18 17 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; One-Dimensional Arrays

19 18 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; One-Dimensional Arrays

20 19 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; 9 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; One-Dimensional Arrays

21 20 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; 9 11 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; One-Dimensional Arrays

22 21 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; 9 11 13 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; One-Dimensional Arrays

23 22 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; One-Dimensional Arrays

24 23 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i 0 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; One-Dimensional Arrays

25 24 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i 0 9 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; One-Dimensional Arrays

26 25 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i 0 9 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; 1 One-Dimensional Arrays

27 26 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i 1 9 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; 11 One-Dimensional Arrays

28 27 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i 1 9 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; 2 11 One-Dimensional Arrays

29 28 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i 2 9 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; 11 13 One-Dimensional Arrays

30 29 int x[3] = {9,11,13}; cout << x[0] << endl; cout << x[1] << endl; cout << x[2] << endl; Value of i 2 9 int x[3] = {9,11,13}; for (int i = 0; i<3; i++) cout << x[i] << endl; 3 11 13 One-Dimensional Arrays

31 30 Write a for loop to initialize all the elements in a 20 integer array ‘values’ to zero. int values[20]; Begin for loop Initialize counter Set limit for counter Increment counter Initialize element in array ‘values’ for ( ) values[i]=0; int i=0;i<20;i++

32 31 Write a program that asks the user for the number of hours worked by 6 employees. Save these hours in an array. float Hours[6]; Begin for loop Initialize counter Set limit for counter Increment counter Read hours into next element of array for ( ) cin >> Hours[i]; int i=0;i<6;i++ cout << “enter hours for” << “six employees”; PG 402 ~ Pgrm. 7-2

33 32 Write a program that initializes an array to the days of each month and displays those days. int Days[12]={31,28,31,30, 31,30,31,31, 30,31,30,31); Initialize array Initialize counter Set limit for counter Increment counter Display each next element of array for (int i=0; i < 12; i++) { cout << “Month” << << (i + 1) << <<Days[i];} Begin for loop PG 408 ~ Pgrm. 7-6

34 33 char n[7]=“Warren”; n[0]n[1]n[2]n[3]n[4] WARRE & n[0] n sizeof n would equal 7 n[5]n[6] char Name1[ ]={‘W’,’A’,’R’,’R’,’E’,’N’,’\0’}; // OR N\0 One-Dimensional Arrays char Name1[ ]={‘W’,’A’,’R’,’R’,’E’,’N’,’\0’};

35 34 char n[7]=“WARREN”; cout << n; for (int x=0; x < 7; x++) cout << n[x]; ONE WAY TO DISPLAY WARREN ANOTHER WAY TO DISPLAY WARREN PG 412 ~ Fig. 7-11 One-Dimensional Arrays

36 35 Passing Arrays to Functions void ShowValue(int); int main() {int Collection[8] = {5,10,15,20,25,30,35,40}; } for (int Cycle =0; Cycle < 8; Cycle++) ShowValue (Collection[Cycle]); // pass by return 0; } // value void ShowValue(int Num) { cout << Num << “ “;} PG 425 ~ Pgrm. 7-13

37 36 void ShowValues(int [ ]); main() {int Collection[8] = {5,10,15,20,25,30,35,40}; } ShowValues (Collection); // pass by return 0; } // reference void ShowValues(int Nums[ ]) { for (int Index = 0; Index < 8; Index++) cout << Nums[Index] << “ “;} PG 427 ~ Pgrm. 7-14 Passing Arrays to Functions

38 37 void DoubleArray(int Nums[ ], int Size) { for ( int Index = 0; Index < Size; Index++) Nums[Index] * = 2; } Set int main() { int Set[12]={1,2,3,4,5,6,7,8,9,10,11,12}; DoubleArray(Set,12); return 0; } 123456789101112 Passing Arrays to Functions PG 429 ~ Pgrm. 7-16

39 38 void DoubleArray(int Nums[ ], int Size) { for ( int Index = 0; Index < Size; Index++ Nums[Index] *=2; } Set int main() { int Set[12]={1,2,3,4,5,6,7,8,9,10,11,12}; DoubleArray(Set,12); rturn 0; } 12345678910111224681012141618202224 Passing Arrays to Functions Set &Set[0] PG 429 ~ Pgrm. 7-16

40 39 void modifyElement (int e) { e *= 5; cout << e;} a 01234 3 e No change in array in main int main () { int a[5]={0,1,2,3,4}; modifyElement(a[3]); return 0; } //pass 1 element by value Passing Arrays to Functions

41 40 void modifyElement (int e) { e *= 5; cout << e;} a 01234 e No change in array in main int main () { int a[5]={0,1,2,3,4}; modifyElement(a[3]); return 0; } //pass 1 element by value 15 Passing Arrays to Functions

42 41 PARALLEL ARRAYS int main() { int Hours[5]; float PayRate[5]; cout << “Enter hours worked” << “by 5 employess and their” << “hourly rates.\n”; PG 422 ~ Pgrm. 7-12

43 42 cout << “Enter hourly pay rate for employee #” cout << (Index +1); cin >> PayRate[Index]; } // end of for loop Hours PayRate 5.5010.358.509.005.75 for (int Index =0; Index < 5; Index++) { cout << “hours worked by employee #” << (Index + 1); cin >> Hours[Index]; 4035503843 PG 42 ~ Pgrm. 7-12

44 43 PARALLEL ARRAYS for (Index =0; Index < 5; Index++) { float GrossPay = Hours[Index] * PayRate[Index]; cout << “Employee #” << (Index + 1); cout << GrossPay << endl;} Hours 4035503843 5.5010.358.509.005.75 PayRate PG 422 ~ Pgrm. 7-12

45 44 PARALLEL ARRAYS EXERCISE #7.13 P. 425 What is the output of the following program? void main(void) { int Time[5] = {1,2,3,4,5}, Speed[5] = {18,4,27,52,100}, Dist[5]; for(int Count =0; Count < 5; Count++) Dist[Count] = Time[Count] * Speed[Count]; for (Count = 0; Count < 5; Count ++) cout << Time[Count] << Speed[Count] << Dist[Count];

46 45 Copying arrays The wrong way: int array1[4] = {2,4,6,9}; int array2[4]; array2 = array1; // syntax error because // left side is a constant pointer // created when the array was declared

47 46 Copying arrays The right way, one element at a time: int array1[4] = {2,4,6,9}; int array2[4]; for (int I = 0; I < 4; I++) array2[I] = array1[I];

48 47 PARALLEL ARRAYS IN CLASS EXERCISE Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the poll in another integer array.

49 48 int responses[40]; int frequency[11]; // do not use position 0 If 10 students respond with a 2 5 students respond with a 10 20 respond with a 7 3 students respond with a 6 2 students respond with a 1 frequency array 2100 000320005

50 49 int main() { int responses[40]; int frequency[11]; cout << “enter 40 student responses” << “ from 1 to 10” <<endl; for (int j=0; j< 40; j++) cin >> responses[j]; for ( j =0; j < 40; j++) ++ frequency[responses[j]];} for ( int k=1; k < 11; k++) cout << “rating #” << k << “had” << frequency[k] << “responses”;

51 STARTING OUT WITH STARTING OUT WITH Conclusion of Class 2


Download ppt "STARTING OUT WITH STARTING OUT WITH Class 2 2 Review of course requirements: Homework due Thursday at beginning of class-- no lates accepted Programs."

Similar presentations


Ads by Google