Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.

Similar presentations


Presentation on theme: "Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about."— Presentation transcript:

1 Arrays

2 Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about next semester?

3 Arrays Declare a list of variables – all with the same type double scores[14]; name[size];

4 Example //declare the array 90.5 7387 scores:

5 Example //declare the array double scores[3]; //put 90.5 in the first box scores:

6 Example //declare the array double scores[3]; //put 90.5 in the first box scores[0] = 90.5; 90.5scores:

7 Example //declare the array double scores[3]; //put 90.5 in the first box scores[0] = 90.5; scores[1] = 73; scores[3] = 87; 90.5 7387 scores:

8 Alternative double scores[3] = {90.5, 73, 87}; Initializes elements of the array to values given when array is created

9 Subscripts Subscript describes which box of the array you are dealing with Array of size N has subscripts –0, 1, 2, … (n-1) –array of size 3 – 0, 1, 2 –subscript added to base address of array Subscript can be a variable or expression which produces an integer –scores[i]; –scores[i+5]; If subscript specified does not exist – runtime error

10 Example scores[1]; scores[3]; //assume i = -2; scores[i+2]; sum = scores[1] + scores[2]; sum = scores[1] + scores[i*8]; scores[2] = scores[1] + 6; 90.5 7387 scores:

11 Accessing Array Elements Print all elements of the array scores –Use only one printf statement

12 Accessing Array Elements Print all elements of the array scores –Use only one printf statement #include #define MAX_SCORES 3 int main(void) { int i; double scores[MAX_SCORES] = {90.5, 72, 87}; for(i = 0; i < MAX_SCORES; i++) { printf("Score %d: %lf\n", i, scores[i]); } return(0); }

13 Passing Array Elements Write a function to add two elements of an array

14 Passing Array Elements Write a function to add two elements of an array –How is the function called? double add(double num1, double num2) { return (num1 + num2); }

15 Passing Array Elements Write a function to add two elements of an array –How is the function called? add(scores[0], scores[1]); … double add(double num1, double num2) { return (num1 + num2); }

16 Passing Array Elements Write a function to swap two elements of an array

17 Passing Array Elements Write a function to swap two elements of an array –How do you call swap? void swap(double *num1, double *num2) { double tmp = *num1; *num1 = *num2; *num2 = tmp; }

18 Passing Array Elements Write a function to swap two elements of an array –How do you call swap? swap(&scores[0], &scores[1]); … void swap(double *num1, double *num2) { double tmp = *num1; *num1 = *num2; *num2 = tmp; }

19 Passing Arrays Would like to pass an entire array into a function –Sum all elements, prompt user for values Array name is treated as a pointer (to first element of array)

20 Example add_one(scores, MAX_SCORES); void add_one(double scores_to_add[], int numscores) { int i; for(i = 0; i < MAX_SCORES; i++) { scores_to_add[i] = scores_to_add[i] + 1; }

21 Misc Parallel Arrays Partially filled arrays


Download ppt "Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about."

Similar presentations


Ads by Google