Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Exercises Fundamnetal C Programming

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Name : _____________________ Date: 26/6/2024 2.

Find the error in each of the following code segments and


explain how to correct it.
Exercises C Programing
1. Write a single C statement to accomplish each of the following: a) x = 1;
while ( x <= 10 );
a) Assign the sum of x and y to z and increment the value of x x++;
by 1 after the calculation. }
b) Multiply the variable product by 2 using the *= operator.
c) Multiply the variable product by 2 using the = and * b) for ( y = .1; y != 1.0; y += .1 )
operators. printf( "%f\n", y );
d) Test if the value of the variable count is greater than 10. If it
is, print “Count is greater than 10.” c) switch ( n ) {
e) Decrement the variable x by 1, then subtract it from the case 1:
variable total. printf( "The number is 1\n" );
f) Add the variable x to the variable total, then decrement x by case 2:
1. printf( "The number is 2\n" );
g) Calculate the remainder after q is divided by divisor and break;
assign the result to q. Write this statement two different default:
ways. printf( "The number is not 1 or 2\n" );
h) Print the value 123.4567 with 2 digits of precision. What break;
value is printed? }
i) Print the floating-point value 3.14159 with three digits to
the right of the decimal point. What value is printed? d) The following code should print the values 1 to 10.
n = 1;
while ( n < 10 )
printf( "%d ", n++ );
3. Mary receives a bag, which has 100 balls. There are only red and blue
balls in the bag. If the player’s guess is incorrect, your program should loop
a) Write an algorithm to count the number of red and blue balls.
until the player finally gets the number right. Your program
b) Write a C program based the answer in (a).
should keep telling the player Too high or Too low to help
4. the player “zero in” on the correct answer.
a) Write a pseudocode the game of “guess the number” as
follows: b) Draw the flowchart based on pseudocode in (a).
c) Write the C program based on algorithm in (a) and (b).
Your program chooses the number to be guessed by selecting
5. a) An example of a display of input and output is shown below.
an integer at random in the range 1 to 10. The program then
types:
Enter a number: 7
&&&&&&&
I have a number between 1 and 10. &&&&&&
Can you guess my number? &&&&&
Please type your first guess. &&&&
&&&
The player then types a first guess. The program responds &&
with one of the following: &

Write a complete program in C to generate to triangle shape using


loops based on a value entered by user.

c) Convert the following algebraic expression to the equivalent


statement in C.

1. Excellent! You guessed the number!


Would you like to play again (y or n)?
2. Too low. Try again.
3. Too high. Try again.
d) Write a code segment in C that accepts any integer number. c) Write a function definition for function display.
Determine and display the first digit. An example of the d) Describe when the return statement is required in a function.
display of input and output is shown below.

8. Input a number: 4773


6. A program in C is designed to perform a mathematical operation by Malaysian Last digit: 4
accepting two integers and an arithmetic operator. An example of the
display of the input and output of the program is shown below.
Meteorological Department is recording a temperature in four states.
The temperature in each location will be recorded for three
Enter first number: 5 consecutive days in the table below.
Enter second number: 3
Choose an arithmetic operator (+, -, *, /): *
Temperature (Celsius)
Result: 5 * 3 = 15 State
Day 1 Day 2 Day 3
Selangor 33 32 33
Perak 31 33 30
Kelantan 27 26 30
a) Write a pseudocode for the above program using a selection Johor 25 31 27
control structure.
b) Write a C program based pseudocode above (a).
a) Declare and initialize an array stateTemp.
7. The function main of a program is given below. b) Write a code segment to calculate and print the average
temperature the four states.
int main ( ) c) Write a code segment to find and print the highest and lowest
{ average temperature.
int a, b, c;
printf ("Enter two integer vafues: ");
scanf (“%d %d", &a, &b);
9. Write statements in C for each of the following:
c = add (a, b); a) Declare a one-dimensional array letter and assign “N” , “S”, “E”,
display (c); “A” and “W” to the array.
}

a) Write a prototype for the function add and function display. b) Declare and initialize values for a sequence number of 1, 4, 7, 10,
b) Write a function definition for the function add that returns the …., 25 to a one-dimensional array number.
sum of two values to the calling function.
c) Declare a two-dimensional integer array matrix with 3 rows and
4 columns.

12. A supermarket stores item name, number of items sold and prices for
10. A declaration statement in C for an array of scienceScore is given each item in its transaction system.
below.

int scienceScore [ ] = {15,18,12,15,17,9,12,16}; a) Write a definition statement struct retail in C to store the
information below.
Write a code segment in C that includes each of the following:
 Calculate the average value from the array above.
 Count the number of array elements that has the value less b) Write an array declaration statement in C for 20 items of type
than average. struct retail as defined in (a).
 Determine whether there is an array element that has the
same value as the average value. If found, print “Yes, the c)
same value exists!”, otherwise print “No, the same value Write a code segment in C to read and display the item name,
does not exist!”. number of items sold and price for each item sold. An example of
the display is shown below.

11. Question 11 as below;

a) Write a structure definition statement in C for footBallPlayer


that contains name, year of experience, age and weight. Use a
suitable data type for each structure member.

b) Write a statement in C that declares an array player with six


elements of type struct footBallPlayer.

c) Write code segment in C to store structure member values in


an array plater using repetition control structure for.

You might also like