Saturday, June 2, 2012

CBSE - Class XI - Computer Science - Simple C++ Snippets (Part-2)

Q1. What is the size of a variable?

Answer: When you declare a variable, memory is allocated for that variable. For example char variable takes 1 byte of memory while an int variable consumes two bytes.

Note how much int will occupy memory space is platform dependent. For example, Turbo C++ is DOS based program allocates 2 bytes to an int variable. While visual c++ 6.0 compiler meant for 32-bit Windows applications, allocates 4 bytes to an int variable.

You can check the size of a variable allocated by a compiler by using sizeof operator.

#include <iostream.h>
#include <conio.h>

int main()
   clrscr();             // clear the screen
   // using sizeof opeator to find the size of variable
   cout << "size of char: " << sizeof(char) << " bytes." << endl;
   cout << "size of int: " << sizeof(int) << " bytes." << endl;
   cout << "size of long: " << sizeof(long) << " bytes." << endl;
   cout << "size of float: " << sizeof(float) << " bytes." << endl;
   cout << "size of short: " << sizeof(short) << " bytes." << endl;
   cout << "size of double: " << sizeof(double) << " bytes." << endl;
   int x =10;
   cout << "size of variable x: " << sizeof(x) << " bytes." << endl;
   float b = -200.10;
   cout << "size of variable b: " << sizeof(b) << " bytes." << endl;
   return 0;

When run (compiled with Turbo C++ 3.0), the program will give the following output:
size of char: 1 bytes.
size of int: 2 bytes.
size of long: 4 bytes.
size of float: 4 bytes.
size of short: 2 bytes.
size of double: 8 bytes.
size of variable x: 2 bytes.
size of variable b: 4 bytes.

Q2: In Turbo C++ (TC) An unsigned integer can have maximum value of 65535. What will happen if you add another number to it?

Answer: In general, we say data overflow happens here. But the behaviour is same as seen in car odometer. When the unsigned int crosses its maximum value, it  wraps around and reset to zero.
e.g. 65535 + 1 will become 0, 65535 + 2 will become 1 and so on...
You may try the following program:
#include <iostream.h>
#include <conio.h>

int main()
  clrscr();             // clear the screen
  int val = 65535;
  cout << "val = " << val << endl;
  val = val + 1;
  cout << "val = " << val << endl;
  return 0;

Q3: Consider the following C++ snippet:
int x = 25000;
int y = 2*x;
cout<< y << "\t" << x;

Why does the output does not come as 50000? What could be the reason?

Answer: The range of int (compiler takes int as signed int) is from -32768 to +32767. After multiplying the value exceeds the upper bound (+32767) and result is not the desired output. We should consider long instead of int.

Q4:Write a program to input a digit and display its ASCII code.

Answer: C++ gives char variable special treatment. It displays the ASCII character.  E.g. if char variable stores 65, it will be displayed as 'A'.

You may assign a character to int (e.g. int x = 'P'). When you cout << x; it will print 80 not 'P'.
// program to input a alphabet or digit
// and display its ASCII code

#include <iostream.h>
#include <conio.h>

void main()
  char ch;
  clrscr();   // clear screen
  cout << "Enter a character or digit:";
  cin >> ch;
  int x = ch;  // stores 1 byte of character as integer
  cout << "ASCII code of character("<< ch << ") is: " << x << endl;
  x = 'P';
  cout << "x = " << x << endl;

Q5: What are different escape sequences or escape characters in C++?

Answer: Any character preceded by a backslash, \, is called an escape sequence, or escape character. In general, escape characters are used to print special characters. Here is the list of escape characters:
Escape SequenceMeaning
 \aTerminal Bell
 \fForm Feed (Used in printers)
 \tTab space (equal to four spaces)
 \nNew Line
 \rCarriage Return
 \vVertical Tab
 \'Terminal Bell
 \"Terminal Bell
 \?Terminal Bell
 \000Octal Number
 \xhhHexadecimal Number
 \0Null character

Following snippet shows the use of escape sequence.
#include <iostream.h>
#include <conio.h>

void main()
  // following line will print Hello C++ is fun.
  cout << "Hello C++ is gun\b\b\bfun"<< endl;

  // following uses tab spaces.
  cout << "\'C++\'\tis\tfun" << endl;

  // you may hear computer beep. 
  cout << "\a";

The output on screen will be:
Hello C++ is fun
'C++'    is    fun.

Q6: Why the Salesmen didn't get any commission?
A business man gives 3% commission to his salesman on sales done by that sales man. He asked the programmer to write a C++ program to compute commission. To his surprise, none of the salesman got any commission. What's wrong with following snippet?

#include <iostream.h>
#include <conio.h>

void main()
  // define float variables
  float sales, comm;
  cout << "Enter the sales value:";
  cin >> sales;

  // compute the commission
  comm = (3/100)* sales;
  // following uses tab spaces.
  cout << "Commission for sales INR " << sales << " is = " << comm << endl;

Answer: The problem lies in the expression i.e. comm = (3/100) * sales. The calculation (3/100) is an integer computation i.e. computer creates a temporary int variable to store value of (3/100 = 0.03 converted into int) which becomes zero.
To correct this, we must tell the compiler that the computation is floating type i.e. the expression should be:
       comm = (3.0/100.0) * sales;
       comm = 0.03 * sales;

Go to Snippets-3

No comments:

Post a Comment