Sie sind auf Seite 1von 9

Chapter 9 Formulation of Algorithms Question Bank

Chapter 9 Formulation of Algorithms


Conventional Questions

(U02C09L02Q001)
Write C statements to perform the following jobs.
(a) Display the words ‘C Programming!’ without the quotation marks.
(b) Prompt a user to input his/her name and then display his/her name two times in a line.
(c) Prompt a user to input a number and then display the square of the number.
(7 marks)
Answers
(a) printf("C Programming!");
(b) printf("Input your name: ");
scanf("%s", Name);
printf("%s %s", Name, Name);
(c) printf("Input a number: ");
scanf("%d", &Num);
printf("%d", Num * Num);

Computer & Information Technology for HKCEE 1 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(U02C09L02Q002)
Write a program List that receives the age, weight and height of 3 students and then displays them
in table format. A sample output is given below.

Sample output:
Enter the age, weight and height of student 1: 15 60.5 169.3
Enter the age, weight and height of student 2: 16 48 160.5
Enter the age, weight and height of student 3: 15 51.3 157
Age Weight Height
Student 1 15 60.5 169.3
Student 2 16 48.0 160.5
Student 3 15 51.3 157.0
(8 marks)
Answers
#include <stdio.h>
void main( ){
int Age1, Age2, Age3;
float Weight1, Weight2, Weight3;
float Height1, Height2, Height3;
printf("Enter the age, weight and height of student 1: ");
scanf("%d %f %f",&Age1, &Weight1, &Height1);
printf("Enter the age, weight and height of student 2: ");
scanf("%d %f %f",&Age2, &Weight2, &Height2);
printf("Enter the age, weight and height of student 3: ");
scanf ("%d %f %f",&Age3, &Weight3, &Height3);
printf("\t\tAge\t\tWeight\t\tHeight\n");
printf("Student1\t%d\t\t%.1f\t\t%.1f\n",Age1,Weight1,Height1);
printf("Student2\t%d\t\t%.1f\t\t%.1f\n",Age2,Weight2,Height2);
printf("Student3\t%d\t\t%.1f\t\t%.1f",Age3,Weight3,Height3);
}

Computer & Information Technology for HKCEE 2 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(U02C09L02Q003)
Declare the variables in the following statements and rewrite the statements as C statements.
(a) Integer X is divided by integer Y and the remainder is stored in floating-number variable R.
(b) Assign the result of integer M multiplying 5 to floating-number variable P.
(c) String variable Tel is assigned to be ‘23423423’.
(d) Prompt a user to input a number and store the number in variable integer NUM. Then store the
square root of the input number in floating-number variable SquareRoot.
(e) The variable C holds the value of the circumference. It is calculated by multiplying the
floating-number constant PI of value 3.14 to the diameter D, which is an integer variable.
(18 marks)
Answers
(a) float R;
int X, Y;
R = X%Y;

(b) int M;
float P;
P = M * 5;

(c) int Tel[] = {"23423423"};

(d) #include <math.h>


void main( ){
int NUM;
float SquareRoot;
printf("Input a number: ");
scanf("%d ",&NUM);
SquareRoot = sqrt(NUM);

(e) #define PI 3.14;


void main( ){
float C;
int D;
C = D * PI;

Computer & Information Technology for HKCEE 3 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(U02C09L02Q004)
In a series of numbers, the values of the first and the second terms are 0 and 1 respectively while the
value of any other term is calculated by adding the two previous terms. So, the first 10 terms are as
follows:
0,1,1,2,3,5,8,13,21,34,…
Write a program to find the value of any term for an input term number.

Sample output:
Enter a positive term number: -5
Non-positive number! Reenter: 0
Non-positive number! Reenter: 10
The value of term 10 is 34
(14 marks)
Answers
#include <stdio.h>
void main( ){
float Tn, Tn1, Tn2;
int Term, n;
printf("Enter a positive term number: ");
scanf("%d", &Term);
while(Term < 1){
printf("Non-positive number! Reenter: ");
scanf("%d", &Term);
}
if (Term == 1)
Tn = 0;
else if (Term == 2)
                  Tn = 1;
                  else{
                            Tn1 = 0;
                            Tn2 = 1;
                            for (n=3;n<Term+1;n++){ 
                                    Tn = Tn1 + Tn2;
                                    Tn1 = Tn2;
                                    Tn2 = Tn;
                                }
                         }
    printf("The value of term %d is %.0f", Term, Tn);
}
Computer & Information Technology for HKCEE 4 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(U02C09L02Q005)
Given that Stack is an array of string. Top is an integer variable. Item is a string variable. The
contents of Stack[1], Stack[2],Stack[3],Stack[4]and Stack[5]are 'Peter',
'John' , 'Mary' , 'Joyce' and 'Leon' respectively. The content of Top is 5。

(a) Refer to the function ListStack below. What is the output if the function ListStack is
executed?
void ListStack(void){
   int i;
   for(i=Top–1;i>0;i­­)
      printf("%10s\n",Stack[i]);
   printf("­­­­­­­­­­­­\n");
}

(b) Refer to the function Push below.


void Push(char Item[10]){
   strncpy(Stack[Top],Item,sizeof(Stack[Top]));
   Top++;
}
What is the output if the following statements are executed?

Push("Tim ");
ListStack( );

(c) Refer to the function Pop below.


void Pop(void){
char Item[10];
   strncpy(Item, Stack[Top], sizeof(Stack[Top]));
   Top­–;
}

What is the output if the following statements are executed?



Pop( );
ListStack( );

Computer & Information Technology for HKCEE 5 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(d) What is the output if the following statements are executed?



Push("XXX");
Pop( );
Pop( );
Push("YYY");
Push("ZZZ");
ListStack( );

(8 marks)
Answers
(a)  Leon 
 Joyce 
 Mary 
 John 
------------
(b)  Tim 
 Leon 
 Joyce 
 Mary 
 John 
------------

(c)  Joyce 
 Mary 
 John 
------------

(d)  ZZZ 
 YYY 
 Joyce 
 Mary 
 John 
------------

Computer & Information Technology for HKCEE 6 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(U02C09L02Q006)
BranchName, Manager, NumOfStaff and Profit are 4 parallel arrays. They store the
information of 12 branches of a company.
(a) Write a function ListBranch that lists out the details of all the branches.
(b) Write a function FindManager that receives a branch name from users and then finds out the
manager of this branch. If the input branch is not present, the function will display a suitable
message.
(c) Write the function MostStaff that finds out the branch with the greatest number of staff.
(d) Write the function ProfitMoreThan that lists out the branch names and profit gained for
those branches with profit more than or equal to the input amount.
(28 marks)
Answers
(a) void ListBranch(void){
   int i;
   printf("Branch\t\tManager\t\tStaff Number\t\tProfit\n");
   for(i=0;i<12;i++)
   printf("%s\t\t%s\t\t%d\t\t%.2f\n", 
BranchName[i], Manager[i], NumOfStaff[i], Profit[i]);
}

(b) void FindManager(void){
   int i;
   char Branch[20];
   printf("Enter branch name: ");
   scanf("%s", Branch);
   i = 0;
   do{
   if(strcmp(BranchName[i],Branch)==0){
      printf("The manager of %s Branch is %s",
BranchName[i], Manager[i]);
  return;
}
i++;
}while(i<12);
   printf("The branch is not present!");
   return;
}

Computer & Information Technology for HKCEE 7 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(c) void MostStaff(void){


int i, Greatest=1;
for(i=1;i<11;i++)
if(NumOfStaff[i]>NumOfStaff[Greatest])
Greatest = i;
printf("%s has the most staff.",
BranchName[Greatest]);
}
(d) void ProfitMoreThan(void){
float Amount;
int i;
printf("Enter an amount: ");
scanf("%f", &Amount);
printf("Branch Name\t\tProfit\n");
for(i=0;i<12;i++)
if(Profit[i] >= Amount)
printf("%s\t\t%.2f\n",BranchName[i],Profit[i]);
}

Computer & Information Technology for HKCEE 8 © Pearson Education Asia Limited 2004
(Module A1)
Chapter 9 Formulation of Algorithms Question Bank

(U02C09L02Q007)
A and B are two arrays of type integer sorted in ascending order. Array A has m elements and array
B has n elements. The function InvertMerge merges the two arrays in descending order and
stores the result in array C. Write the function InvertMerge.
(18 marks)
Answers
void InvertMerge(void){
int i, j, k=0;
i = m-1;
j = n-1;
while((i > -1)&&(j > -1))
if(A[i] > B[j]){
C[k]=A[i];
k++;
i--;
}
else{
C[k]=B[j];
k++;
j--;
}
if(i > 0)
while(i >-1){
C[k]=A[i];
k++;
i--;
}
else while(j >-1){
C[k]=B[j];
k++;
j--;
}
}

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

Das könnte Ihnen auch gefallen