```Information and Computer Science Department
Spring Semester 132
ICS 103 – Computer Programming in C
Midterm Exam key
Thursday, April 03, 2014
Duration: 120 minutes
Name:
ID#:
Section#:
Instructor:
Question #
Maximum
1
4
2
12
3
36
4
15
5
10
6
8
7
15
Total
100
Obtained
ICS103
Midterm Exam Key
Page # 2 out of 9
Question # 1 [4 points]
Fill in the circles with the software used in developing a high-level language program:
Question # 2 [12 points]
Apply the software development method to find the volume and surface area of a sphere given its radius.
4
= 3  3   = 4 2 , where r is the radius and π=3.14159
Note: Apply the first four steps ending with a complete C program.
Problem:
Find the volume and surface area of a sphere given its radius.
Analysis:
Problem Constant
PI 3.14159
Problem Input
Problem Output
double volume, surface_area
ICS103
Midterm Exam Key
Relevant Formula
4
= 3  3   = 4 2 , where r is the radius and π=3.14159
Design:
Algorithm
2. Compute the volume
a. Assign 4/3 * PI * radius ^ 3 to the volume
3. Compute the surface area
a. Assign 4 * PI * radius ^ 2 to the surface area
4. Display the volume and the surface area
Implementation:
/* Calculating the Volume and the Surface Area of a Sphere */
#include <stdio.h>
#define PI 3.14159
int main(void)
{
double volume; /* output - volume */
double surface_area; /* output - surface area */
/* Calculate the volume */
/* Calculate the surface area */
/* Display the volume and surface area */
printf("The volume is %.2f\n", volume);
printf("The surface_area is %.2f\n", surface_area);
return (0);
}
Page # 3 out of 9
ICS103
Midterm Exam Key
Page # 4 out of 9
Question # 3 [36 points]
Identify the error(s), if any, in each of the following code fragments. If a fragment has no errors, write its output.
[Note: No explanation of error(s) is required].
Code Fragment
Output
1 mark
int x = 3;
8
x = x * x – x / x;
printf("%d", x);
int a, b, c, x;
x = 1;
a = 77;
b = 10;
c = 11;
x = a % b;
printf("%d ", x);
x = a / b;
printf("%d ", x);
x = b % a;
printf("%d ", x);
x = b / a;
printf("%d ", x);
double x=1234.5678;
int y=77;
printf("%.1f%d\n", x, y);
printf("%1.1f%2d\n", x, y);
printf("%4.2f%3d\n", x, y);
printf("%7.3f%4d\n", x, y);
printf("%9.3f%4d\n", x, y);
double x = 10.4, y;
int m = 2 , n = 7 ;
y = x / m;
printf("%.1f\n", y);
y = n / m;
printf("%.1f\n", y);
int x = 12;
if(x > 5)
printf("A");
if(x > 6)
printf("B");
if(x > 12)
printf("C");
else if(x > 8)
printf("D");
else if(x > 4)
printf("E");
else
printf("F");
int x = 10;
if (x > 15)
x = 0;
printf(“%d”, x);
else
printf(“%d”, x + 5);
4 marks
7 7 10 0
5 marks
1 2 3
1 2 3
1 2 3
1 2 3
1 2
4
4
4
4
3
2 marks
5.2
3.0
3 marks
ABD
2 marks
Else without if
.
.
.
.
4
6
6
5
5
.
7
7
7
6
5
7
7
7
8
6
7
7
8
7
7
7
ICS103
Midterm Exam Key
Page # 5 out of 9
Code Fragment
int x;
scanf(“%d”, &x);
switch(x){
case 1:
x = x +
break;
case 3:
x = x +
case 5:
if(x ==
x
case 6:
x = x +
break;
default:
x = x –
}
printf(“%d”, x);
Output
3 marks
When x is 1
2
1;
2;
4)
= x + 6;
3;
1;
int i, j;
i = 3;
while (i < 7){
for(j = 5; j >= i; j = j-2) {
printf("%d ", i + j);
}
printf("\n");
i = i + 3;
}
printf("%d %d\n", i, j);
int i,j,count = 0;
for(i = 3; i != 5; i +=2)
for(j = 3; j > i; j = j-2)
count++;
printf("%d %d %d\n", i, j,count);
int i, j;
for (i = 1; i <= 5; i++){
for (j = 1; j <= i; j++)
printf("%d",j);
for (j = i; j <= 5; j++)
printf("%d",j);
printf("\n");
}
#include <stdio.h>
int f1(int x);
int main()
{
int k = 1,m = 6;
printf("%d %d \n",f1(k),f1(m));
return 0;
}
int f1(int x)
{
if (x <= 2)
return 2;
else
return 2*(x-1);
}
When x is 2
1
When x is 3
8
4 marks
8 6
9
5
3 marks
5 3 0
5 marks
112345
122345
123345
123445
123455
4 marks
2 10
ICS103
Midterm Exam Key
Page # 6 out of 9
Question # 4 [15 points]
In each semester, a private University charges 2000 Saudi Riyals per course for each of the first four courses a
student takes. For each course in excess of 4, the charge is 1500 per course. Write a C program that prompts for
and reads the number of courses a student takes in a semester; it then displays the total charge to be paid. Your
program must display an appropriate error message if the entered number of courses is zero or negative.
Sample program runs:
Note: Your program must be general and not specific to the given sample runs.
#include <stdio.h>
#define CHARGE1 2000
#define CHARGE2 1500
int main(void){
int numCourses, charge;
printf("Enter number of courses: ");
scanf("%d", &numCourses);
if(numCourses <= 0)
printf("Error: Invalid number of courses\n");
else{
if(numCourses <= 4)
charge = numCourses * CHARGE1;
else
charge = 4 * CHARGE1 + (numCourses - 4)*CHARGE2;
printf("Total charge = %d Saudi Riyals\n", charge) ;
}
return 0;
}
ICS103
Midterm Exam Key
Page # 7 out of 9
Question # 5 [10 points]
Write a C program that asks the user to enter an integer number n and displays the multiplication table for
numbers 1 to n. Display each number in 3 places. The output of your program should be as follows for n = 5:
#include <stdio.h>
int main ()
{
int n, i, j;
printf("Enter a number: ");
scanf("%d", &n);
printf("
");
for (i = 1; i <= n; i++)
printf("%3d",i);
printf("\n");
for (i = 1; i <= n; i++){
printf("%3d", i);
for (j = 1; j <= n; j++)
printf("%3d", i*j);
printf("\n");
}
return 0;
}
ICS103
Midterm Exam Key
Page # 8 out of 9
Question # 6 [8 points]
Write a C program that computes the following sum based on the value of x input by the user.
=10
= ∑
=1

2 − 1
#include <stdio.h>
#include <math.h>
int main(){
double x, sum, i;
sum = 0;
printf("Enter value of x >");
scanf("%lf", &x);
for(i = 1; i <= 10; i = i + 1)
sum = sum + pow(x, i) / (2 * i - 1);
printf("sum =%.2f\n", sum);
return 0;
}
ICS103
Midterm Exam Key
Page # 9 out of 9
Question # 7 [15 points]
The body mass index (BMI) is a measure for human body shape based on an individual's weight and height. It is a
simple method to assess how much an individual's body weight departs from what is normal. It can be measure by
the formula:
BMI = (weight in kg) / (height in m)2
Depending on the value of BMI, a person can be categorized in different weight ranges as given in the table below.
BMI (kg/m2)
Weight Range
Less than 18.5
Underweight
From 18.5 to 24.9 Normal
From 25 to 29.9
Overweight
30 and more
Obese
Write a complete C language program using a function bmi_calc to calculate BMI. Ask the user about height and
weight in the main function. Print a message to the user showing him weight in kg, height in m, BMI and the weight
range category as shown in the image.
#include <stdio.h>
double bmi_calc (double weight, double height);
int main(){
double w, h, bmi;
/* get the input weight from the user */
scanf("%lf", &w);
/* get the input height from the user */
scanf("%lf", &h);
bmi = bmi_calc (w, h);
/* print the result */
if (bmi < 18.5)
printf("Category: Underweight\n\n");
else if (bmi < 25)
printf("Category: Normal\n\n");
else if (bmi < 30)
printf("Category: Overweight\n\n");
else
printf("Category: Obese\n\n");
return 0;
}
double bmi_calc (double weight, double height){
/* bmi calculation */
return (weight)/(height * height);
}
```