Complex Data Types Arrays Structs Topic 7 Plan






![Arrays Example: int arr[10] = {12, 49, -2, 26, 5, 17, -6, 84, 72, Arrays Example: int arr[10] = {12, 49, -2, 26, 5, 17, -6, 84, 72,](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-7.jpg)
![Array declaration <type> <name>[size]; <type> <name>[ ] = {initial-values}; – Example: int numbers[10] = Array declaration <type> <name>[size]; <type> <name>[ ] = {initial-values}; – Example: int numbers[10] =](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-8.jpg)

![Accessing elements <name>[<index>] // access <name>[<index>] = <value>; // modify – Example: int numbers[10] Accessing elements <name>[<index>] // access <name>[<index>] = <value>; // modify – Example: int numbers[10]](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-10.jpg)
![Arrays and Loops Example: int a[10], i; for (i = 0; i < 10 Arrays and Loops Example: int a[10], i; for (i = 0; i < 10](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-11.jpg)









![Reading strings char name[10]; printf("Enter your name: "); scanf("%9 s", name); • Don't exceed Reading strings char name[10]; printf("Enter your name: "); scanf("%9 s", name); • Don't exceed](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-21.jpg)



![String Copy char s[10] = "help"; char *foo = "War of the worlds"; strncpy(s, String Copy char s[10] = "help"; char *foo = "War of the worlds"; strncpy(s,](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-25.jpg)



![Useful Functions (stdlib. h) Function double atof(const char str[]) int atoi(const char str[]) double Useful Functions (stdlib. h) Function double atof(const char str[]) int atoi(const char str[]) double](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-29.jpg)
![Example char str[] = "3. 14 This is pi"; char *rest. Of. String; double Example char str[] = "3. 14 This is pi"; char *rest. Of. String; double](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-30.jpg)


![Exercise int my. Atoi(char str[]) { int result = 0; for(int i = 0; Exercise int my. Atoi(char str[]) { int result = 0; for(int i = 0;](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-33.jpg)

![Input on command line int main(int argc, char* argv[]) {…} • argc: # of Input on command line int main(int argc, char* argv[]) {…} • argc: # of](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-35.jpg)
![Example int main(int argc, char *argv[]) { for(int i = 0; i < argc; Example int main(int argc, char *argv[]) { for(int i = 0; i < argc;](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-36.jpg)
![Command Line Args int main(int argc, char *argv[]) { if(argc < 2) printf("Need an Command Line Args int main(int argc, char *argv[]) { if(argc < 2) printf("Need an](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-37.jpg)





![Structures struct UTCourse { char course. Name[50]; int course. Number; int sec. ID; }; Structures struct UTCourse { char course. Name[50]; int course. Number; int sec. ID; };](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-43.jpg)

![Designated Initializers (C 99) typedef struct name { char first[20]; char last[20]; } full. Designated Initializers (C 99) typedef struct name { char first[20]; char last[20]; } full.](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-45.jpg)



![Example #include<stdio. h> #include<string. h> typedef struct { char first[20]; char last[20]; } full. Example #include<stdio. h> #include<string. h> typedef struct { char first[20]; char last[20]; } full.](https://slidetodoc.com/presentation_image_h/2ae2b2fb32a62230d5ddf7b4803fad09/image-49.jpg)
- Slides: 49
Complex Data Types: Arrays & Structs Topic 7 Plan for the Day: Array Basics Strings (aka character arrays) Structures
Arrays
Array Overview • • • ordered collection of items stored in contiguous memory all items, called elements, are of same type array cannot be resized indexing starts at 0 – offset from beginning of array name of array is pointer (memory address) of 1 st array element Declaration syntax: <elt-type> <array-name>[size]; • sets aside size*sizeof(elt-type) bytes Example: int grades[80]; Example: int nums[5] = {1, 2, 3, 4, 5};
Can we solve this problem? • Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44. 6 4 days were above average.
Why the problem is hard • We need each input value twice: – to compute the average (a cumulative sum) – to count how many were above average • We could read each value into a variable. . . but we: – don't know how many days are needed until the program runs – don't know how many variables to declare • We need a way to declare many variables in one step.
Why the problem is hard • We need each input value twice: – to compute the average (a cumulative sum) – to count how many were above average • We could read each value into a variable. . . but we: – don't know how many days are needed until the program runs – don't know how many variables to declare • We need a way to declare many variables in one step. • Solution: an array
Arrays Example: int arr[10] = {12, 49, -2, 26, 5, 17, -6, 84, 72, 3}; index 0 1 value 12 49 element 0 2 3 4 5 6 -2 26 5 17 -6 element 4 7 8 84 72 9 3 element 9
Array declaration <type> <name>[size]; <type> <name>[ ] = {initial-values}; – Example: int numbers[10] = {0}; index 0 1 2 3 4 5 6 7 8 9 value 0 0 0 0 0
Initializing an Array • An array initializer is a list of values enclosed in braces: int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; • If initializer too short, remaining elements are set to 0: int a[10] = {0}; • If an initializer is present, the length of the array may be omitted, and is determined by the compiler: int a[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; • Can declare these to be unchangeable if needed const int a[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Accessing elements <name>[<index>] // access <name>[<index>] = <value>; // modify – Example: int numbers[10] = {0}; numbers[0] = 27; numbers[3] = -6; printf("%dn", numbers[0]); if (numbers[3] < 0) { printf("Element 3 is negative. "); } index 0 1 2 3 4 5 6 7 value 27 0 0 0 -6 0 0 0 8 9 0 0
Arrays and Loops Example: int a[10], i; for (i = 0; i < 10 ; i++) {a[i] = 10 - i; } Warning: Array bounds in C are not checked; when a subscript goes out of bounds, the result is unpredictable. int a[10]; for (int i = 1; i <= 10; i++) a[i] = 2*i; What's the sizeof (a) ? What's the sizeof (a) / sizeof(a[0]) ? // wrong!
Weather question • Use an array to solve the weather problem: How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44. 6 4 days were above average.
Weather Answer #include<stdio. h> // Reads temperatures from the user, computes average and # days above average int main() { int days; printf("How many days' temperatures? "); scanf(" %d", &days); int temps[days]; // array to store days' temps int sum = 0; // Read and store each day's temperature for(int i = 0; i < days; i++) { printf("Day %d's high temp: ", i+1); scanf(" %d", &temps[i]); sum += temps[i]; } double average = (double) sum/days; // see if each day is above average int count = 0; for(int i = 0; i < days; i++) { if(temps[i] > average) count++; } // report results printf("Average temp = %. 1 fn", average); printf("%d days above averagen", count); }
"Array mystery" problem • traversal: An examination of each element of an array. • What element values are stored in the following array? int a[] = {1, 7, 5, 6, 4, 11}; for (int i = 0; i < sizeof(a)/sizeof(a[0])-1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } } index 0 1 2 3 4 value 1 7 5 6 4 5 6 14 11
"Array mystery" problem • traversal: An examination of each element of an array. • What element values are stored in the following array? int a[] = {1, 7, 5, 6, 4, 11}; for (int i = 0; i < sizeof(a)/sizeof(a[0])-1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } } index 0 1 value 1 7 2 3 10 12 4 8 5 6 14 22
Array Arguments • Often must pass length of array as well as array • Array argument: what's actually passed is pointer to (memory address) array int foo(int arr[]) { // sizeof won't tell us # of elts in arr parameter } • Exercise: Write a function that takes an int array and the length of the array, and returns the sum of its elements. int sum. Array(int a[], int len) {. . .
Array Limitations • You cannot resize an array • You cannot compare arrays with == or != – char s[10], t[10]; – if(s == t) {. . . } // s and t are memory addresses • You cannot put array name on left side of assignment – char s[10]; s = "abc"; // NO • You cannot use sizeof to determine length of array parameter
Strings
Character Arrays • Strings are represented as arrays of characters • The end of the string is specified with '