0% found this document useful (0 votes)
594 views7 pages

HEXCO Past Exam Questions

The document contains several C programming exam questions and their corresponding solutions. The programs cover various topics such as finding the highest and lowest numbers, counting letter frequencies, counting words and lines in text, conducting an election vote count, and calculating areas of different shapes using functions. Each program is accompanied by comments detailing the programmer's name and purpose of the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
594 views7 pages

HEXCO Past Exam Questions

The document contains several C programming exam questions and their corresponding solutions. The programs cover various topics such as finding the highest and lowest numbers, counting letter frequencies, counting words and lines in text, conducting an election vote count, and calculating areas of different shapes using functions. Each program is accompanied by comments detailing the programmer's name and purpose of the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

HEXCO Past Exam Questions

Question

Write a C program that can accept 10 numbers between 1 and 100. If values outside
the range are entered an error message should be displayed and allow the user to enter
a correct value. It should then find the highest value entered and lowest value entered.

Answer
#include <stdio.h>
/*
The program to find the highest and lowest number entered.
Programmer: Tinashe Gumbochuma.
*/
void main()
{
int high,low,num,i;
int verify();
clrscr();
printf("Enter first value \n");
scanf("%d",&num);
if((num<0)||(num>100))
num= verify(num);
high=low=num;
for(i=1;i<=9;i++)
{
printf("Enter next value \n");
scanf("%d",&num);
if((num<0)||(num>100))
num=verify(num);
if(num>high)
high=num;
if (num<low)
low=num;
}
printf("The highest value is %d and lowest is %d",high,low);

verify(val)
{
while((val>100)||(val<0))
{
printf("Number out of range, should be between 0 and 100\n");
printf("Please try again \n");
scanf("%d",&val);

}
return(val);
}

Question

Write a C program which accumulates the frequency of occurence of each letter of the
alphabet in its input data and which outputs the frequency of each letter as a
percentage of the total number read. Terminate using a period.

Answer
#include <stdio.h>
#include <ctype.h>
#define STOP '.'

// Program to count the frequence of occurance of each


// character entered and express it as a percentage of the
// total number of characters entered.
// PROGRAMMER: Tinashe Gumbochuma.

char
arr1[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z'};

main()
{
int i,arr2[26],tot=0;
char ch;
float per;

for (i=0;i<=26;i++)
arr2[i]=0;
printf("Enter line terminate with full stop.\n");
ch = 'Y';
while (ch!=STOP)
{
ch=getchar();
ch=(toupper(ch));
if (ch!=STOP)
{
if(isalpha(ch))
tot++;
}
for(i=0;i<=26;i++)
{
if(ch==arr1[i])
arr2[i]++;
}
} //while
for(i=0;i<=25;i++)
{
if(arr2[i])
{
printf("%c - ",arr1[i]);
printf("%d ... ",arr2[i]);
per=(((float)arr2[i]/(float)(tot))*100.0);
printf("%.2f\n",per);

}
}
printf("Total chars is %d\n",tot);
return(0);
}

Question

A piece of text consists of a character sequence spanning a number of lines and


terminated by a period symbol. Count the number of words and lines. A word is
defined as a character string delimited by white space symbols (space, tab,newline).

Answer
#include <stdio.h>
#define STOP '.'
// Progam to count the number of words and lines entered.
// Programmer: Tinashe Gumbochuma
// HND Computer Studies - Harare Polytechnic
main()
{
char sent;
int w_count=0,line_count=1,i;
printf("Enter your line, terminate with a full stop \n");
sent='N';
while (sent != STOP)
{
sent=getchar();

if (sent == ' ')


w_count++;
if (sent == '\n')
{
line_count++;
w_count++;
}
}
w_count++;
printf("The number of words is %d \n",w_count);
printf("The number of lines is %d ",line_count);
return 0;
}
Question

The national examinations intents to choose the chief coodinator and the post is
contested by 6 candindates numbered from 1 to 6. Voting is done by marking the
candidate number on the ballot screen. If a number outside the range 1-6 is read, the
ballot is counted as spoilt. The program should display the winner and number of
spoils when voting is through.

Answer
#include<stdio.h>
/*
The program to count number of votes cast on six candidates. It
declares the winner and the number of spoilt ballots.
Past Exam Question November 2001.
Programmer: Tinashe Gumbochuma.
Date: 29 January 2002.
*/
main()
{
int votes[6],spoils=0,winner,i,vote,num_votes,j;
clrscr();
for(i=0;i<=5;i++)
votes[i]=0;
printf("How many people are to vote\n");
scanf("%d",&num_votes);
for(j=1;j<=num_votes;j++)
{
printf("Enter number for your candidate 1-6 ");
scanf("%d",&vote);
switch(vote)
{
case 1: votes[0]++; break;
case 2: votes[1]++; break;
case 3: votes[2]++; break;
case 4: votes[3]++; break;
case 5: votes[4]++; break;
case 6: votes[5]++; break;
default: spoils++;
}
}
winner=0;
for(i=1;i<=5;i++)
if(votes[i]>votes[winner])
winner=i;
winner++;
printf("ELECTION RESULTS .....\n");
for(i=0;i<=5;i++)
printf("Candate %d : %d votes\n",i+1, votes[i]);
printf("The winner of the election is %d\n",winner);
printf("Spoilt ballots are %d\n", spoils);
getch();
return(0);
}

Passing Values To Functions

The following program illustrates how to pass values to functions. It uses function
prototyping
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#define PI 3.141592
#define STOP 'Q'
// This program demonstrates the use of functions.
// It calculates areas of different shapes.
// Programmer: Tinashe Gumbochuma.
main()
{
char menu();
char code;
float square(float);
float rect(float,float);
float circle(float);
float tri(float, float);
float sq,rec_width,rec_length,radius,base,height;

do
{
clrscr();
code=menu();
switch (code)
{
case 'A':
{
printf("\nEnter length for square ");
scanf("%f",& sq);
printf("\nThe area for the square is %.2f \n",square(sq));
printf("\n");
getch();
break;
}
case 'B':
{
printf("\nEnter length of rectangle ");
scanf("%f",&rec_length);
printf("\nEnter width of rectangle ");
scanf("%f",&rec_width);
printf("\nArea of rectangle is %.2f \n",
rect(rec_length,rec_width));
printf("\n");
getch();
break;
}
case 'C':
{
printf("\nEnter radius of circle ");
scanf("%f",&radius);
printf("\nThe area of the circle is %.2f\n",circle(radius));
printf("\n");
getch();
break;
}
case 'D':
{
printf("\nEnter base of triangle ");
scanf("%f",&base);
printf("\nEnter height of triangle ");
scanf("%f",&height);
printf("\nThe area of a triangle is %.2f\n",
tri(base,height));
printf("\n");
getch();
break;
}
case 'Q':
{
printf("\nPROGRAM TERMINATED\n");
break;
}
default:
{
printf("\n");
printf("Invalid Option \n");
printf("\n");

}
}while (code != STOP);
return 0;
} // main prog

float square(float length)


{
float area;
area = length*length;
return(area);
}

float rect(float l,float w)


{
float area;
area = l*w;
return(area);
}

float circle(float radius)


{
float area;
area = PI*radius*radius;
return (area);
}

float tri(float b,float h)


{
float area;
area = 0.5*b*h;
return(area);
}

char menu()
{
char code;
printf(" M a i n M e n u \n");
printf("\n");
printf("A. Square \n");
printf("B. Rectangle \n");
printf("C. Circle \n");
printf("D. Triangle \n");
printf("Q. Quit program \n");
printf("Select Choice ");
code = getchar();
code = toupper(code);
return(code);
}

You might also like