27
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Embed Size (px)

Citation preview

Page 1: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

C Programming

Lecture 10

Instructor: Wen, Chih-YuDepartment of Electrical Engineering

National Chung Hsing University

Page 2: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays

Topics Passing 2-D arrays to functions Declare the size of a 2-D array Reading an entire 2-D array form a file Calling a function with a 2-D array Using a 2-D array in a function

Page 3: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays

/* Program for Lesson 6_6 */ #include <stdio.h> #define MAX_NUM_ROWS 8 #define MAX_NUM_COLS 10 void function1(int m, int n, int b[ ][MAX_NUM_COLS]); void main (void) { int i,j, num_rows, num_cols; int a[MAX_NUM_ROWS][MAX_NUM_COLS]; FILE *infile; infile = fopen ("L6_6.DAT","r"); /*************************************************************** SECTION 1- READING A 2-D ARRAY FROM A FILE ***************************************************************/ fscanf (infile,"%d %d", &num_rows, &num_cols) ; for (i=0; i<num_rows; i++) { for (j=0; j<num_cols; j++) { fscanf(infile,"%d ", &a[i][j]); } }

Input file L6_6.DAT3 41 2 3 42 4 6 83 5 7 9

In a function prototype, a 2-D array can have the left set of brackets empty, but the other set of brackets must be filled.

Page 4: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays

How to envision the array a[ ][ ]? 1 2 3 4 * * * * * * 2 4 6 8 * * * * * * 3 5 7 9 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Page 5: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays How to have a program to determine the actual numbers

of rows and columns filled for two-dimensional arrays? Sentinel values: values that distinct from and cannot be

confused with other data values (similar to how we handle EOF)

From the input file How to read a 2-D array from a data file?

for (i=0; i<num_rows; i++) { for (j=0; j<num_cols; j++)

{

fscanf(infile,"%d ", &a[i][j]);

}

}

Loop over the number of rows

Loop over the number of columns

Page 6: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays How to use a multidimensional array in the parameter list of a

function call? /*********************************************************** ** SECTION 2 - CALLING A FUNCTION WITH A 2-D ARRAY ***********************************************************/ function1(num_rows, num_cols, a); /*********************************************************** ** SECTION 3 - PRINTING A 2-D ARRAY ***********************************************************/ for (i=0; i<num_rows; i++) { for (j=0; j<num_cols; j++) { printf("%d ", a[i][j]); } printf("\n"); } }

Calling function1. it is advisable to pass the number of rows and columns along with the array address to a function.

Page 7: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays Passing the ability to access all array elements to a

function

function1(num_rows, num_cols, a);

void function1(int m, int n, int b[ ][MAX_NUM_COLS]);

Address of first element passed to pointer variable indicated with brackets

Page 8: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays /*************************************************************** SECTION 4 - FUNCTION THAT PERFORMS OPERATIONS ON A 2-D ARRAY ***************************************************************/ void function1 (int m, int n, int b[ ][MAX_NUM_COLS]) { int i,j; for (i=0; i<m; i++) { for (j=0; j<n; j++) { b[i][j] += 100; } } }

Page 9: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays

Why to include the maximum number of columns in the second set of brackets of the array parameter in the prototype? For an array element, a[x][y] of an array declared with the

size a[MAX_NUM_ROWS][MAX_NUM_COLS], C uses a formula to locate the element’s position in memory: sequence location = (x * MAX_NUM_COLS) + y + 1

a[1][2] (x = 1, y = 2) sequence location = (1 * 10) + 2 + 1 = 13, which is the 13th

element.

Page 10: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays

For a 4-D array, what to include in the function prototype?

#define I 10 #define J 5 #define K 8 #define L 3 int a[I][J][K][L]; Function prototype

void function1(… int a[ ][J][K][L]…);

Page 11: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Functions and 2-D Arrays

Function prototype and function declarator void function1(int m, int n, int b[ ][MAX_NUM_COLS]); void function1(int, int, int [ ][MAX_NUM_COLS]); C ignores the variable names in the function prototype.

Page 12: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data

Topics The set of characters Single character input and output Characters treated as integers Input buffer Flushing the buffer

Page 13: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data How to declare character variables?

Declared with the key word char. char var1, var2, var3, …;

How to write an assignment statement with these character variables? Enclose the constant in single quotes, ‘ ’. For instance, c1 = ‘g’;

How does C handle ANSI C character constants? C actually uses the integer value of the character

functions and operations. Appendix A (ANSI C characters and their

codes/values)

Page 14: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data How to print characters?

Use %c or %d conversion specification printf(“%c %c”, c1, c2);

print the character variables c1 and c2 printf(“%d %d”, c4, c5);

Cause the integer values to be printed

How the putchar function work? Prints the character that is argument to the standard output device

(screen). putchar (character) putchar(c2);

Causes the value of the character c2 to be printed to the screen putchar(‘y’);

Causes the character y to be printed to the screen

Page 15: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data

What does putchar(32) do? C uses the integer value of the character in its functions. putchar(32) causes the character represented by the

integer value 32 to be printed to the screen (the character ‘space’).

printf(“%c %c”, c1, c2); putchar(c1); putchar(32); putchar(c2);

Page 16: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data How to read characters from the keyboard using

the scanf function? Use %c conversion specification.

scanf(“%c%c”, &c4, &c5); what does the function getchar do?

Returns a character that has been input from the standard input device (keyboard) to the program.

Works with the input buffer to get the information typed at the keyboard

getchar(); Nothing should be put in the parentheses. c6 = getchar();

Page 17: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data What is input buffer and how does it work with getchar?

A buffer is a portion of memory reserved for temporarily holding the information that is being transferred.

A position indicator keeps track of the point at which no further information has been read.

The getchar function works with the buffer position indicator to retrieve the next character in the buffer and advance the position indicator.

xp return

x p \n

Position indicator

Page 18: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data getchar();

c6 = getchar();

c7 = getchar();

vs return

x p \n

Position indicator

x p \n v s \n

Position indicator

Page 19: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data

What are the difficulties in dealing with the getchar function? Need Enter to work An extra \n always will be in the buffer As further getchar calls are executed, the extra

character may cause execution to be different from what it should be.

How to solve this problem? The function fflush

Page 20: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data

What does the function fflush do? The function fflush can be used to flush or empty the

buffer after obtaining the characters of interest. fflush(stdin);

c8 = getchar();

c9 = getchar();

fflush(stdin); Flushing the buffer after the two calls allows the next

calls to getchar to avoid reading extraneous characters.

Flushed the input buffer before and after the two calls to getchar.

Page 21: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data

What is stdin? stdin is a file pointer, which is declared in stdio.h. In stdio.h, stdin is defined to point to the standard input

stream. Therefore,

fflush(stdin); flushes what is pointed by stdin, which is the standard

input stream – the input buffer from the keyboard. Cannot choose to use stdin as an identifier for a variable

Page 22: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data

How to avoid the problems of getchar? The function getche This function does not work with the input buffer and,

therefore, produced unbuffered input. It deals with the OS and does not require the Enter key

to be pressed for the character code to be transferred. The form is getche(); where nothing should be put in

the parentheses. Lose some portabilities of your program (not ANSI C compatible)

Page 23: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Single Character Data Why not forget getchar and just use scanf?

scanf When the scanf function is activated, execution of the

program stops. Execution begins again when the user presses the Enter key,

causing scanf to read the information in the input buffer. Interprets the information in the input buffer based on the

string literal used in the function call.1. Conversion or format specifiers (begun with a %)2. White-space characters (treats a space, Tab, and Enter as white

space)3. Non-white-space characters

Page 24: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

The scanf Function Call scanf (“%c%c”, &c4, &c5);

The string literal here has two conversion specifiers with no white-space and no non-white-space characters.

It is difficult to deal with scanf characters because of the way that scanf function deals with white space in both the literal and the input buffer.

A space can be interpreted as a character. scanf(“%c[ ]%c”, &c1, &c2);

Will not accept [ ] [ ] [ ] as input ([ ] is a single space) Until a non-white-space character is pressed, scanf will not move

forward

Page 25: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Strings and PointersDeclaring, Initializing, and Printing Strings (Lesson 7.1)

Topics Character arrays Initializing single characters Initializing strings Printing strings Memory arrangement

Page 26: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Arrays and Strings

What is a string? A string is an array of characters including the

terminating null (\0) character. bb[0] = ‘c’; bb[1] = ‘a’; bb[2] = ‘t’; bb[3] = ‘\0’; The last memory cell contains the escape sequence ,‘\0’,

which is treated as a single character. ‘\0’ is called the null character.

Page 27: C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University

Arrays and Strings

“This is a string constant.” C recognizes this statement to be a string and adds \0

after the period when stored in memory The character array must be large enough to include

the \0 character. A string is stored in a character array, whereas a

single character is stored in a character variable.