1 / 21

Arrays

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 next semester?. Arrays. Declare a list of variables – all with the same type double scores[14]; <data type> name[size];. Example.

hantonio
Download Presentation

Arrays

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. 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]; • <data type> name[size];

  4. Example //declare the array scores: 90.5 73 87

  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; scores: 90.5

  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; scores: 90.5 73 87

  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; scores: 90.5 73 87

  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 <stdio.h> #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

More Related