Showing posts with label Using cout. Show all posts
Showing posts with label Using cout. Show all posts

Quick Post For 1st Semester Exam

Hey Guys.. This is a Quick Post to Discuss About the Model Paper of my Uni.. hope others will get something from this too..

Here I pasted the paper...  And Down there I Discussed it...



Question 01                                                                                                                (25 Marks)

(a)    Differentiate the machine language and the high level language.                  (5 Marks)
(b)   Define the terms compiler and interpreter.                                                     (5 Marks)
(c)    Describe the different ways to include comments in a C++ source code. State whether the comments are nested.                                                                                     (5 Marks)
(d)   List the three main kinds of program errors.                                      
                                i.            Identify the error discovered, if you “misspell a variable or a function name”.
                              ii.            Identify the error discovered, if you “use a single equal sign to check equality”.
(5 Marks)
(e)    Write a C++ program to display the following output.                                  (5 Marks)
(Hint: Use escape sequences.)
            







Question 02                                                                                                          (25 Marks)

(a)    The following identifiers are written in C++ and some of them are syntactically incorrect. Identify the syntactically incorrect identifier(s) and state the reason.      (5 * 1 = 5 Marks)
                                i.            CASE4
                              ii.            2010IncomeTax
                            iii.            sales$
                            iv.            exchange Rate
                              v.            cout

(b)   Write C++ expressions for the following mathematical formula.             (5 Marks)
                    


(c)    Determine the value of each of the following expressions, if x = 5, y = 10 and z = -6. Write your steps clearly.                                                                   (5 Marks)
                                i.            y > 15 && z < x + y
                              ii.            ! ( y <= 12 ) || x % 2 == 0

(d)   Illustrate the outputs of the following program segments.                       (5 * 2 = 10 Marks)
                                i.           

int num1, num2, sum;
num1 = 5;
num2 = 4;
sum = num1 + num2;
cout << "The sum of ";
cout.width(2);
cout << num1 << " and " << num2;
cout << "is" << sum << "\n";


float value1, value2, product;
value1 = 5.31;
value2 = 7.8;
product = value1 * value2;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "The Product of\n" << value1 << "and";
cout << value2 << "is\n" << setw(14) << product;

 




                              ii.             






                                                                                               
Question 03                                                                                                                (25 Marks)

You are required to implement a program to handle customer sales of a fruits stall at Katubedda Super Market. Write a program in C++ to calculate the amount of price and print a bill for the customer. Your program should allow:
(a)    To input a number of items bought (quantity) and the price per item (unit price).                                                                                                                                         (5 Marks)
(b)   Calculate the total cost; give discount to the customer according to the following conditions.                                                                                                       (5 Marks)
If the customer bill amount is greater than Rs.500/=, will be having a 2.5% discount.
If the customer bill amount is greater than Rs.750/=, will be having a 3% discount.
If the customer bill amount is greater than Rs.1000/=, will be having a 5% discount.
(c)    Your program should ask for the amount of money tendered and print the correct amount of money change.                                                                                                                                 (5 Marks)         
(d)   Print an appropriate bill. The output should correct to two decimal places.   (5 Marks)
(e)    Your program should allow doing the calculation as many times as the user wishes.
(Hint: Use suitable C++ loop structure to implement this task.)        (5 Marks)                              

Question 04                                                                                                                (25 Marks)

A competition is being organized by StarCricket Club to select the most popular player for year 2015. Nalaka, Amila, Saman, Ruwan and Chathura are five players who are contesting for popularity. The players are numbered from 1 to 5 and voting is done by entering the player number as an input.
Write a C++ program to read the vote and to count the votes cast for each player using an array. In case, a number read is outside the range (1..5), the vote should be considered as a ‘spoilt vote’.
Your program should include the following tasks.

(a)    Define an array called “vote” to store votes of players. Initialized each array element into zero.                                                                                                                (5 Marks)
(b)   Read data into an array called “vote”. (Nalaka’s votes are in the first element of the array, Amila’s votes are in the second element of the array etc..)                               (5 Marks)
(Hint: Use switch statement to handle the user selection.)
(c)    Your program should allow 100 club members to cast their votes.                      (5 Marks)
(Hint: Use a suitable C++ loop structure to implement this task.)                            
(d)   Calculate the number of votes cast for each player, number of spoilt votes and total number of votes.                                                                                                       (5 Marks)
(e)    Print the number of votes cast for each player, number of spoilt votes and total number of votes.                                                                                                              (5 Marks)


END OF PAPER


Q1. 

a.) Difference between Machine Language and a high level         language is , 

  • A machine Language is a low level language which a computer can understand or computer's use machine language to work. ex:- Assembly


  • On the Other hand a high Level Language is a programming language closer to human language or as humans high level languages are easy to understand and to work-with for us.  ex:- java , c++
b.) Define compiler and Interpreter..
  • compiler and interpreter both work as translators of a source code to a machine language.
  • it means it translate the code we type using codeblocks into machine language.(like binary )
  • Some Programming languages use compiler only i.e C, C++, some use interpreter i.e Python, Ruby, some use both.
c.)



  • Compilers Translate the entire source code at once into machine code file, and then we can run our program any time we want. Compilers are Faster. Consume more memory. If there is any error in the code compilers are less accurate in finding them.
  • Interpreters on the other hand  translate the code line by line , consume more time, memory efficient, and accurate in finding errors in the code.
c.) Comments in C++,

  • Comments are  lines of text we put in a code which does not effect to the code any how. We use comments to mention important things in a code. We can write anything as comments there are no limits.
  • There are two types of comments in C++, Single line comments and Multi line comments.
  • For Single line comments we put   //  , two backward slashes and we type the comment text in a single line.

  •  For multi line comments we put   /*  at the begining of the comment and we end the comment with    */ 
  • Nesting comments means put multi line comment in side another multi line comment. C++ does not support Nested comments. 
  • we can check that by 
    • int nest = /*/*/0*/**/1;
    • cout<<nest;
  • if the output is 1 , it supports nested comments and if output  is 0 then it doesn't support.
d.) Types of Errors in c++
  • Compile Error  (Syntax Error)
    • This happens when we type incorrect codes, (ex: Missing Semi Colon ; , misspell a variable name, forget to close a Bracket  } ,  )These programs can not compile, it shows an error message.
  • Logical Error.
    • This error occur due to our misunderstanding of the programming logic,These programs compile without producing any error messages, but does something totally different other than what we expected from the program.(To get a total, instead of addition + we type multiplication * , this doesn't give any error message, but give an unwanted result.)  
  • Runtime Error. 
    • Programs with this error also compile without any error message until some error occur in runtime.
    • Ex :- Devide by zero in runtime.
  1. Syntax Error (Compile Error)
  2. Logical Error.
Q2.
a.) Identifiers are the names we use when programming , (Names we use to name Variables , function , class etc..) An identifier starts with a letter A to Z or a to z or an underscore (_) followed by

zero or more letters, underscores, and digits (0 to 9).

Identifiers can't have !@#$%^&*()+=.. Only Underscore.

CASE4
2010IncomeTax
sales$      // $ Dollar Sign
exchange Rate   //  Space between two words
cout  // cout is a function name, cant use as an identifier

b.)
c.)

  && - Logic AND    % - Modulas (Remainder of a devision)
   || - Logic OR        == - is equal to ??
  ! - Logic NOT


  i)
    = 10>15 &&    -6<15
     = false    &&    true     
      = false


ii)
       =  !(10 <= 12)   || 5%2 == 0
       =  !true  ||  false
       =  NOT (true)   OR  false
       =   false 
d.)
     i)
       


Q3.
a.) Here I posted some Screen Shots from My Code. Some code parts have described with in comments. 

Screenshot 1
(1)

(2)

(3)

(4)
Q4.
 a.)
Here also I explained every part of codes in comments, in this example I have used an if else if statement to find the winner, which has not asked to do in question paper.

(1)

(2)

(3)

(4)


That's all guys... Hope this will help you.. C..U after the exams.. Good luck.. :)

Life of a Systems Engineer

  Hello, my dear readers. ✌😀✋ I'm talking to you after a very long time. And I am thrilled to talk-to and hear-from you after all thi...