Showing posts with label using code blocks. Show all posts
Showing posts with label using code blocks. Show all posts

Answers for the First Semester Exam

            Hey there.. Folks.. I'm Back after 1st sem exams.. Back in Action... ;) But, Still I can't take my mind out of the exams.. So I decided to do a post about  the exam paper.. Hope this will important to my blog readers.. :)

           This paper was included with 4 questions, and had to answer all of them. Lets start from the first one.

Q1.




a.) This is a theoretical question about programming. I copied       this answer-phrase from my previous post.

Types of Programming Errors. 
  • 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.
b.)
        (i)   Syntax Error
        (ii)  Logical Error
        (iii) Runtime Error
c.)
        (i) This statement is 'True' because in any program                      language the codes are executed line by line, so 
             without the declaration-part reads first, the program
             doesn't know what is the variable is.
        (ii) This is 'False' because, c++ is case sensitive. The                  variable name value with lower case letters and
             the variable name VALUE with upper case letters
             are not identical in c++, means both variables can be              exist at the same time.
        (iii) This is also 'True' because when 2 integers are                       divided there might be a remainder for that                             division. If any two of decimal numbers are divided
              (Floats) the answer is also a decimal point number,
              there won't be any remainder for that division.
d.)
            In this program there are 3 inputs, and we want to find the largest one only. so what we do is, we compare two numbers first and get the largest from those two and compare that with the other remaining number.(we need not to find the order of numbers , just the largest one only) To do that we need three selection conditions, one input and three output diagrams.

 Q2.

a.)
            In this question what you have to do is create some names for some variables they have given, using the rules and etiquette we have taught. 
(i)   float speed , float Speed , float spd ,.. etc.
(ii)  char Grade , char grade , char grades , char Grades ,              char Grad...etc
(iii)  float selPrice , float sel_price ,...etc

b.) 
(i)   int sum ; 
(ii)  count = 0 ;
(iii)  cout<<" The average is: "<<avg ;
       or
       cout<<"\" The average is: \" "<<avg ;

c.)
(i)  False- Even without the default case the switch works             without producing any error, but in most cases default is
      very essential part of the switch.
(ii)  False - Precedence of arithmetic operators are higher.
      Here's the order of precedence of operators.

(iii)  True- because || operator is the Logical OR operator.

d.)
Q3.

a.)
(i) while loop
    do while loop
     for loop
(ii) The while loop and the for loop are both entry-condition          loops. The test condition is checked before each iteration        of the loop, so it is possible for the statements in the loop        to never execute. 
     Exit-condition loop, in which the condition is checked after      each iteration of the loop, guaranteeing that statements          are executed at least once. This variety is called a do               while loop
(b)  

  (i)   2  1.5  1  0.5
  (ii)   10
          7
          4
          1
(iii)   10

(c)



Q4.

a.)
(i)   score
(ii)  double
(iii)  10

b.)
            A function has 2 main parts, Function Header and Function Body. As the features of a function we can consider few parts in detail, 
  • return type ex:- int , static , void , float, double
  • function name ex:-  int main ()  , double func1  ()
  • parameter list ex:- int func1 (int num1, int num2)
  • function body : rest of the function in curly braces
c.)
(i)   int   smallest   (int x,int y,int z)
(ii)  void  instructions  ()
(iii)  double  intToDouble  (int number)

d.)  


            In above function the parameters(arguments) in function header x and y are declared with ampersands. int &x , int &y.This is because we want to pass the value to the function by reference.
            That means when the values of the arguments are changed in the function, actual values of the reference variables are also changed. 

Here I attached the complete program for you to try.


#include <iostream>

using namespace std;


void zeroSmaller(int &x, int &y);

int main ()
{
   
   int a;
   int b;
 cout<<"Enter a value for  a"<<endl;
 cin>>a;
cout<<"Enter a value for  b"<<endl;
 cin>>b;
   zeroSmaller(a, b);

   cout << "After  value of a :" << a << endl;
   cout << "After  value of b :" << b << endl;

   return 0;
}

void zeroSmaller(int &x, int &y)
{
  if(x>y)
  {
      y=0;
  }
  else{x=0;}

   return;
}



            Here in function zeroSmaller, if x is lower then x will be set to zero by the function , but at the same time the actual value of a is also set to zero, because the value was passed by reference. And the value of b will be set to zero if the y is the lowest.

            So that's all for the first semester exam. Hang around with my c++ programming posts series to learn this completely from the beginning.  

            If you have anything to be more explained please mention in comments section below or any good or bad thoughts about this blog please comment.

See you in my next post.. Happy vacation.. :)

The first Program in C++. Using Code Blocks


Hello Again my Fellow Readers..!!
Hope you have installed Code Blocks and waiting to start your first C++ program and looking for me.. ;)
Sorry for the delay..! Let’s start quickly..
So if you opened a new project and opened it’s main.cpp file , now you can see the first program has already created…
Really..!!
Sure..  What you can actually see is bunch of lines of codes of a simple program. This line of code is known as the source code of your program. Means this is the set of instructions you gave to the program.

So what happened in this program.. Let’s check it out.. In the top tool bar you will see an icon with a cog wheel saying build.. You can build your program using it.. Click on it..
 (It will compile your program.. )  Then you see a green color arrow head icon next to build icon.. (Run) Click it and boom!! There is your first program.. It will open the command prompt and run your program in it.

This program will display the message “Hello World” in cmd. Press Enter and then the program will terminated.
            So what happened here was the source code was translated into machine code by a compiler. That’s what you done by building the program. Then you run the program.
So a compiler is a translator between programming language  and machine language.
There is another translator called interpreter. Both compiler and interpreter do the same thing but in different ways. A compiler translates the entire program at once. Interpreter does that by translating the codes line by line , consuming more time but, easier to identify errors more accurately than compilers.

Come back to your code, Select the code, and press delete. Right.. Now do exactly what I say..
1.     Type   #include<iostream>
     This is a header file, Also known as a preprocessor.
2.     Next line Type   include namespace std;
    
This part is essential to output a message in cmd. Don’t forget the semi-colon ; at the end of the line.
3.     Then type   main()
4.     On the next line type   {
     Open curly brace.
5.     Then type   cout<<”Your Name”;
      replace the Your name with your name  ;) (Within quotation marks)  ex:-    “Shehan”. Don’t forget the semi colon again.
6.     Then type    return 0;
7.     And finally type       }

Done…   Now build and run the program.    
This will be the output.


Right, now I want you to delete   cout<<”Your name”;   and  type  cout<<”Your_Name”<<endl;  and  cout<<”Your_Address”<<endl; in two different lines.
Build it.. and run it.. And see the result.


       I think you got the idea that cout<<” ” is use to display an outputs in your program.   
       endl; - is refer as end of line, which creates a new line, after that.

       So…!   Use cout, to display different outputs.
 Try to use it creatively.
See You in m next Post..!!!   Have fun..    R.S.
Next Post>>

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