Riphah International University Faculty of Computing Programming Fundamentals Lab 2: Data Type
Riphah International University Faculty of Computing Programming Fundamentals Lab 2: Data Type
Objective:
Learn basic data types in C++.
Learn how to use data type in problem
solving. Learn how to declare a variable
Variables (Introduction)
As a programmer, you have to decide what types of information are necessary for your program
and how this information would be used. When writing a program, you will provide these pieces of
information to the computer, the computer then puts them together. When your program runs it is
“loaded” into the memory (RAM). When the user is using your program, the information that your
application requests also goes into the RAM while your program is processing such requests.
Because your program will be made of many of these things, the computer needs to know what
these things would be, and how much space each one of them would need. Because such a thing can
change (vary) throughout your program, it is called a variable.
By its definition, a variable is a place in memory that holds some information. Some
information may be numbers, characters, strings or Booleans.
Declaration:
Variable names:
When using the various necessary variables in your programs, each one of them need to be
identified. A variable is primarily recognized by its name. C++ provides rules for naming items
in a program.
A name can consist of one word such as country. A name could also be a combination of more than
one word, such as firstname or dateofbirth.
C++ is case-sensitive; this means that CASE, Case, case, and CaSe are four
completely different words.
The following example will print the actual size allocated based on your computer:
/* Displays the number of bytes used to store each basic type */
#include <iostream>
int main() {
cout<<"The size of char is \n"<<sizeof(char); cout<<"The
size of short is \n"<< sizeof(short); cout<<"The size of int is
\n"<<sizeof(int); cout<<"The size of long is
\n"<<sizeof(long); cout<<"The size of float is
\n"<<sizeof(float); cout<<"The size of double is
\n"<<sizeof(double); cout<<"The size of long double is
\n"<<sizeof(long double);
return 0;
}
Write the following segments of codes in Microsoft Visual C++. Execute and observe the output.
1. A program declares a single variable of type int.
#include
<iostream>
using
namespace std;
int main ()
3
{
int age; //declaration
cout<<"This program declares a variable age\n"<<age;
return 0;
}
2. Display a value of a variable
#include
<iostream>
using
namespace std;
int main ()
{
int age; //declaration
cout<<"This value of age is
“<<age; return 0;
}
You will observe that it will display some garbage value, the variable is uninitialized and its just
returns whatever value happened to be in that memory location.
3. Each variable must be initialized. Assignment operator (=) is used to store a value to
a variable.
#include
<iostream>
using
namespace std;
int main ()
{
int age = 21; //initialization
cout<<"This value of age is
“<<age; return 0;
}
Always initialize your variable when you define them. If you don't know what value a
variable should have when you define it, initialize it with zero.
4. Assignment operator (=) is also used to store result of a calculation in a variable.
#include <iostream>
using namespace std;
int main ()
{
int var1 = 10, var2 = 20;
int var3 = var1 + var2;
cout<<"This value of variable 3 is “<<var3;
return 0;
}
More than one variable can be declared in single statement (separated by comma)
5. Declare the variable with following names and observe which is correct and which not
the correct variable name is.
age-limit, _temprature, 3rdDay, lastName, mounth45
Laboratory Tasks:
1. Write a program that stores the integers 62 and 99 in variables, and stores the sum of these
two in a variable named total.
2. A customer in a store purchasing five items. The prices of the five items are :
Price of item 1 = 12.95
4
Write a program that holds the price of the five items in five variables. Display each item`s
price, the sub total of the sale. The amount of sales tax and the total. Assume the sales tax is
6%.
number = 62.7
double number;
cout<<number<<endl;
return 0;
}