lecture 08 metu dept. of computer eng. summer 2002 ceng230 - section 01 introduction to c...

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

Post on 21-Dec-2015

223 views

Category:

Documents


3 download

TRANSCRIPT

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

Lecture 08

METU Dept. of Computer Eng. Summer 2002Ceng230 - Section 01 Introduction To C Programmingby Ahmet Sacan

Mon July 24, 2002

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

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] ;

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

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]);

}

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

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' };

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

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} */

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

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'};

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

errors...

• more initializers than size:

int scores[3] = {90,100,50, 20};

• accessing unallocated blocks:

int scores[3] = {90,100,50};

...

scores[4] = 3;

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

Arrays as Arguments

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

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

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 ); ...

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

Goal

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

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

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); ...

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

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);

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

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' };

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

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";

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

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);

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

Goal

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

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

void PrintCharArr(char str[ ], int maxSize)

{

int i;

for(i=0; i<size; i++){

if(str[i] == '\0')

break;

printf("%c", arr[i]);

}

}

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

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);

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

Goal

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

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

#define MAX_SIZE 256void 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][]

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

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.

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

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 */

}

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

Back to Non-char arrays.

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

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.

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

#define MAXCOLSvoid 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]; } } }}