You are on page 1of 22

Computer and Information Technology for (HKCEE) Module A1

6.1Arrays and Strings


6.2 Text files
6.3 User-Defined Functions
6.4 Implement a Structured Program Using
Functions
Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


Array
a collection of values of same type
each element in the array is accessed by an
index
array name index value
number [0] number [1] number [2] number [n-1]

1st element 2nd element 3rd element nth element

© Longman Hong Kong Education Page2


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


One dimensional array
e.g. an integer array of 3 integers:
declaration: int number[3];
initialisation:
number[0]=50;
number[1]=40;
number[2]=60;
initialise during declaration:
int number[3]={50,40,60};
first element of the array: number[0]
last element of the array: number[2]

© Longman Hong Kong Education Page3


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


Two dimensional array
e.g. two-dimensional array with 2 rows and 3
columns
declaration: int number[2][3];
legal range of the first dimension: 0 to 1
legal range of the second dimension: 0 to 2
assign a value to the element corresponding to ith row
and jth column:
number[i][j]=1;
initialise during declaration:
int a[2][3]={{1,3,2},{-3,5,0}};
or int a[2][3]={1,3,2,-3,5,0};
© Longman Hong Kong Education Page4
Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


String
an array of characters
e.g. declare and assign the string “hello”
char a[] = “hello”;
the string “hello” consists of 6 characters:
‘h’,‘e’,‘l’,‘l’,‘o’ and termination character
‘\0’

h e l l 0 \0
a[0] a[1] a[2] a[3] a[4] a[5]

© Longman Hong Kong Education Page5


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


String
alternative ways to declare and initialise the string:
char a[6] = “hello”;
char a[] = {‘h’,‘e’,‘l’,‘l’,‘o’,‘\0’};
char a[6] = {‘h’,‘e’,‘l’,‘l’,‘o’,‘\0’};
char a[6];
a[0]=‘h’; a[1]=‘e’; a[2]=‘l’;
a[3]=‘l’; a[4]=‘o’; a[5]=‘\0’;
incorrect way of declaration and initialisation
char a[5] = “hello”;
char a[];
© Longman Hong Kong Education Page6
Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


String
print in string format
printf(“a %s”,a);
print in character format
for(i=0; a[i} != ‘\0’; i++)
printf(“%c”, a[i]);
input a string using scanf
scanf(“%s”,b);

© Longman Hong Kong Education Page7


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


String handling
string.h header file
#include <string.h>
strcpy
string copy function
char a[10], b[10]
strcpy(b, “I like C”);
strcpy(a,b); copy the content from b to a

© Longman Hong Kong Education Page8


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


String handling
strcat
concatenation (the joining of two strings into one)
char s[30],t[30];
strcpy(s,“Hello, ”);
strcpy(t,“how are you?”);
strcat(s,t);
after execution:
string s: “Hello, how are you?”
string t: “how are you?”

© Longman Hong Kong Education Page9


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.1 Array and Strings


String handling
strlen
return the length of a string
int n;
char s[30];
strcpy(s,”how are you?”)
n = strlen(s);
– n = 12 (integer)
– “\0” is not counted

© Longman Hong Kong Education Page10


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.2 Text files


Opening and closing files
#include <stdio.h>
void main(){ 1. Declare a variable fp of
FILE *fp; file pointer type: FILE *
fp=fopen(“test.txt”, “w”);
fprintf(fp, “Testing\n”);
fclose(fp);
}

© Longman Hong Kong Education Page11


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.2 Text files


Opening and closing files
#include <stdio.h>
void main(){ 2. Open the file for writing
FILE *fp;
fp=fopen(“test.txt”, “w”);
fprintf(fp, “Testing\n”);
fclose(fp);
}

© Longman Hong Kong Education Page12


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.2 Text files


Opening and closing files
#include <stdio.h>
void main(){ 3. Call output functions
FILE *fp; to write to file
fp=fopen(“test.txt”, “w”);
fprintf(fp, “Testing\n”);
fclose(fp);
}

© Longman Hong Kong Education Page13


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.2 Text files


Opening and closing files
#include <stdio.h>
void main(){
FILE *fp;
fp=fopen(“test.txt”, “w”);
fprintf(fp, “Testing\n”);
fclose(fp);
} 4. Close the file associated
with the file pointer.

© Longman Hong Kong Education Page14


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.2 Text files


Opening and closing files
fopen: 3 modes
“r”: read or input data from file
“w”: write or output data to file
“a”: append data to existing file
fopen return NULL for non-existing file
handling file input: fscanf, fgetc
handling file output: fprintf, fputc

© Longman Hong Kong Education Page15


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.3 User-Defined Functions


Calling a function

function name

y=abs(x);
obtains return parameter
value of the / argument
abs function
© Longman Hong Kong Education Page16
Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.3 User-Defined Functions


Function prototype (function declaration)
the name of function
the number of arguments
the types of arguments
the return type
syntax:
type function_name(type1 parameter1,
type2 parameter2, ...);
example:
double length(double xcomponent, double
ycomponent);

© Longman Hong Kong Education Page17


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.3 User-Defined Functions


Function definition
syntax:
type function_name(type1 parameter1, type2
parameter2,...)
{
declaration(s)
statement(s)
}
example:
double length(double x, double y) {
return sqrt(x*x+y*y);
}
© Longman Hong Kong Education Page18
Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.3 User-Defined Functions


Called by value
When main calls function
main passes the value of variable to function
function keeps a duplicated copy of the value
After function finishes running
the original variable in main does not change
Called by reference
The variable in main shares the same memory
with the parameter in function

© Longman Hong Kong Education Page19


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.3 User-Defined Functions


Local variable
a variable declared within a subroutine is only
visible within the subroutine
Global variable
a variable declared outside of all the functions

© Longman Hong Kong Education Page20


Computer and Information
Technology for (HKCEE)
Module A1: Part B

6.4 Implement a Structured Program


Using Functions
3 characteristics of structured program
contain no goto command
adopt top-down design
modular

© Longman Hong Kong Education Page21


Computer and Information Technology for (HKCEE) Module A1

END

You might also like