Pree Mid Work Sheet

  • Upload
    habtamu

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

  • 8/10/2019 Pree Mid Work Sheet

    1/8

  • 8/10/2019 Pree Mid Work Sheet

    2/8

    Work-sheet1 for introduction in computer programming using C++

    Compiled by Mulugeta H Page 2

    Chapter two; fundamentals of C++ programming

    1. What is the need for programming?2. What are the three main types of computer programming languages?

    a. Machine language, assembly language, high level languageb. Imperative language, functional language, declarative languagec. COBOL, Fortran-77, C++

    3. What is the difference between a machine-language program and a high level languageprogram?

    4. From the point of view of the programmer what are the major advantages of using a high-level language rather than internal machine code or assembler language?(a) Program portability(b) Easy development(c) Efficiency

    5. What is the only language that a computer understands directly?

    a.

    English, as spoken in Boston, Mass.b. BASIC, the Beginners' All-purpose Symbolic Instruction Codec. Machine language, different for every type of CPUd. Like c and c++ languages

    6. What are the roles of a compiler, assembler and interpreter and their main difference?7. What is a source program? What is an object program?8. What are the properties of well-designed programs? 9. What are the steps involved in going from the specification of a problem to producing an

    executable program which will solve the problem?10. What is the need of writing an algorithm before writing programming code?11. What are advantages and drawbacks of using flow charts in developing programming

    languages?12. Write an algorithm to compute the sum of the squares of integers from 1 to N where N is a

    number input from key board and also draw the corresponding flowchart. Assuming thatyou have to code the above given problem, show the steps involved in program design.

    13. A program is required which will read in the breadth and height of a rectangle, check thebreadth with height to identify whether is rectangle or square and which will output thearea and the length of the perimeter of the rectangle. The output should be `the area andperimeter of the square are . . . ' whereas if they are not equal then the output should be `thearea and perimeter of the rectangle are . . . '.Write an algorithm and develop the flow chartfor this problem.

    14. What is the purpose of the following two lines?a. #include

    b. Using namespace std;15. Explain Syntax and Logical errors?16. A compiler produces an error in compiling a program because a closing round bracket has

    been missed out in the source code. What type of error is this?a) Syntax Errorb) Logical Errorc) Linker Errord) Run-time Error

  • 8/10/2019 Pree Mid Work Sheet

    3/8

    Work-sheet1 for introduction in computer programming using C++

    Compiled by Mulugeta H Page 3

    17. Suppose you write a program that is supposed to compute the interest on a bank account ata bank that computes interest on a daily basis, and suppose you incorrectly write yourprogram so that it computes interest on an annual basis. What kind of program error is this?

    18. When the program is running it produces a number that is too large to fit into the spaceallocated for it in memory. What type of error is this?

    19. If you omit a punctuation symbol (such as a semicolon) from a program, an error isproduced. What kind of error?

    20. Correct the syntax errors in the following C++ program:include iostreamusing name space stdMain();{Float x,y,z;cout < "Enter two numbers ";cin >> a >> bcout

  • 8/10/2019 Pree Mid Work Sheet

    4/8

    Work-sheet1 for introduction in computer programming using C++

    Compiled by Mulugeta H Page 4

    25. Given the declarations:float x;int k, i = 5, j = 2;

    To what would the variables x and k be set as a result of the assignmentsk = i/j;x = i/j;k = i%j;x = 5.0/j;

    26. What values does C++ use to represent true and false ?27. Consider the following section of C++ program, in which i and n are int variables

    n = 7;i = 4;i = n++;What are the values of i and n?(a) i=7 n=8(b) i=7 n=7

    (c) i=8 n=8(d) i=4 n=728. True or false: A variable of type char can hold the value 301.29. Consider the following section of C++ program, in which i and n are

    int variablesn = 5;i = 9;i = --n;What are the values of i and n?(a) i=9 n=5(b) i=4 n=4(c) i=4 n=5

    (d) i=5 n=430. Write a logical expression which returns true if a float variable x lies between -10.0 and10.0.

  • 8/10/2019 Pree Mid Work Sheet

    5/8

    Work-sheet1 for introduction in computer programming using C++

    Compiled by Mulugeta H Page 5

    Chapter three; control statements and loops

    1. Percentage marks attained by a student in three exams are to be entered to a computer. Anindication of Pass or Fail is given out after the three marks are entered. The criteria forpassing are as follows: A student passes if all three examinations are passed. Additionally astudent may pass if only one subject is failed and the overall average is greater than orequal to 50. The pass mark for an individual subject is 40. Write a C++ program toimplement this task.

    2. Write a program that allows the user to enter a time in seconds and then outputs how far anobject would drop if it is in free fall for that length of time. Assume that the object starts atrest, there is no friction or resistance from air, and there is a constant acceleration of 32 feetper second due to gravity. Use the equation:NB: You should first compute the product and then divide the result by 2

    3. Write an algorithm to produce an n times multiplication table (n less than or equal to 10).For example, if n is equal to four the table should appear as follows:

    1 2 3 41 1 2 3 42 2 4 6 83 3 6 9 124 4 8 12 16

    Convert your algorithm into a program. Use nested for loops and pay attention to

    formatting your output so that it is displayed neatly.4. Write a C++ program to get variable x from keyboard repeatedly and if x is positive then a

    variable poscount is incremented and if x is negative a variable negcount is incremented tocount the number of negative and positive numbers separately and also their sum anddisplay the information to your screen

    5. Write a nested if-else statement that will assign a character grade to a percentage mark asfollows; 70 or over A, 60-69 B, 50-59 C, 40-49 D, 30-39 E, less than 30 F. and for invalidinput requesting the user to input valid mark(invalid marks negative numbers, charactersetc)

    6. Write a C++ program which when two integers x and y are input will output the absolutedifference of the two integers. That is whichever one of (x-y) or (y-x) is positive. Think of all

    the cases that can arise and consequently plan a set of test data to confirm the correctnessof your program.

  • 8/10/2019 Pree Mid Work Sheet

    6/8

    Work-sheet1 for introduction in computer programming using C++

    Compiled by Mulugeta H Page 6

    7. Use for loops to construct a program that displays a pyramid of Xs on the screen. Thepyramid should look like this

    Except that it should be 20 lines high, instead of the 5 lines shown here. One way todo this is to nest two inner loops, one to print spaces and one to print Xs, inside anouter loop that steps down the screen from line to line.

    8. A series of positive numbers are to be entered from the keyboard with the end of the seriesindicated by a negative number. The computer should output the sum of the positivenumbers. Write an algorithm for this task. Use a while type of loop with the condition`number just entered is positive'. Think carefully about what initializations are requiredbefore entering the while loop. Do a desk check of your algorithm with a small data set; say3 positive numbers then a negative number. What would your algorithm do if the userentered a negative number first? Is what your algorithm would do in this circumstancesensible?

    9. Using functions write a C++ programming code in your text editor to solve the problemsrelated the following activities;

    a) Ask the user to enter one natural number N then your program should display thefactorial of the entered number in the form N!=N*(N-1)*(N- 2)**3*2*1 .Forexample if the users input number is 5 then the output should be5!=5*4*3*2*1=120NB: ask your user to try for other number (if hi/she press some specific key) beforeexiting the screen otherwise exit the program.

    b) Ask request to the user that how many numbers he/she wants to enter (N). Oncethe user specify the size (N numbers) input these numbers from keyboard sortthem both in ascending and descending order. Example for 5 numbers entered fromkeyboard (0 7 -2 12 -11) the output should beThe numbers are before sorting are: 0 7 -2 12 -11The numbers sorting in ascending order: -11 -2 0 7 12The numbers sorting in descending order: 12 7 0 -2 -11

    c) Ask request to the user that how many numbers he/she wants to enter (N). Oncethe user specify the size (N numbers) input these numbers from keyboard find thesmallest and largest numbers and their occurrences and display the information tothe screen. Example for 10 input numbers(let 1 5 4 98 23 -2 3 45 -2 8) our outshould be ;The 10 input numbers are: 1 5 4 98 23 -2 3 45 -2 8The smallest number is -2 and it occurs 2 timesThe largest number is 98 and it occurs 1 times

  • 8/10/2019 Pree Mid Work Sheet

    7/8

  • 8/10/2019 Pree Mid Work Sheet

    8/8

    Work-sheet1 for introduction in computer programming using C++

    Compiled by Mulugeta H Page 8

    h) Develop a program to display the fabonacii series ; fabonacii series is a series ofnumbers generated by adding the next two numbers and put the result next;1 1 2 3 5 8 13 21 34 55 89 .. NB: ask your user to try for other number (if hi/she press some specific key) beforeexiting the screen otherwise exit the program

    i) Ask the user to enter numbers continuously if he/she press Y and update him/hertheir newly average and how many numbers he/she entered previously whilehe/she press any key=! Y exits the terminal (screen).Example: enter the first number >2

    You enter 1 number and the new average is updated average = 2Do you want to enter another number if so press Y > YEnter the second number > 8You entered 2 numbers and the new average is updated average = 5Enter the third number >3You enter 3 number and the new average is updated average = 4.3333Do you want to enter another number if so press Y > YEnter the forth number > 4

    You entered 4 numbers and the new average is updated average = 4.25etc until the u ser press key!=Y