0% found this document useful (0 votes)
68 views

Structured Programming, 2nd Year All

1. The document contains practice problems for structured programming concepts like variables, data types, arithmetic operators, decisions/logic, and basic programs. 2. Example problems include evaluating arithmetic expressions, writing if/else statements to find maximum values, programs to calculate quadratic equations and projectile motion, and more. 3. The problems are presented over multiple sheets and cover fundamental C programming and problem solving skills for second year students.

Uploaded by

Prince yemen
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Structured Programming, 2nd Year All

1. The document contains practice problems for structured programming concepts like variables, data types, arithmetic operators, decisions/logic, and basic programs. 2. Example problems include evaluating arithmetic expressions, writing if/else statements to find maximum values, programs to calculate quadratic equations and projectile motion, and more. 3. The problems are presented over multiple sheets and cover fundamental C programming and problem solving skills for second year students.

Uploaded by

Prince yemen
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Military Technical College Spec.: All 2nd year.

Chair: Computers. Term: Summer.


Subject: Structured Programming. A/C Year:2007-2008

Sheet (1): Variable Types


(1) Can the following be used as a user-defined identifier? Why not?
)a Void b) Max_Num c) double d) G
e) Sue’s f) insert# g) 7th h) X2
i) Part20% j) printf k) X Y )l Scanf

(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 );

(4) What is wrong with the following C program?


void main
{ float years, days
printf "please type your age in years: ";
scanf ("%f", years); days = years*365;
Printf ("You are %f days old.\n" , days);}

(5) What is the output of the following program?


void main (void)
{
printf ("%s\n%s\n%s", "one", "two", "three") ;
}

(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.

(7) Write a program that will print the phrase


a, b, and c are all letters.
Use characters constants to represent the letters.

1
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2007-2008

Sheet (2): Arithmetic Operators


(1) Evaluate the following C expressions:
a) 7 / 22 b) 22 / 7 c) 7 % 22
f) 7.0/22.0 d) 22 % 7 e) 22.0 / 7.0

(2) Write the following as a valid C arithmetic expression


b a  b  3 
a) X a b) Y   * 0.324 c) Z 1  d
c  2  a  b

(3) Evaluate the following C expressions:


a) floor (15.8 ) b) floor (15.8 + 0.5 ) c) log10 ( 1000.0 )
d) ceil ( - 7.2 ) * pow (4.0, 2.0 )
e) sqrt ( floor ( fabs ( -16.8 ) ) )

(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;

(6) Write the following equation as a C- language statement


a +5
K =
cd + e

(7) Write a program to get the roots of a quadratic equation;


ax 2 + bx + c = 0

given its 3 coefficients a, b, and c.

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).

(9) A Manufacturer wishes to determine the cost of producing an open-top cylindrical


container. The surface area of the container is the sum of the area of the circular base plus
the area of the outside (the circumference of the base times the length of the container).
Write a program to take (from the keyboard) the radius of the base, the height of the
container, and the cost per cm2 of the material. The program should calculate and display the
cost of each container.

(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

Sheet (3): Decisions (Logic)


(1) What is wrong with the following decision statements?
a) if (x > 10) then y= sqrt(x);
b) If (x > 5 & x < 7.5) y = 3.5 * x;
c) if (x =! 16); y == 0.25
d) if ( x > 3 && x < 20 && x != 14 ) y = 9;
e) if (x > 0.0); else type = 'N';

(2) What values of x ill cause the following expressions to be true?


a) (x<4)&&(x>8)
b) (x<4)||(x>8)
c) (x<8)||(x>4)
d) (x<8)&&(x>4)
e) !(x<8)||(x>4)

(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.

(5) Write a C program that check if a number divisible by 3.

(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.

(7) Write a C program to calculate the variable Y if X is given. Where :


x5 + 4 x4 + 3 x3 + 2 x2 + x + 10 x > 10
Y= 100 x = 10
2 x2 + x + 20 x < 10

5
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007

(8) Write a C program to determine whether a number (given by the user) if is


odd or even.

(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

Sheet (4): Multiple Decisions


(1) Trace the following program showing its output:
void main (void)
{
float num1, num2; char op;
printf("Type number, operator, number\n");
scanf("%f %c %f", &num1, &op, &num2);
if (op == '+') printf (" result=%f", num1+num2);
else if (op == '-') printf (" result=%f", num1-num2);
else
switch (op)
{
case '/': printf("result=%f",num1/num2); break;
case '*': printf("result=%f",num1*num2); break;
default: printf ("unknown operator");
}
}

(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

Sheet (5): Loops


(1) Trace the following program showing its output:
void main (void)
{
int count =0;
printf ("Type in a phrase:\n");
while (getche() != '\r')
count ++;
printf ("\n Character count is %d");
}

(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”);

(3) Trace the following program showing its output:


void main (void)
{
char ch = 'a';
while (ch != '\r' )
{
printf("Enter a character: \n");
ch=getche();
printf("\nThe code for %c is %d.\n", ch, ch);
}
}

(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>.

(6) Write a C program to tabulate the following function for x = 1 to 20 where,


f(x) = (3 x3 + 10 x2 + x)/(x-4)(x-7)(x-9) and then print out the average of f(x).

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).

(9) Fibonacci numbers form an interesting sequence in which each number is


equal to the sun of the previous two numbers;
FI = FI – 1 + FI – 2
where FI refers to the Ith Fibonacci number. F1 = 0, F2 = 1. Write a C program to display
the first N Fibonacci numbers.

(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.

(14) Write a C program that displays a triangle of numbers in the form:


a) 1 b) 7654321234567
212 65432123456
32123 543212345
4321234 4321234
543212345 32123
65432123456 212
7654321234567 1

11
Military Technical College Spec.: All 2nd year.
Chair: Computers. Term: Summer.
Subject: Structured Programming. A/C Year:2006-2007

Sheet (6): Functions


(1) Is this a correctly written function?
abs(num);
{
int num;
if (num < 0)
num = - num;
return (num);
}

(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().

(6) Write a function ln_approx that computes an approximation of the natural


logarithm of a number between 1 and 2 by summing a given number of terms of this
series:
K ( 1 ) n 1 x n
ln( 1  x )  
n 1 n
Also write a driver that calls ln_approx twice with the same value, requesting first the
sum of four terms (K=4) and then requesting sever terms (K=7). The program should
display a message comparing the results of the two calls to the value returned by the math
library function log.

(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 

abc
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

Sheet (7): Arrays and Strings


(1) Trace the following C++ programs showing its outputs:
void main ( )
{
int p=1, i;
int x[10]={10,8,5,20,2,3,5,12,3,10};
for (i=1; i < =10 ; i++)
if (x[i] < x[p])
p=i;
cout << x[p];
}

(2) Trace the following C++ programs showing its outputs:


void main ()
{ int i, j=1, k=1;
int x[6];
while (k <= 6)
{ x[k] = 3*k;
x[k+1] = k+2;
k += 2;
}
while (j <= 6)
{
x[j] + = x[j+1]; j+ = 2;
}
if (x[j] < 7)
x[2] = x[3];
for(i=1 ; i <= 6 ; i++)
cout << x[i];
}

(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

You might also like