0% found this document useful (0 votes)
49 views6 pages

EP100 MidSemesterTest S12010 Ans

The document contains a C program that [1] prompts the user to enter a radius value for a circle, [2] checks that the radius is positive, [3] prompts the user to select the units for displaying the circumference and area from meters, centimeters, or millimeters, [4] checks that a valid unit is selected, and [5] calculates and displays the circumference and area results based on the provided radius and selected units. The program implements input validation, calculations for circumference and area of a circle, and output formatting based on user-selected units.

Uploaded by

Francis Gomes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

EP100 MidSemesterTest S12010 Ans

The document contains a C program that [1] prompts the user to enter a radius value for a circle, [2] checks that the radius is positive, [3] prompts the user to select the units for displaying the circumference and area from meters, centimeters, or millimeters, [4] checks that a valid unit is selected, and [5] calculates and displays the circumference and area results based on the provided radius and selected units. The program implements input validation, calculations for circumference and area of a circle, and output formatting based on user-selected units.

Uploaded by

Francis Gomes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Fundamentals

1. The following C program contains 5 (five) syntax errors. State the line numbers of
where the errors occur (1 mark each) and rectify these errors (1 mark each).
[10 marks]

10 /* Author : Tele Tan */


20 /* Date : 29 Mar 2010 */
30 /* This program ask the user to enter an angle in degrees */
40 /* and computes the sine and cosine of this angle */
50 /* before printing the results */
60
70 #include <stdio.h>
80 #include <maths.h>
90
100 #define PIE 3.1415
110
120 int main()
130
140 float angle;
150 float sine, cosine;
160
170 printf("Enter an angle in degrees : ");
180 scanf("%f",angle);
190
200 sine=sin(angle*PIE/180.0);
210 cosine=cosine(angle*PIE/180.0);
220
230 printf("The sine of %.2f is equals to %.2f\n",angle,sine);
240 printf("The cosine of %.2f is equals to
%.2f\n",angle,cosine);
250
260 return 0
270
280 }

Ans:

10 /* Author : Tele Tan */


20 /* Date : 29 Mar 2010 */
30 /* This program ask the user to enter an angle in degrees */
40 /* and computes the sine and cosine of this angle */
50 /* before printing the results */
60
70 #include <stdio.h>
80 #include <maths.h> - #include <math.h>
90
100 #define PIE 3.1415
110
120 int main()
130 - missing {
140 float angle;
150 float sine, cosine;
160
170 printf("Enter an angle in degrees : ");
180 scanf("%f",angle); - scanf(“%f”,&angle)
190
200 sine=sin(angle*PIE/180.0);
210 cosine=cosine(angle*PIE/180.0); - cosine -> cos
220
230 printf("The sine of %.2f is equals to %.2f\n",angle,sine);
240 printf("The cosine of %.2f is equals to
%.2f\n",angle,cosine);
250
260 return 0 - return 0;
270
280 }
Design

2. Explain what are these errors in a C program (3 marks each):


a. Syntax error.
b. Logical error.
[6 marks]
Ans:

a. Syntax error is an error caused by wrong syntax written in program

b. Logical error is an error caused by improper logic in code for any operation
Control and Repetition

3. Someone asked you to write a C Program to compute and display the


circumference and area of a circle given an inputted radius value of the circle. The
program must have the following behaviour.

The program should display on the screen a prompt for the radius in metres. The user
should enter this radius value at the keyboard, from which the program should read it.
The program should than check if the radius value is valid (i.e. positive values). If the
value is not valid, it shall continuously prompt the user until a valid value is entered.
The program should than ask for the units of the displayed results for the
circumference and area. The selectable units are metres (m and m2), centimetres (cm
and cm2) and millimetres (mm and mm2). The user should than enter one of this
selection at the keyboard, from which the program should read it. The program should
than check if the selection is valid. If it is not valid, it shall continuously prompt the
user until a valid selection is entered. The program should than proceed to compute
the circumference and area of the circle given the radius value and display the results
according to the selected unit.

Write the entire C program to implement this.


[24 marks]
Ans

/* Author : Tele Tan */


/* Date : 29 Mar 2010 */
/* This program calculates the area and circumference */
/* of a circle given its radius in (m). The program */
/* will ask for the units in either m, cm or mm. */

#include <stdio.h> // 2 mark for headers


#include <math.h>

#define PI 3.1415 // 2 mark for constant

int main()
{
float radius; // 2 marks for variable with correct data types
float area, circumference;
int unit;
float conversion_m_cm=100; // 2 marks for conversion terms
float conversion_m_mm=1000;

radius=-1; // 4 marks for radius prompting


while(radius <= 0.0)
{
printf("Enter the radius of the circle in metres : ");
scanf("%f",&radius);
if(radius <= 0.0)
printf("error, radius must be non zero and +ve\n");
}
unit=-1; // 4 marks for unit selection prompting
while((unit <1)||(unit>3))
{
printf("What output units? (Select 1 for m, 2 for cm or 3 for mm : ");
scanf("%d",&unit);
if((unit <1)||(unit>3))
printf("error, unit selection must be between 1 and 3\n");
}

switch(unit) // 2 marks for switch or if-elseif


{
case 1: // 2 marks for case 1
circumference=2*PI*radius;
area=PI*pow(radius,2);
printf("The circumference is %.2f m\n",circumference);
printf("The area is %.2f m^2\n",area);
break;
case 2: // 2 marks for case 2
circumference=2*PI*(radius*conversion_m_cm);
area=PI*pow(radius*conversion_m_cm,2);
printf("The circumference is %.2f cm\n",circumference);
printf("The area is %.2f cm^2\n",area);
break;
case 3: // 2 marks for case 3
circumference=2*PI*(radius*conversion_m_mm);
area=PI*pow(radius*conversion_m_mm,2);
printf("The circumference is %.2f mm\n",circumference);
printf("The area is %.2f mm^2\n",area);
break;
}

return 0;

You might also like