Lec 07 Multi-Dimensional Arrays Intro To Structures
Lec 07 Multi-Dimensional Arrays Intro To Structures
Lec 07 Multi-Dimensional Arrays Intro To Structures
Review
int myarray1[] = {2, 1, 15, 20}; int iVar = 3; int myarray2[] = {25, 30, 40, 70}; What is sizeof(myarray1)? Which of the expressions are valid/safe and what do they do?
myarray1[1]++; myarray1 = myarray2; myarray2[0] = iVar; myarray2[iVar] = myarray2[myarray1[0]]; scanf(%d, &(myarray1[3])); int myarray3[iVar];
Review
int myarray1[] = {2, 1, 15, 20}; int iVar = 3; int myarray2[] = {25, 30, 40, 70}; What is sizeof(myarray1)? sizeof(int)*4 = 4*4 = 16 Which of the expressions are valid/safe and what do they do?
myarray1[1]++; myarray1 = myarray2; /* Invalid */ myarray2[0] = iVar; myarray2[iVar] = myarray2[myarray1[0]]; scanf(%d, &(myarray1[3])); int myarray3[iVar]; /* Invalid */
Review
How do you declare a function parameter that is an array of int?
void func( <what should be here?>, int iSize) { while (iSize > 0) { /* myarray is an integer array */ printf("%d\n", myarray[--iSize]); } }
Review
int myarray1[] = {2, 1, 15, 20, -1};
Sample 07-01
Implement the following functions int get_sum(int numbers[ ], int count)
Returns the sum of the numbers in the array
Demonstrate modularity
Sample 07-02
Demo: functions can modify array contents void myreset(int numbers[ ], int count)
set all elements in numbers to 0
Review
char myarray[10] = {a, b, c, '\0'};
What is sizeof(myarray)? What is myarray[1]? What is strlen(myarray)? How do I access c? Is the code below valid?
char tmp[10]=abc;
Review
char myarray[10] = {a, b, c, '\0'};
What is sizeof(myarray)? 10 What is myarray[1]? 'b' What is strlen(myarray)? 3 How do I access c? myarray[2] Is the code below valid? Yes
char tmp[10]=abc;
Sample 07-03
Implement using arrays: int mystrlen(char input[ ]);
Same as strlen(); Displays the number of characters in the string
Demo:
fgets( ) char arrays with '\0' inserted in the middle
Sample 07-04
Implement using arrays: void mytoupper(char input[ ]);
Capitalizes all characters in the string
Sample 07-05
Implement using arrays: int mystrcmp(char str1[ ], char str2 [ ])
Same as strcmp; compares two strings
Implement a program that asks the user to guess a secret code (e.g. eee11)
Give hints like higher, lower
Multidimensional Arrays
Multidimensional Array
Matrix List of words like: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
EEE11
Data type
Column size
EEE11
EEE11
aArray2d[1][3]
EEE11
Recall that a string is a 1D-array of char 2D-array of char can be thought of as a collection of several 1D-array of char Example: {'d', 'e', 'f', '\0' } }; /* alpha[0] ==> {'a', 'b', 'c', '\0'} ==> abc alpha[1] ==> {'d', 'e', 'f', '\0'} ==> def */
EEE11
Using Lists
char alpha[2][4] = { {'a', 'b', 'c', '\0' }, {'d', 'e', 'f', '\0' } }; int i; for (i=0; i<2; i++) printf("%s\n", alpha[i]);
EEE11
Using lists
char days[7][10] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; int i; for (i=0; i<7; i++) puts(days[i]); /* printf("%s\n", days[i]); */
EEE11
Using lists
char temp[ ] = testing; char sample[7][10]; int i; for (i=0; i<7; i++) strcpy(sample[i], temp);
EEE11
Sample 07-06
Use a 2D array (list) to implement a simple number to word converter (0-9 only)
EEE11
Sample 07-07
EEE11
3D Arrays
3d Array:
int aArray3d[4][2][5];
A collection of four (4), two(2) 5-integer array (40 integers in all)
2x5 array
Accessing 3D arrays
int aArray3d[4][2][5];
0
1
0 1 2 3 4 0 1
aArray3d[2][1][3]
N-dimensional Array
EEE11
Intro to Structures
a variable (of basic data types) an ordered set of the same data type (array) student (name, ID #) Credit card
EEE11
31
Structure
Collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling
EEE11
32
EEE11
33
struct student_info {
Variables or Members
EEE11
Braces as delimiter
34
struct { };
Semicolon
Braces
EEE11 35
IMPORTANT
Declaration of a structure does NOT create a variable It is just a new data type
EEE11
36
EEE11
37
EEE11
38
Dot operator
/*We can also modify each member*/ snprintf(bayani.name, sizeof(bayani.name), "%s", "Andres Bonifacio"); /*OR*/ strncpy(bayani.name, "Andres Bonifacio", sizeof(bayani.name) - 1);
EEE11
39
sizeof a Structure
EEE11
41
EEE11
42
Sample 07-07
Name Votes
Get 9 votes, asking users to pick which candidate Display winner at the end
EEE11
43
References
Largely from actual coding and sheer curiosity If you are looking for a hard copy:
The C Pogramming Language by K&R Problem Solving and Program Design in C by Hanly Open source codes
EEE11
44