Structured Programming, 2nd Year All
Structured Programming, 2nd Year All
(2) Indicate which of the following are valid type int, double, or char, and which are
not:
)a 14 b) 15.0 c) ‘*’
d) $ e) -999 f) 0.123
g) ‘R’ h) “R” L) ‘Hello’
m) 32e-4 n) ‘-5’ p) 0.0
(3) Show the output displayed by the following (assume exp = 5):
printf(“My name is Ali \n and I live in “);
printf(“Cairo, Egypt \n and I have “);
printf(“ %d years of programming experience. “, exp );
(6) Write a two-statement program that will generate the following output:
Mr. Ahmed is 42,
Mr. Kamel is 48.
Use string constants to represent the names and integer constants to represent the ages.
1
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2007-2008
(4) What is the value of the variable f after executing the following assignment
statements:
a) F = 7 * 10 – 5 % 3 * 4 + 9
b) f = ( 7 * ( 10 – 5 ) % 3 ) * 4 + 9
(5) for the following code, find the values of loCost and hiCost:
float loCost, hiCost;
loCost= 22.342; hiCost= 22.348;
loCost = (float) ( (int) ( loCost*100.0+0.5) ) / 100.0;
hiCost = (float) ( (int) ( hiCost*100.0+0.5) ) / 100.0;
2
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2007-2008
(8) Write a program that takes (from the keyboard) the length and width of a piece of
land, and the length and width of a house to be built inside it and then tells you how much
grass you need to buy to cover the garden (in square meters).
(10) Write a program that asks a student for his grade ( out of 100) in 3 exams and then print
out his final grade (out of 100), given that the weight of the first exam is 30%, the second
30%, and the third 40%
(11) Write a program that accepts (from the keyboard) two numbers X and N, evaluates and
displays the value of XN.
(12) Write a computer program that computes the duration of a projectile’s flight and its
height when it reaches the target.
Program inputs (from the keyboard)
double theta /* firing angle*/
double distance /* distance to target */
double velocity /* firing velocity in m/sec */
Program outputs (to the screen)
double time /* time of flight */
double height /* height at impact */
Relevant Formulas
flight time
dis tan ce g .time 2
height velocity. sin( ).time
velocity.cos( ) 2
(13) Write C program that accepts 4 numbers and returns their "RMS value".
3
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2007-2008
(14) Write a C program that takes a temperature in centigrade scale and displays the
corresponding Fahrenheit value, where:
Fahrenheit = 32 + (centigrade * 9 )/5
(15) Write a program to help handling of personnel data. The program should ask for user
age, current monthly salary, and the number of years already worked. The program should
calculate: the number of years left till retirement (65 years), the maximum number of years
the user may work before retirement (including years already worked), and the annual
pensions (one-fifth of the current annual salary for each complete year of service).
(16) Write a program that takes (from the keyboard) a float variable with a decimal
fraction and displays the integer number and the fraction number separately.
4
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(3) Write a C program that takes in two numbers and returns their maximum using two
different techniques:
a) if – else b) conditional operator
(4) Write a C program that takes in three numbers and returns their maximum.
(6) Write a program to simulate a police radar gun. The program should take an
automobile speed and display the message SPEEDING if the speed exceeds 100
Km/hr.
5
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(9) A factory divides its workers into 3 skill levels. Unskilled workers receive
L.E.8.15 per hour. Semiskilled workers receive L.E.12.55 per hour. Skilled
workers receive L.E.18.6 per hour. Write a program to calculate and display a
worker’s weekly pay formatted to 2 decimal places (based on 8 hours for 6 days of
work). Your program input should consist of the hours worked and a skill level
indicator. Solve using two different techniques:
a) if - else b) switch - case
(10) Write a C program that accepts worked hours, normal rate, over time rate for each
employee and calculates the tax and net pay for an employee according to the following:
Gross pay = worked hours × normal rate (if worked hours are up to 40 hours per week)
Gross pay = 40 × normal rate + (worked hours – 40) * overtime rate (other wise).
The tax is calculated according to the following table:
Gross pay Tax rate
0 to 500 3%
500 to 1000 5%
1000 to 1500 8%
> 1500 10 %
6
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(2) The Earthquake information center has asked you to write a program implementing the following
decision table to characterize an earthquake based on its Richter scale number.
Richter scale (n) Characterization
n < 5. 0 Little or no damage
5.0 ≤ n < 5.5 Some damage
5 . 5 ≤ n < 6. 5 Serious damage: walls may crack or fall
6. 5 ≤ n < 7 . 5 Disaster: houses and buildings may collapse
Higher Catastrophe: most buildings destroyed
Write the program to take the value of n and display the corresponding message
(3) Write a C program that displays the tax and final salary based on the following:
Salary Base Tax (%)
0 to 149.99 0.0
150 to 299.99 1.0
300 to 499.99 2.25
500 to 799.99 5.0
(4) Write a C program that calculates bills for the Electricity company. There are 3
types of customers: residential (code R), commercial (code C), and industrial (code I).
For a code R customer, the bill is 10 L.E plus 0.05 L.E for each kilowatt
used.
7
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
For a code C customer, the bill is 1000 L.E for the first 2000 kilowatt, and
0.005 L.E for each additional kilowatt used.
For a code I customer, the bill is 1000 L.E if he used less than 4000 kilowatt,
2000 L.E if he used between 4000 and 10000 kilowatt, or 3000 L.E if he
used more than 10000 kilowatt.
The inputs of the program should be the type of customer (R, C or I) and the kilowatts
used. The output should be the amount of money the customer has to pay (use switch
– case statement).
(5) Write a C program that takes the degree of the student in the exam, and then prints
out his grade based on the following:
Degree Grade
85 to 100 Excellent
75 to 85 Very Good
65 to 75 Good
50 to 65 Sufficient
Less than 50 Fail
8
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(2) During the execution of the following program segment, how many lines of the
asterisks are displayed?
for (I=0; I<10; ++I)
for (j=0; j<4; ++j)
printf(“*******\n”);
(4) Write a program that will count characters in a phrase typed in by the user until a
period ‘.’ is typed.
(5) Write a C program to count the number of characters; number of digits and
number of words in a phase you typed until pressing <Enter>.
9
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(7) Write a program that prints the squares of all numbers from 1 to 20.
(8) Write a C program to compute the letter grades of a class of N students. Read the
student number as well as M test scores. Compute the average of each student and assign
a letter grade to each student according to the following limits:
A (90 : 100) ; B (80 : 89) ; C (70 : 79) ; D (60 : 69) ; F (below 60).
(10) Write a C program to print out the first 100 prime numbers.
(11) Write a C program that calculates the average score for a number of students and
displays the number of excellent and failed students; the program should run until a score
of zero is typed in.
(12) Write a program that finds the equivalent parallel resistance for a collection of
resistors values.
Your program should scan first the number of resistors N and then read all the resistor
values. The program should compute and display the equivalent parallel resistance by the
formula:
1
Req =
1 1 1 1
+ + + .... +
R1 R2 R3 RN
10
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(13) Write a C program that estimates the approximate value of π based on the infinite
series
4 4 4 4 4
π 4 ........
3 5 7 9 11
The program should print a table showing the value of π after each of the first 100 terms
of this series.
11
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(2) What is the output value of the variables x and y of program A and program B?
/* Program A /* Program B
void simple (int x); void simple (int *x);
int main (void) int main (void)
{ int x, y ; { int x, y ;
x = 5 ; y=6 ; x = 5 ; y=6 ;
simple (x) ; simple (&x) ;
simple (y) ; simple (&y) ;
printf(“x=%d, y=%d”,x,y) ; printf(“x=%d, y=%d”,x,y) ;
} }
void simple (int x) void simple (int *x)
{ {
int y ; int y ;
y = x+2 ; y = *x+2 ;
x = x*2 ; *x = *x*2 ;
} }
(3) Trace the following code, showing its output on the screen :
#include <stdio.h>
int fun1 (int, int);
void fun2 (int, int);
int main ()
{ int a=2 ; int b= 2 ; int c=3;
b=fun1(a,c) ;
printf(“a=%d b=%d c=%d\n”,a,b,c);
fun2(a,b);
printf(“a=%d b=%d c=%d\n”,a,b,c);
}
int fun1(int x, int y)
{ int z= x*y ; return z ;
}
int fun2(int p, int q)
{ int r=p*p + q*q ;
printf( “ The value is %d\n”, r );
return;
}
12
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(4) Write a prototype for a function sum_n_avg that has three type double input
parameters n1, n2, n3 and two output parameters: sump and avgp.
(5) Write a program that prints out the larger of two numbers entered from the
keyboard. Use a function to do the actual comparison of the two numbers. Pass the two
numbers to the function as arguments, and have the function return the answer with
return().
(7) Write a function that accepts an integer number and returns its factorial.
(8) Given the lengths a, b, c of the sides of a triangle (all in double format), write a
program to compute the area A of the triangle. The formula for computing A is given by
A s s a s b s c
abc
where s is the semi perimeter of the triangle s
2
Use a function to take the values of a, b, and c from the user. Use another function to
perform the calculations. The main function should display the area of the triangle.
13
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(3) Write a C program that reads N integers and determines and prints the largest and
the smallest integer in the group.
(4) Write a program that accepts temperatures of a week's days then stores, sorts, and
displays the sorted list on the form:
Day 3 37C
Day 1 34C
Day 4 33.5C
14
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(5) A shop needs to keep an inventory of its 100 items for sale. Write a program that
uses parallel arrays to input the item code, price and quantity of each item. Then the
program should display a neat table of all items whose price > 100 pounds, then it
displays another table of all items whose quantity is less than 10 items.
(6) Write a C program that accepts a set of characters and returns their reverse
(forward and backward) using functions. For example:
Input: "This is a string."
Output: ".gnirts a si sihT"
(7) Write a C program that reads 200 numbers and then prints out their sum, average,
minimum, and maximum.
(8) Write a C program that reads a set of positive scores and stores them in one-
dimensional array. The program should print max, min elements in that array and print
out the sorted list of the array. Your program should include:
a) a function to read the list of scores.
b) a function to find maximum and minimum scores.
c) a function to sort the list of scores.
d) a function to print the sorted list.
(9) A palindrome is a number or a text phrase that reads the same (backwards and
forwards); for example; 12321, 11611 etc. Write a C program that reads in five-digit
integer and determines whether or not it is a palindrome.
(10) Write a C program that accepts two dimensional array A, and B, then calculates the
transpose and multiplication of them.
15
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007
(11) Write a C - program to accept and store the data of 100 students; each consists of:
- score of the student - identification number of the student.
The program should calculate and display the following:
1- average score.
2- The ID and the score of the student who has got the highest score.
3- The count and a list of ID’s of students who got a grade of very good.
(12) the Discrete Fourier Transform (DFT) has the two parts: a real matrix R and an
2πkn
imaginary matrix I of the size M-by-M. the matrix R has the values of cos
M
, while
2πkn
the matrix I has the values of sin
M
where k=0,1,2,….,M-1represents the rows of the
matrix and n=0,1,2,….,M-1 represents the columns of the matrix. Write a program to read
from the user the value of M and construct the two matrices R and I .
(13) Write a C program that accepts two input strings str1 and str2. The program
should be capable of doing the following tasks:
a) Compare the two strings.
b) Concatenate both strings.
c) Copy string str1 into X.
d) Copy 8 characters from str2 into Y.
e) Concatenate 8 characters from str1 and str2.
16