Sunteți pe pagina 1din 4

COLLEGE OF ENGINEERING & TECHNOLOGY

Department: Computer Engineering (Cairo Branch)


Course Code: CC112 Sheet: 1

Course : Structured Programming Topics: program structure / Data types / Input-Output

____________________________________________________________
Level 1:
Topic Declaration 1) Question Assume that you want to define a variable to store the number of students in a class. What type of variable would you need to define? Show how you would define such a variable. Below is a sample program that will not compile. Why not? By moving which line can we get the code to compile? int main() { Printf (Hello World); return 0 ; } # i n c l u d e < stdio > Determine which of the following variable declarations are valid. For the invalid declarations, indicate why they are invalid. int cvg1234; float double; char 1letter; long hello$world; int x_y_4; Below is a sample program. Use it to answer the following question: What happens if we declare the same name twice within a block, giving it two different meanings? Did your program compile? If so, what does it print? If not, what error message Do you get? #include <iostream> int main() { int arg1; arg1 = -1; int x, y, z; char myDouble = 5; char arg1 = A; printf (%d\n, arg1); r e t u r n (0) ; } Syntax 5) 6) 7) 8) What is the purpose of the #include <stdio.h> at the beginning of most C programs? Explain, briefly, the difference between the data types long int and int. Explain, briefly, the difference between the data types double and float. What do we mean by the term reserved word?

2)

3)

4)

1
Version 13a

9)

Consider the following program #include <stdio.h> int main() { int arg1; arg1 = -1; int x, y, z ; char myDouble = 5 ; char arg1 = A ; printf ( %d\n, arg1 ) ; return ( 0 ) ; } " a) What errors result from running this program? Why were these errors issued? b) What can you deduce about the rules for naming variable identifiers after running this program? Indicate which of the following are illegal names for a variable identifier stating the reason in each case a) Double b) main c) 3BlindMice d) he_llo e) num 4 f) E2E Write a program that writes the message Welcome the world of C Show how you would print the following menus on the screen using a single printf statement a- Start Game b- Load Game c- Save Game d- Exit Please enter your choice: Write a program that reads two integers and print their sum Show how you would read four variables, called X, Y, Z and W, from a user in a single scanf. You may assume that the variables are of type char, int, float and long respectively. Write a simple program that reads an integer from a user and stores it in a variable called X. Copy the following program and fill in the instructions as indicated by the comments #include <stdio.h> int main ( ) { //1. declare a character variable with the name alpha //2. initialize alpha to a Capital letter Printf ( "\n\nalpha was initialized to %c\n", alpha ) ; alpha = alpha + 32; printf ( "This program has changed it to: %c", alpha ) ; return 0; } a) Run the program (Make sure to input a small case alphabet). What was your input to the program? What was the output from this program? b) In two or three sentences explain what did this program do 2
Version 13a

10)

Input/output

11) 12)

13) 14)

15) 16)

Input Trace

17) 18)

Why do we use the function getch() at the end of the main function? Trace the output of the following program if the user enters a value of 10 when prompted for the radius. #include <stdio.h> int main(void){ float radius, area; printf(Please enter the radius); scanf(%f,&radius); area=3.142*radius*radius; printf(The area is %f,area); return 0; } Trace the following program if the user enters the numbers 20 and 10. Try to explain in words the function of the program. #include <stdio.h> int main(void){ float x,y,result; printf(Please enter two numbers: ); scanf(%f %f,&x,&y); result=(x+y)/2; printf(The result is: %f,result); return 0; } Copy the following program to your IDE. Run the program, correct any syntax errors that you might find include <stdio.h> int main () { int x; double f char c; printf ( " x = ", x ) ; printf ( " c = %c", c ) ; printf ( f = %lf", f ) ; return 0; } a) What are the values printed for x, f, and c? b) Where did these values come form? c) Add the following code to your corrected program before the first printf statement and run the program again x = 5; c = 'A'; f = 3.4; d) What are the values printed for x, c, and f? e) What did the statements added in 4 do to x, c, and f?

19)

20)

Level 2:
Topic Input/output 21) Program Write a C program that reads the radius of a circle and calculates and displays The area of a circle. The circumferences of the circle

3
Version 13a

22)

Write a C program that reads the length and breadth of a rectangle and calculates and displays the area and perimeter of the rectangle Write a C program that reads a distance in meters and converts it to centimeters and millimeters. The program should display the results on the screen. Write a program that gives the following output **************** * AASTMT * ****************

23)

24)

4
Version 13a

S-ar putea să vă placă și