0% found this document useful (0 votes)
3 views

Lab Manual 03 - Data Types.

The Lab Manual for Programming Fundamentals at the University of Management and Technology focuses on data types in C++. It covers objectives, useful concepts, variable declaration, and initialization, along with examples and exercises for practical application. The manual also includes lab rubrics to assess students' understanding of programming structures and problem-solving skills.

Uploaded by

rajabfan555
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab Manual 03 - Data Types.

The Lab Manual for Programming Fundamentals at the University of Management and Technology focuses on data types in C++. It covers objectives, useful concepts, variable declaration, and initialization, along with examples and exercises for practical application. The manual also includes lab rubrics to assess students' understanding of programming structures and problem-solving skills.

Uploaded by

rajabfan555
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Manual: Programming Fundamentals

University of Management and Technology,


Lahore Campus
Lab- 03 Manual
Resource Person: Awais Amin
Department of Computer Science
Email: [email protected]
Lab 03: Data Types
Table of Contents

Lab # 03.....................................................................................................................2
3.1 Objective:............................................................................................................2
3.2 Scope:..................................................................................................................2
3.3 Useful Concept:..................................................................................................2
3.4 Examples:...........................................................................................................3
3.5 Lab Exercise.......................................................................................................7
3.6 Home Work.........................................................................................................7

Lab Rubrics

Identify key programming


Areas Problem Understanding structures such as Design/ Development of Solutions
Assessed variables, loops, and
functions.
(CLO 1) (CLO 2) (CLO 3)
No able to identify key
Not able to understand Not able to Model a solution for a given
Poor programming structures such
Problem problem
as variables, loops, and
functions.

Partially able identify Modeled an average solution for


Able to partially understand
Average key programming a given problem using
the problem
structures such as programming principles
variables, loops, and
functions.

Thoroughly identify key


Modeled a proper solution for a given
Very Good Fully Understand the problem programming structures
problem using programming principles
such as variables, loops,

Department of Computer Science, UMT, Lahore. 1 Awais Amin


Lab Manual: Programming Fundamentals
and functions.

Department of Computer Science, UMT, Lahore. 2 Awais Amin


Lab Manual: Programming Fundamentals
3.1Objective:
Learn the Problem Solving and Basics of C++ Language

3.2Scope:
The student should know the following:
 Problem Solving
 Different data types of C and their Use.
 Declaring Variables
 Standard Input and Output
 Writing Complete Programs

3.3Useful Concept:
A list of basic data types of C++, number of bytes used to store these data types in memory of
computer system is:

DATA TYPE MEMORY MINIMUM MAXIMUM


(BYTES) VALUE VALUE
bool 1 Logical Value Logical Value
true/false true/false
char 1 -128 127
unsigned char 1 0 255
short 2 -32768 32767
unsigned short 2 0 65535
int 2 -32768 32767
unsigned int 2 0 65535
long 4 -2147483648 2147483647
unsigned long 4 0 4294967295
float 4 10-38 1038
double 8 10-308 10308
long double 10
104932 104932

Department of Computer Science, UMT, Lahore. 3 Awais Amin


Lab Manual: Programming Fundamentals
Variables
 C++ variable is a named location in a memory where a program can manipulate the
data. This location is used to hold the value of the variable.
 The value of the C++ variable may get change in the program.
 C++ variable might be belonging to any of the data type like int, float, char etc.

Rules for naming C++ variable:


1. Variable name must begin with letter or underscore.
2. Variables are case sensitive
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscore.
5. sum, height, _value are some examples for variable name

Declaring & initializing C++ variable:


 Variables should be declared in the C++ program before to use.
 Memory space is not allocated for a variable while declaration. It happens only on variable
definition.
 Variable initialization means assigning a value to the variable.

S.No Type Syntax Example


1 Variable declaration data_type variable_name; int x, y, z; char flat, ch;
2 Variable data_type variable_name = int x = 50, y = 30; char flag = ‘x’,
initialization value; ch=’l’;

3.4 Examples:

Example 3.1: This example demonstrates declaration and initialization of variables.


#include <iostream> using
namespace std;
int main ()
{int a, b;
int result;
a = 5;
b = 2;
a = a + 1; result = a - b;
cout << result; return 0;}

Here’s the program’s output: 4

Department of Computer Science, UMT, Lahore. 4 Awais Amin


Lab Manual: Programming Fundamentals
Example 3.2: This program illustrates different techniques to initialize a variable.
// initialization of variables #include
<iostream>
using namespace std;

int main ()
{
int a = 5; // initial value: 5
int b(3); // initial value: 3
int c = 2; // initial value: 2. Also try int c{2};
int result; // initial value undetermined

a = a + b; result = a
- c; cout << result;

return 0;
}

Here’s the program’s output: 6

Example 3.3: This program illustrates use of strings.

// my first string #include


<iostream> #include
<string> using namespace
std;

int main ()
{
string myname;
myname = " Abbas Khalid"; cout
<< myname;
return 0;
}

Here’s the program’s output: Abbas Khalid


Example 3.4: This program calculates the area of the circle. The area of the circle
is πr². The value of π is constant that is 3.14 but radius can change so this program
gets the value of radius variable form user and calculate the area on that value.

#include <iostream> using


namespace std;

Department of Computer Science, UMT, Lahore. 5 Awais Amin


Lab Manual: Programming Fundamentals
int main ()
{
float radius,area;
cout<<"Enter radius of circle: "; cin>>radius;
area = 3.14*radius*radius;
cout<<"Area of the circle is:"<< area<<endl;

return 0;
}

Here’s the program’s output:

Example 3.5: This program illustrates the addition on character values.

#include <iostream> using


namespace std;

int main ()
{
char x, y ;
int z ; x =
'a' ;
y = 'b' ;
z=x+y; //Add the assci value of 'a' with assci value of 'b'. cout<<z<<endl;

return 0;
}
Here’s the program’s output: 195

Example 3.6: This program illustrates the use of sizeof() function which is used
to find the memory space allocated for each C++ data type.
#include <iostream>
#include <climits> using
namespace std; int main ()
{
int a; char b;

Department of Computer Science, UMT, Lahore. 6 Awais Amin


Lab Manual: Programming Fundamentals
float c;
double d;
cout<<"Storage size for int data type:"<<sizeof(a)<<endl; cout<<"Storage size for char data
type:"<<sizeof(b)<<endl; cout<<"Storage size for float data type:"<<sizeof(c)<<endl;
cout<<"Storage size for double data type:"<<sizeof(d)<<endl;
return 0;
}
Here’s the program’s output:

Example 3.7: A program to illustrate Octal, Decimal and Hexadecimal


representation using cout.

#include
<iostream> using
namespace std; int
main()
{
int n = 54;
cout << std::oct << "oct - " << n <<
'\n'; cout << std::dec << "dec - " <<
n << '\n'; cout << std::hex << "hex -
" << n << '\n';

return 0;
}

Here’s the program’s output:

Department of Computer Science, UMT, Lahore. 7 Awais Amin


Lab Manual: Programming Fundamentals
3.5 Lab Exercise
Exercise 3.1: Write a program to compute circumference of a circle.
Exercise 3.2: Write a program that takes any ASCII value from user and display next five char
after that ASCII value.
Hints: - if user enters 95, your program should display the char against the ASSCII value
96,97,98,99 and 100.
3.6 Home Work
1. Write a program converts a temperature from Celsius to Fahrenheit. Use the following
formula: F = 1.8 x C + 32.
2. Write a program that reads three integers representing hours, minutes, and seconds of a
time. Then it should calculate the equivalent time in seconds.
3. Write a program that input length and width from the user, it calculates and displays area of
rectangle by using formula Area=(length)(width).
4. Write a program converts a temperature from Celsius to Fahrenheit. Use the following formula:
F = 1.8 x C + 32.
5. Write a program that reads three integers representing hours, minutes, and seconds of a time.
Then it should calculate the equivalent time in seconds.

Department of Computer Science, UMT, Lahore. 8 Awais Amin


Lab Manual: Programming Fundamentals

Department of Computer Science, UMT, Lahore. 9 Awais Amin

You might also like