21
CS1010 Discussion Session Week 13 DG8

DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Embed Size (px)

DESCRIPTION

Question 1 #include int main(void) { FILE *infile; int num; if ((infile = fopen("demo3.in", "r")) == NULL) { printf("Cannot open file demo3.in "); exit(1); } while (!feof(infile)) { fscanf(infile, "%d", &num); printf("Value read: %d ", num); } fclose(infile); return 0; } feof() is TRUE only after the end of file (EOF) is read, not when EOF is reached.

Citation preview

Page 1: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

CS1010 Discussion Session Week 13

DG8

Page 2: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

FILE * fopen ( const char * filename, const char * mode );Mode:

r: Open a file for reading. The file must exist.w: Create an empty file for writing. If a file

with the same name already exists its content is erased and the file is treated as a new empty file.

Returned value:If the file has been succesfully opened the

function will return a pointer to a FILE object that is used to identify the stream on all further operations involving it. Otherwise, a null pointer is returned.

Page 3: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question 1

#include <stdio.h> #include <stdlib.h>   int main(void) { FILE *infile; int num;   if ((infile = fopen("demo3.in", "r")) == NULL) { printf("Cannot open file demo3.in\n"); exit(1); }   while (!feof(infile)) { fscanf(infile, "%d", &num); printf("Value read: %d\n", num); }   fclose(infile); return 0; }

feof() is TRUE only after the end of file (EOF) is read, not when EOF is reached.

Page 4: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

“feof” checks if EOF value reached, no values read yet.“fscanf” gets 10 and stores in “num”. Print out “num”.“feof” checks if EOF value reached, but “10” was read.“fscanf” gets 20 and stores in “num”. Print out “num”.“feof” checks if EOF value reached, but “20” was read.“fscanf” gets 30 and stores in “num”. Print out “num”.“feof” checks if EOF value reached, but “30” was read.“fscanf” gets EOF. “num” is still 30. Print out “num”.“feof” checks if EOF value reached, EOF was read.Loop stops.

FILE *infile; int num;… while (!feof(infile)){

fscanf(infile, "%d", &num); printf("Value read: %d\n", num);}

Input: 10 20 30

Page 5: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question 1http://www.gidnetwork.com/b-58.html“fscanf” returns EOF when it can’t retrieve value

Quick fix 1: while (!feof(infile)) { if (fscanf(infile, "%d", &num) != EOF) printf("Value read: %d\n", num); }

Quick fix 2: while (fscanf(infile, "%d", &num) != EOF) printf("Value read: %d\n", num);

Page 6: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question 2Rewrite your sorting program to read in a list

of integers from an input text file, sort the list, and write the sorted list to an output text file. You may make your own assumption on the largest size of your list.

Q2_sort.c

Page 7: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question 3Write a program to read two sorted lists of

integers from two input text files, merge the lists, and write the merged list to an output text file. You should write a function to merge the lists:

merge(int arr1[], int size1, int arr2[], int size2, int arr3[]);

where arr1 and arr2 are the two given lists with sizes size1 and size2 respectively, and arr3 is the merged list.

Page 8: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

3 5 9 21

1 4 25

Counter 1

Counter 2

1 3 4 5 9 21 25

Page 9: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Do Exercises now!http://www.comp.nus.edu.sg/~laixn/cs1010/

week13/

Page 10: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #4 - 1

10© CS1010 (AY2010-2011 Semester 1)

Given a two-dimensional 20x20 integer matrix M where all the integers are in the range 0 to 99 (both inclusive), write a function

int findMost(int M[][20])

to find the integer that appears the most number of times in the matrix. In the case of a tie, return any one of the integer that appears the most.

Page 11: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #4 - 2

11© CS1010 (AY2010-2011 Semester 1)

#include <stdio.h>#include <time.h>

int main(){ int i, j, M[20][20];

srand((unsigned)time(NULL));

for(i=0; i<20; i++) for(j=0; j<20; j++) M[i][j] = rand() % 100;

printf("Most appearing integer: %d\n", findMost(M));

return 0;}

Access a value in computer’s system clock, which is used as a seed for random number generation.

Page 12: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #4 - 3

12© CS1010 (AY2010-2011 Semester 1)

int findMost(int M[][20]){ int freq[100] = {0}; int i, j, index, // index of the most frequent integer mostAppear; // frequency of the most frequent integer

for(i= 0; i<20; i++) { for(j=0; j<20; j++) { __________________ } } (to be continued on next page)

freq[M[i][j]]++; M[i][j] = 54, freq[54]++M[i][j] = 9, freq[9]++

Number of appearance for integers 0-99

Page 13: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #4 - 4

13© CS1010 (AY2010-2011 Semester 1)

________________________

________________________

for(i=1; i<100; i++) { if(mostAppear < freq[i]) { ________________________

________________________ } } return index;}

index = 0;

mostAppear = freq[0];

mostAppear = freq[i];

index = i;

Page 14: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question 5int foo(int n) { int a[3] = {2,3,5}, i;  if (n == 1) return 1;  for (i=0; i<3; i++) if (!(n%a[i])) return foo(n/a[i]);  return 0;}

a) Code traceb) Purpose of “foo”c) Iterative version

Page 15: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #5 - 1Q5

(a)

(b)

© CS1010 (AY2010-2011 Semester 1) 15

foo(30) -> foo(15) -> foo (5) -> foo(1);return value: 1

foo(840) -> foo(420) -> foo(210) -> foo(105) -> foo(35) -> foo(7);return value: 0

Exhaustively divide a given number by 2 first, then 3 and finally 5. Return 1 if the given number is just composed of factors 2, 3 and 5, return 0 otherwise.

Page 16: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #5 - 2Q5

(c)

© CS1010 (AY2010-2011 Semester 1) 16

int foo_iter(int n){ int a[3] = {2,3,5}, i;

for (i = 0; i < 3; i++) { while(n % a[i] == 0) n /= a[i]; }

return n == 1;}

Page 17: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #6 - 1

17© CS1010 (AY2010-2011 Semester 1)

int number[6][6];

15 22 8 11 59 44

50 64 29 10 34 20

17 86 27 98 57 21

6 16 88 42 36 45

31 26 12 23 82 54

28 66 58 69 41 1

Page 18: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #6 - 2

18© CS1010 (AY2010-2011 Semester 1)

take values of col j

sort

assign back to col j

int i, j, tempNumber[6]; for (i = 0; i < 6; i++) selectionSort( number[i] ); // At this point, fill in Table(a) on the content // in the number array.

for (j = 0; j < 6; j++){ for (i = 0; i < 6; i++) tempNumber[i] = number[i][j];

selectionSort( tempNumber ); for (i = 0; i < 6; i++) number[i][j] = tempNumber[i]; } // At this point, fill in Table(b) on the content // in the number array.

Page 19: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #6 - 3

19© CS1010 (AY2010-2011 Semester 1)

Table (a)8 11 15 22 44 59

10 20 29 34 50 64

17 21 27 57 86 98

6 16 36 42 45 88

12 23 26 31 54 82

1 28 41 58 66 69

Table (b)1 11 15 22 44 59

6 16 26 31 45 64

8 20 27 34 50 69

10 21 29 42 54 82

12 23 36 57 66 88

17 28 41 58 86 98

sort rows sort columns

Page 20: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

1 11 15 22 44 59

6 16 26 31 45 64

8 20 27 34 50 69

10 21 29 42 54 82

12 23 36 57 66 88

17 28 41 58 86 98

Question #6 - 4The following table has been sorted by rows first, by

column secondly:

© CS1010 (AY2010-2011 Semester 1) 20

y

x

Page 21: DG8. FILE * fopen ( const char * filename, const char * mode ); Mode: r: Open a file for reading. The file must exist. w: Create an empty file for writing

Question #6 - 5

© CS1010 (AY2010-2011 Semester 1) 21

int search (int key, int table[][6], int startX, int startY, int endX, int endY) {

int midX = (startX+endX) / 2, midY = (startY+endY) / 2;

if (startX > endX || startY > endY) return _________ ;

if (key == table[midX][midY]) return _________ ;

if (key < table[midX][midY]) return _______________________________________________

else

return _______________________________________________ }

0

1

search( key, table, midX+1, startY, endX, midY ) ||search( key, table, startX, midY+1, endX, endY );

search( key, table, startX, startY, endX, midY-1) ||search( key, table, startX, midY, midX-1, endY);