Sie sind auf Seite 1von 28

Chapter 6 Arrays, Text Files and Functions Question Bank

Chapter 6 Arrays, Text Files and Functions


Multiple Choice Questions

(U02C06L01Q001)
Consider the following program.
#include <stdio.h>
/* Prototype declaration */
char p (char n);
void main( ){
int n;
Statement S
p(n);
} /* main */
void p (char n){
Statement T
printf("%c", n);
return;
}
Which of the following is valid for the statement S and the statement T?
Statement S Statement T
A. n = 5; n = '7';
B. n = '5'; n = '7';
C. n = '5'; n = 7;
D. n = 5; n = 7;
Answer
A

Computer & Information Technology for HKCEE 1 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L01Q002)
Consider the following prototype declaration.
void P2 (float A, int B);
Which of the following statements are INVALID if r is a floating-point variable and n is an integer
variable?
(1) P2(n, 5);
(2) P2(n, n);
(3) P2(2.5, r);
A. (1) only
B. (1) and (2) only
C. (2) and (3) only
D. (1), (2) and (3)
Answer
D

(U02C06L01Q003)
What are the outputs of the following program?
#include <stdio.h>
void p(int *x, int y);
void main( ){
int m, n;
m = 1; n = 2;
p(&m, n);
printf("m = %d; n = %d", m, n);
}
void p(int *x, int y){
*x = 10; y = 20;
return;
}
A. m = 1, n = 2
B. m = 1, n = 20
C. m = 10, n = 2
D. m = 10, n = 20
Answer
C

Computer & Information Technology for HKCEE 2 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L01Q004)
What is the output of the following program?
#include <stdio.h>
void p (int *x, int y);
void main( ){
int m, n;
m = 100; n = 200;
P(&m, n); P(&n, m);
printf("%d", m);
}
void p (int *x, int y){
*x = *x + 1; y = y + 10;
return;
}
A. 100
B. 101
C. 110
D. 111
Answer
B

Computer & Information Technology for HKCEE 3 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L01Q005)
What is the output of the following program?
#include <stdio.h>
void p5(int *x, int y);
void main( ){
int m, n;
m = 100; n = 200;
p5(&m, n); p5(&n, m);
printf("%d", n);
}
void p5(int *x, int y){
x = x + 1; y = y + 10;
return;
}

A. 200
B. 201
C. 210
D. 211
Answer
B

(U02C06L01Q006)
Which of the following statements about function declaration and definition is correct?
A. The function call is found in the called function.
B. The function declaration requires that the parameters be named.
C. The function definition is done with a prototype statement.
D. The function definition contains executable statements that perform the function’s task.
Answer
D

Computer & Information Technology for HKCEE 4 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L01Q007)
Consider the following function P7.
void P7(float *x, int y){
int i;
for (i = 2; i < y + 1; i++)
*x = *x * 2;
return;
}
What is the value of x after the statements below are executed?
x = 2;
P7(&x, 5);
A. 1
B. 2
C. 10
D. 32
Answer
D

(U02C06L01Q008)
The function that returns the absolute value of a long integer is
A. abs
B. labs
C. fabs
D. dabs
Answer
B

(U02C06L01Q009)
Evaluate the value of the following expression:
floor(-1.1);
A. -1
B. -2
C. -1.0
D. -2.0
Answer
D

Computer & Information Technology for HKCEE 5 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L01Q010)
Evaluate the value of the following expressions:
float pow(float x, float y);
pow(3.0, 4.0);
A. 12.0
B. 81.0
C. 27.0
D. 64.0
Answer
B

(U02C06L01Q011)
Which of the following is NOT a standard file stream?
A. stdin
B. stdfile
C. stdout
D. stderr
Answer
B

(U02C06L01Q012)
If a file is opened in the r mode, C
A. opens the file for reading and sets the file marker at the beginning.
B. opens the file for reading and sets the file marker at the end.
C. opens the file for writing and sets the file marker at the beginning.
D. opens the file for writing and sets the file marker at the end.
Answer
A

(U02C06L01Q013)
Which of the following functions that prepares a file for processing?
A. fopen
B. fclose
C. fscanf
D. fprintf
Answer
A

Computer & Information Technology for HKCEE 6 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L01Q014)
Which of the following is a correct file pointer variable?
A. FILE : *fpStudent;
B. FILE *fpStudent;
C. FILE : fpStudent;
D. FILE fpStudent;
Answer
B

(U02C06L01Q015)
Which of the following file modes is INCORRECT?
A. "c"
B. "r"
C. "w"
D. "a"
Answer
A

(U02C06L01Q016)
Which of the following functions reads the next character from the standard input stream and
returns its value?
A. fputc( )
B. fputs( )
C. fgetc( )
D. fgets( )
Answer
C

(U02C06L01Q017)
The two input functions that read text data and convert the data to the types:
A. fputs and puts
B. fread and read
C. fprintf and printf
D. fscanf and scanf
Answer
D

Computer & Information Technology for HKCEE 7 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L01Q018)
Consider the following segment. What are written to the file, outFile?
FILE *outFile;
Char desc[ ] = "Hammers";
int quant = 24;
fprintf(outFile, "%s %d\n ", desc, quant);
A. Hammers, 24
B. Hammers, quant
C. desc, quant
D. desc, 24
Answer
A

Computer & Information Technology for HKCEE 8 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

Conventional Questions

(U02C06L02Q001)
Write down the output of the following C statements. (Use a ‘ ’ to indicate a blank.)
(a) printf(“Enter a number: ”);
(b) printf(“%10f”, 678.267);
(c) printf(“%2.2f”,1200);
(d) printf(“a\t\tb\tc”);
(e) printf(“654,321”);
(f) printf(“AB”);
printf(“C\n”);
printf(“D”);
printf(“EF\n”);
printf(“G”);
(g) X = 1; Y = 2; printf(“X = %d%d = Y”, X, Y);
(7 marks)
Answers
(a) Enter a number:
(b) 6.78267000
(c) 1200.00
(d) a b c
(e) 654,321
(f) ABCD
EF
G
(g) X=12=Y

Computer & Information Technology for HKCEE 9 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q002)
Write statement(s) for each of the following cases.
(a) Display your name.
(b) Prompt a student to input his/her name. Store the name in a string variable StudentName.
Then display the word ‘Hello!’ and the name of the student. For example, if the student
inputs Paul, the program displays ‘Hello! Paul’.
(c) Prompt users to input a number and then display the square root of the number with 1 decimal
place.
(7 marks)
Answers
(a) printf(“Chan Siu Ming”);
(b) printf(“Input your name: ”);
scanf(“%s”, &StudentName);
printf(“\nHello! %s”, StudentName);
(c) printf(“Input a number: ”);
scanf(“%d”, &Num);
printf(“%.1f”, sqrt(Num));

Computer & Information Technology for HKCEE 10 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q003)
What are the values stored in variables x, y and z after executing the following program segments?
(a) x = 2;
y = x + 4;
x = x + y;
(b) x = 10;
y = 1
x = x + 1;
x = x + 2;
y = x - y;
(c) x = 10;
y = (int)floor(sqrt(x));
z = x + y;
(d) x = -5;
y = -x;
z = abs(x - y);
(10 marks)
Answers
(a) x = 8, y = 6
(b) x = 13, y = 12
(c) x = 10 , y = 3, z = 13
(d) x = -5, y = 5, z = 10

Computer & Information Technology for HKCEE 11 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q004)
Write a C program which receives the name, the weight in kg and the height in m of a student. Then
the program calculates and displays the Body Mass Index (BMI) of the student according to the
formula below.
Weight
BMI =
Height 2
A sample output is given below.

Sample output:
Enter your name: Peter Chan
Enter your weight in kg: 50.25
Enter your height in m: 1.65
Peter Chan’s BMI is 18.5.
(7 marks)
Answers
#include <stdio.h>
void main( ){
char Name[25];
float Weight, Height, BMI;
printf(“Enter your name: ”);
scanf(“%s”, &Name);
printf(“Enter your weight in kg: ”);
scanf(“%f”, &Weight);
printf(“Enter your height in m: ”);
scanf(“%f”, &Height);
BMI = Weight /(Height * Height);
printf(“%s\’s BMI is %.1f.”, Name, BMI);
}

Computer & Information Technology for HKCEE 12 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q005)
Write the C code for each of the following formulas. Assume that all variables are defined as
double.
mv 2
(a) KinEn =
2

b+c
(b) res = 2bc

(12 marks)
Answers
(a) #include <stdio.h>
void main( ){
double KinEn, m, v;
printf(“Enter a distance, m: ”);
scanf(“%lf”, &m);
printf(“Enter a velocity, m/s: ”);
scanf(“%lf”, &v);
KinEn = (m * v * v)/2;
printf(“The Kinetic Energy = %.2lf”, KinEn);
}

(b) #include <stdio.h>


void main( ){
double res, b, c;
printf(“Enter the value, b: ”);
scanf(“%lf”, &b);
printf(“Enter the value, c: ”);
scanf(“%lf”, &c);
res = (b + c)/(2 * b * c);
printf(“The value, res = %.2lf”, res);
}

Computer & Information Technology for HKCEE 13 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q006)
Write a C program which prompts a user to input his/her name and age. Then the program displays
the user’s name and age in a complete sentence.

Sample output:
Input your name: Peter Wong
Input your age: 16
Peter Wong is 16 years old.
(6 marks)
Answers
#include <stdio.h>
void main( ){
char Name[25];
int Age;
printf(“Input your name: ”);
scanf(“%s”, &Name);
printf(“Input your age: ”);
scanf(“%d”, &Age);
printf(“%s is %d years old.”, Name, Age);
}

Computer & Information Technology for HKCEE 14 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q007)
(a) Write a program which receives a time in ‘hour : minute : second’ format and then converts the
input time to number of seconds. A sample output is given below.

Sample output:
Enter a time in hour, minute, second : 13 24 5
13 hours 24 minutes 5 seconds have 48245 sec.

(b) Write another program which receives a time in second and then converts the time to ‘hour:
minute: second’ format. A sample output is given below.

Sample output
Enter a time in seconds (0-86400): 48245
13 : 24 : 5
(8 marks)
Answers
(a) #include <stdio.h>
void main( ){
float h, m, s;
printf(“Enter a time in hour, minute, second: ”);
scanf(“%f%f%f”, &h, &m, &s);
printf(“%.2f hours %.2f minutes %.2f seconds have %.2f
sec.”, h, m, s, (((h * 60) + m) * 60 + s));
}
(b) #include <stdio.h>
#include <math.h>
void main( ){
int s;
printf(“Enter a number in seconds (0-86400): ”);
scanf(“%d”, &s);

printf(“%d:%d:%d”,(int)(floor(s/60))/60,(int)(floor(s/60))%6
0, s%60);
}

Computer & Information Technology for HKCEE 15 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q008)
Write a function PowerOf which gets two integer parameters M and N. Then it displays the value
of MN on screen.
(8 marks)
Answers
void PowerOf(int M, int N){
int I, X;
X = 1;
for (I = 1; I <= N; I++)
X = X * M;
printf("%d", X);
return;
}

(U02C06L02Q009)
Write a function that receives a positive floating-point number and rounds it to two decimal places.
For example, 127.565044 rounds to 127.570000. Hint: To round, you must convert the floating-
point number to an integer and then back to a floating-point number. Printed the rounded numbers
to six decimal places.
(7 marks)
Answers
void round2(float x){
float front, /* integral numbers */
back; /* decimal places */
front = floor(x); /* digits before dot */
back = (x - front) * 1000; /* 3 digits after dot */
back = ceil(back / 10); /* get 2 decimal places */
printf("%d.%d", (int)front, (int)back);
return;
}

Computer & Information Technology for HKCEE 16 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q010)
The program receives the coefficients of a quadratic equation and displays the roots of the equation.
#include <stdio.h>
#include <math.h>
void main( ){
float A, B, C, D, R1, R2;
printf("The format of a quadratic equation: Ax^2 + Bx + C = 0.");
printf("\nEnter coefficients A, B and C : ");
scanf("%f%f%f", &A, &B, &C);
D = B * B - 4.0 * A * C;
if(D == 0){
R1 = (-B + sqrt(D)) / (2.0 * A);
printf("The equation has two real and equal roots: %.2f", R1);
}
else
if(D > 0){
R1 = (-B + sqrt(D)) / (2 * A);
R2 = (-B - sqrt(D)) / (2 * A);
printf("The equation has two real and unequal roots: ");
printf("%.2f %.2f", R1, R2);
}
else printf("The equation has no real root.");
}
The program is rewritten by using modular approach. The main program is given as follows:
#include <stdio.h>
/* FUNCTION PROTOTYPES */
void GetCoefficient (void);
float FindDiscriminant (float, float, float);
void FindRoot (float, float, float, float);
void DisplayResult(float);
/* VARIABLES */
float A, B, C, D, R1, R2;
void main( ){
GetCoefficient();
FindDiscriminant(A, B, C);
FindRoot(A, B, C, D);
DisplayResult(D);
}

Computer & Information Technology for HKCEE 17 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

The details of each function are as follows:


(a) function GetCoefficient prompts users to key in the coefficients.
(b) function FindDiscriminant receives coefficients A, B and C as input and finds out the
discriminant D of the equation as output.
(c) function FindRoot receives coefficients A, B, C and discriminant D as input and then find
out the roots R1 and R2 as output.
(d) function DisplayResult displays the roots of the equation.
Complete the functions.
(20 marks)

Computer & Information Technology for HKCEE 18 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

Answers
(a)
void GetCoefficient (void){
printf("The format of a quadratic equation: Ax^2 + Bx + C = 0.");
printf("\nEnter coefficients A, B and C : ");
scanf("%f%f%f", *&A, *&B, *&C);
return;
}
(b)
float FindDiscriminant (float A, float B, float C){
D = B * B – 4 * A * C;
return D;
}
(c)
void FindRoot (float A, float B, float C , float D){
if (D == 0)
R1 = (-B + sqrt(D)) / (2 * A);
else{
R1 = (-B + sqrt(D)) / (2 * A);
R2 = (-B - sqrt(D)) / (2 * A);
}
return;
}
(d)
void DisplayResult(float D){
if (D > 0){
printf("The equation has two real and unequal roots: ");
printf("%.2f and %.2f ", R1, R2);}
else
if (D == 0){
printf ("The equation has two real and equal roots: ");
printf("%.2f", R1);}
else printf("The equation has no real root.");
return;
}

Computer & Information Technology for HKCEE 19 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q011)
The program receives the lengths of the two shorter sides (legs) of a right-angled triangle and then
finds out the length of the hypotenuse.

Sample Output 1:
Enter the lengths of the two sides: 3.1 6.83
The length of the hypotenuse = 7.5

Sample output 2:
Enter the lengths of the two sides: 4.5 7.2
The length of the hypotenuse = 8.49

#include <stdio.h>
#include <math.h>
void GetInput(void);
float FindSide(float, float);
float Side1, Side2, Ans;
void main( ){
……
}
void GetInput(void){
……
}
float FindSide(float, float){
……
}
(a) Function GetInput asks users to enter the lengths of the two sides of a right-angled triangle:
Side1 and Side2. Consider the sample outputs above. Complete the function GetInput.
(b) Function FindSide receives the lengths of the two sides from the Function GetInput.
Then it returns the length of the hypotenuse, Ans to the main function. Complete the
function FindSide.
(c) Complete the main function with the use of the functions.
(13 marks)

Computer & Information Technology for HKCEE 20 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

Answers
(a)
void GetInput(void){
printf("Enter the lengths of the two sides: ");
scanf("%f%f", &Side1, &Side2);
return;
}
(b)
float FindSide(float Side1, float Side2){
Ans = sqrt(Side1 * Side1 + Side2 * Side2);
return Ans;
}
(c)
#include <stdio.h>
#include <math.h>
void GetInput(void);
float FindSide(float, float);
float Side1, Side2, Ans;
void main( ){
GetInput( );
Ans = FindSide(Side1, Side2);
printf("The length of the hypotenuse = %.2f", Ans);
}

Computer & Information Technology for HKCEE 21 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q012)
A program receives the old salary and the new salary of an employee. Then it computes the raise
and the percent increase in salary.

Sample Output:
Enter employee name: Wilson
Enter the old salary: 25000.00
Enter the new salary: 26450.00
Wilson received a 5.8% raise and now earns $1450.00 more a year.

Some parts of the program are given as follows.

/* PREPROCESSING DIRECTIVES */
#include <stdio.h>
/* FUNCTION PROTOTYPES */
void InputData(void); /* input data */
void CalcRaise(void); /* calculate raise */
void PrnOutput(void); /* print output */
float percentfn(float, float); /* percent increase */
/* CONSTANTS AND VARIABLES */
char name[21]; /* employee name */
float oldSalary, /* old salary */
newSalary, /* new salary */
raise, /* dollar increase in salary
*/
percent; /* percent increase in salary
*/
void main ( ){
InputData( ); /* input data */
CalcRaise( ); /* calculate raise */
PrnOutput( ); /* print output */
}

Complete the functions: InputData( ),CalcRaise( ), PrnOutput( ) and


percentfn(float, float).
(12 marks)

Computer & Information Technology for HKCEE 22 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

Answers
void InputData(void){
printf("Enter employee name: ");
scanf("%s", &name);
printf("\tEnter old salary: ");
scanf("%f", oldSalary);
printf("\tEnter new salary: ");
scanf("%f", newSalary);
return;
}

void CalcRaise(void){
raise = newSalary – oldSalary;
percent = percentfn(raise, oldSalary);
return;
}

void PrnOutput(void){
printf("\n\n%s received a %.1f%% raise and ", name, percent);
printf("\nnow earns $%7.2f more a year. ", raise);
return;
}

float percentfn(float amount, float salary){


float result;
result = (amount/salary) * 100;
return result;
}

Computer & Information Technology for HKCEE 23 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q013)
(a) Explain briefly the relationship between the terms character, field, record and file.
(b) In the following text file which stores the names and sex of 3 persons, state which are fields,
records and files.

Name Sex
Alex M
Joyce F
Simon M

(7 marks)
Answers
(a) A file usually contains records separated into lines. Each record consists of different data items
called fields, which are constructed by characters.
(b) The name and sex are 2 fields. Every line consists of a record and all the records form a file.

(U02C06L02Q014)
Explain briefly the characteristics of text files.
(2 marks)
Answers
Text files are sequential files that save data sequentially. All the data are stored as a sequence of
characters even though they belong to different lines.

(U02C06L02Q015)
What is the use of an end-of-file marker in a text file? What do most IBM-compatible
microcomputers use to signal EOF?
(2 marks)
Answers
An end-of-file marker is an invisible mark located at the end of a text file. It is used to denote the
end of a text file. They use <ctrl + z> key combination to signal EOF.

Computer & Information Technology for HKCEE 24 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q016)
Read the following program and write out the result displayed.
#include <stdio.h>
#include <stdlib.h>
void main(){
FILE *fpIn;
int numIn;
fpIn = fopen("P7_04.DAT","r");
if (!fpIn)
printf("Could not open file\a\n");
else{
while(fscanf(fpIn, "%d", &numIn)!=EOF)
printf("%d\n", abs(numIn));
}
}
Text File (P7_04.DAT):
12 -27 -50 90
10 08 02 11

(3 marks)
Answers

12
27
50
90
10
8
2
11

Computer & Information Technology for HKCEE 25 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q017)
Write a program that copies a text file of integers (P7_05A.DAT) to a new file(P7_05B.DAT).
The program should show a message
i. if the files could not be opened;
ii. if the copying is processing;
iii. if the copying is complete.
(18 marks)
Answers
Suggested program codes.
#include <stdio.h>
#include <stdlib.h>
void main(){
FILE *fpIn;
FILE *fpOut;
int numIn;
int closeResult;
printf("Running file copy\n");
fpIn = fopen("P7_05A.DAT","r");
if (!fpIn){
printf("Could not open file\a\n");
exit(101);
}
fpOut = fopen("P7_05B.DAT","r");
if (!fpOut){
printf("Could not open file\a\n");
exit(102);
}
while(fscanf(fpIn, "%d", &numIn) != EOF)
fprintf(fpOut, "%d\n", numIn);
closeResult = fclose(fpOut);
if (closeResult == EOF){
printf("Could not close output file\a\n");
exit(201);
}
printf("File copy complete");
}

Computer & Information Technology for HKCEE 26 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q018)
What output is produced from the following program? Include in your answer the contents of any
files created.
#include <stdio.h>
void main(){
FILE *fp;
int i;
char ch;
fp = fopen("TEST.DAT", "w");
for (i=2; i<20; i+=2)
fprintf(fp, "%3d", i);
fclose(fp);
fp = fopen("TEST.DAT", "r");
while((ch = fgetc(fp)) != EOF)
if(ch != '1')
fputc(ch, stdout);
else{
putchar('\n');
putchar(ch);
}
}
(5 marks)
Answers
Both procedures readln and read can read data from a text file and store them in variables in the
variable list.
TEST.DAT
2 4 6 8 10 12 14 16 18
On screen
2 4 6 8
10
12
14
16
18

Computer & Information Technology for HKCEE 27 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 6 Arrays, Text Files and Functions Question Bank

(U02C06L02Q019)
Write a program that counts the number of words in a file( P7_07.DAT ). A word is defined as one
or more characters separated by one or more whitespace characters --- that is, by a space, a tab or a
newline.
(20 marks)
Answers
Suggested program codes.
/* Count number of words in file. Words are separated by
whitespace characters: space, tab, and newline. */
#include <stdio.h>
#define WHT_SPC (cur == ' ' || cur == '\n' || cur == '\t')
int main(void){
/*Local definition */
int cur;
int countWd = 0;
char word = 'X'; /* X is out of word: Y in word */
FILE *fp1;
if (!(fp1 = fopen("P7_07.DAT", "r"))){
printf("Error opening P7_07.DAT for reading");
return (1);
}
while((cur = fgetc(fp1)) != EOF){
if (WHT_SPC)
word = 'X';
else if (word == 'X'){
countWd++;
word = 'Y';
}
}
printf("The number of words is: %d\n", countWd);
fclose(fp1);
return 0;
}/* main */

Computer & Information Technology for HKCEE 28 © Pearson Education Asia Limited 2004
(Module A1)

Das könnte Ihnen auch gefallen