Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002.

Similar presentations


Presentation on theme: "Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002."— Presentation transcript:

1 Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002

2 Copying Arrays an array cannot be directly assigned to another array. int x[100], y[100]; x = y;/* ILLEGAL */ You must copy values one-by-one: for(i=0; i<100; i++) x[i] = y[i] ;

3 Perfect your skills at: using arrays in loops, esp. in for-loops. for(i=0; i<ARR_SIZE; i++){ scanf("%d", &arr[i]); printf("%d", &arr[i]); }

4 Array Initialization int myscores[3] = { 90, 95, 56 } ; int ttt[3][3] = { {0,1,0}, {2,0,0}, {1,0,2} }; int ttt[3][3] = {0,1,0,2,0,0,1,0,0} ; char myName[5] = { 'a', 'h', 'm', 'e', 't' };

5 options... Can omit size when initializers are specified. int myScores[ ] = { 90, 95, 56 }; when less initializers than the size, the rest filled with zeros. (?) int myScores[3] = {90,95}; /*same as: {90,95,0} */ int goals[6] = {1,2,0}; /*same as: {1,2,0,0,0,0} */

6 character arrays a character array may be initialized using a double-quoted string. char myName[ 6 ] = "ahmet"; /* why 6? */ char myName[6] = {'a','h','m','e','t','\0'};

7 errors... more initializers than size: int scores[3] = {90,100,50, 20}; accessing unallocated blocks: int scores[3] = {90,100,50};... scores[4] = 3;

8 Arrays as Arguments Write a function that takes in a float-array of size 10, and returns the average of these 10 numbers.

9 Arrays as arguments float avg( float x[10] ) { int i; float sum = 0; for(i=0; i < 10; i++) sum += x[i]; return sum/10; } void main(){ float arr[10];... a = avg( arr );...

10 Goal Write a function that takes a float-array of any size, and returns the average of the elements.

11 float avg( float x[], int size ) { int i; float sum = 0; for(i=0; i < size; i++) sum += x[i]; return sum/size; } void main(){ float arr[10];... a = avg( arr, 5);...

12 when...... array dimension is greater than 1, all but first dimension size must be specified. #define MAXCOLS 5 int PrintMatrix(int a[][MAXCOLS], int rows);

13 more on: char [] we'll call a null ('\0') terminated character array: a string. char mystr[16] = "hello"; is equivalent to: char mystr[16] = {'h', 'e', 'l', 'l', 'o','\0' };

14 char array initialization to initialize a character array, you can use a double-quoted string only when you are declaring the array. char lastName[32] = "sacan"; lastName = "ekmen"; /* INVALID */ make sure that array is long enough to hold the string. Leave one extra character for '\0' - null character. char name[6] = "ahmet";

15 Printing char arrays Like regular arrays, you may use a for- loop to print individual elements Or, use %s, so that printf takes care of it for you...: printf("%s", arr);

16 Goal Write a function that prints a character array, char-by-char.

17 void PrintCharArr(char str[ ], int maxSize) { int i; for(i=0; i<size; i++){ if(str[i] == '\0') break; printf("%c", arr[i]); }

18 Reading Input-Line into char-array Use the special function fgets, defined in stdio.h, to read a line of input. –The fgets() function reads characters from the stream into the array pointed to by s, until n-1 characters are read, or a newline character is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null character. fgets(str, MAX_SIZE, stdin);

19 Goal Read a line of input into an array and print the array character by character.

20 #define MAX_SIZE 256 void main(){ int i; char str[MAX_SIZE]; printf("enter string : "); fgets(str, MAX_SIZE, stdin); for(i=0; i<MAX_SIZE; i++){ if(str[i] == '\0') break; printf("[%c]", str[i]); } sample Run: enter string: trshx [t][r][s][h][x][ ]

21 Goal Write a function that takes a character- array as input, and its maximum size, and returns the length of the string contained in the array.

22 int GetStrLength(char str[ ], int maxLen){ int i; for(i=0; i<maxLen; i++){ if(str[i] == '\0'){ return i; } return 0; /* not a null-terminated array */ }

23 Back to Non-char arrays.

24 Goal Write a function that takes does matrix production. Function takes three matrices and their sizes, put the product of the first two into the third.

25 #define MAXCOLS void MatProd(float x[ ][MAXCOLS], int xR, xC, float y[ ][MAXCOLS], yC, float z[ ][MAXCOLS], int zA, zB) { int i,j,k; for(i=0; i < xR; i++){ for(j=0; j < xC; j++){ z[i][j] = 0; for(k=0; k < yC; k++) { z[i][j] += x[i][k] * y[k][j]; }


Download ppt "Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002."

Similar presentations


Ads by Google