250 likes | 323 Views
Learn how to work with arrays in C programming, covering copying, initialization, errors, functions, multi-dimensional arrays, and more. Improve your skills and understanding with practical examples and detailed explanations.
E N D
Mon July 24, 2002 Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan
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] ;
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]); }
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' };
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} */
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'};
errors... • more initializers than size: int scores[3] = {90,100,50, 20}; • accessing unallocated blocks: int scores[3] = {90,100,50}; ... scores[4] = 3;
Arrays as Arguments • Write a function that takes in a float-array of size 10, and returns the average of these 10 numbers.
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 ); ...
Goal • Write a function that takes a float-array of any size, and returns the average of the elements.
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); ...
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);
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' };
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";
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);
Goal • Write a function that prints a character array, char-by-char.
void PrintCharArr(char str[ ], int maxSize) { int i; for(i=0; i<size; i++){ if(str[i] == '\0') break; printf("%c", arr[i]); } }
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);
Goal • Read a line of input into an array and print the array character by character.
#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][ ]
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.
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 */ }
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.
#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]; } } } }