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

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Introduction to C Programming
1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Chapter 10.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Chapter 8 Arrays and Strings
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Lecture 13. Outline Standard Input and Output Standard Input and Output (I/O)– Review & more Buffered/unbuffered input Character I/O Formatted I/O Redirecting.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Chapter 8 Arrays and Strings
Chapter 18 I/O in C.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Chapter 3 Input and Output
Spring 2005, Gülcihan Özdemir Dağ Lecture 8, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 8 Outline 8.1 Declaring.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CS 1704 Introduction to Data Structures and Software Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Files A collection of related data treated as a unit. Two types Text
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
C Formatted Input/Output
© 2016 Pearson Education, Ltd. All rights reserved.
Input/output.
Chapter 7 Text Input/Output Objectives
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Programming in C Input / Output.
Input and Output Lecture 4.
Programming in C Input / Output.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 18 I/O in C.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
File Input and Output.
Chapter 18 I/O in C.
Programming in C Input / Output.
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
Module 12 Input and Output
Introduction to C EECS May 2019.
Files Chapter 8.
Chapter 18 I/O in C.
Presentation transcript:

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

Functions and 2-D Arrays /* Program for Lesson 6_6 */ #include #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.DAT 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.

Functions and 2-D Arrays How to envision the array a[ ][ ]? * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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

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.

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

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

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) = 13, which is the 13th element.

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

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.

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

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)

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

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

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

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 xp\n Position indicator

Single Character Data getchar(); c6 = getchar(); c7 = getchar(); vs return xp\n Position indicator xp\nvs Position indicator

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

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.

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

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)

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

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

Strings and Pointers Declaring, Initializing, and Printing Strings (Lesson 7.1) Topics Character arrays Initializing single characters Initializing strings Printing strings Memory arrangement

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.

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.