0% found this document useful (0 votes)
19 views5 pages

Lab #2

Uploaded by

Mugheera Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Lab #2

Uploaded by

Mugheera Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB MANUAL PROGRAMMING FUNDAMENTALS

LAB NO. 2 DD/MM/YYYY

PRIMITIVE DATA TYPES AND VARIABLES


Lab outcomes:
After completing this lab, students will be able to:
 Understand and implement data types.
 Understand and implement variables.
Corresponding CLO and PLO:
 CLO-1, PLO-2 (Engineering Knowledge)
Theory:
Primitive Data Types: Primitive data types in C++ are basic data types that are used to define
variables and their values. These data types are built into the C++ language and are fundamental
for performing arithmetic and logical operations on them. C++ has five primitive data types,
namely:
1. Integers (int): Integers are used to store whole numbers without any fractional part. In
C++, the int data type is used to define integer variables. The size of an integer depends
on the system architecture, but it is usually 4 bytes. Here's an example:
int age = 25;
In this example, the variable "age" is defined as an integer and initialized with the value 25.
2. Floating-point numbers (float and double): Floating-point numbers are used to store
decimal numbers. In C++, the float data type is used to define single-precision floating-
point numbers, while the double data type is used to define double-precision floating-
point numbers. The size of a float is 4 bytes, while the size of a double is 8 bytes.
The main difference between single precision and double precision floats is their
precision, or the number of significant digits they can represent. Single precision floats
have a precision of roughly 7 decimal digits, while double precision floats have a
precision of roughly 15-16 decimal digits. This means that double precision floats can
represent numbers with greater accuracy and range than single precision floats.
Here's an example:
float weight = 65.5;
double height = 1.75;
In this example, the variable "weight" is defined as a float and initialized with the value 65.5,
while the variable "height" is defined as a double and initialized with the value 1.75.
LAB MANUAL PROGRAMMING FUNDAMENTALS

3. Characters (char): Characters are used to store single characters such as 'a', 'b', 'c', etc.
In C++, the char data type is used to define character variables. They are stored as ASCII
values in memory and take 1 byte of memory space. Here's an example:
char grade = 'A';
In this example, the variable "grade" is defined as a char and initialized with the character 'A'.
4. Booleans (bool): Booleans are used to store true or false values. In C++, the bool data
type is used to define Boolean variables. They take 1 byte of memory space. Here's an
example:
bool isMarried = true;
In this example, the variable "isMarried" is defined as a bool and initialized with the value true.
5. Void (void): Void is a special data type that is used to specify that a function does not
return any value. Here's an example:
void printHelloWorld() {
std::cout << "Hello World!" << std::endl;
}
In this example, the function "printHelloWorld" is defined with a void return type, indicating that
it does not return any value. When called, it prints the message "Hello World!" to the console.
Variable: A variable is a named storage location that can hold a value of a particular data type.
Declaring Variable: To declare a variable in C++, you need to specify its data type and give it a
name. Here is the general syntax for declaring a variable:
data_type variable_name;
Here, 'data_type' represents the type of data that the variable will hold, such as int, float, char,
bool, etc. And 'variable_name' is the name given to the variable to uniquely identify it within the
program.
For example, to declare an integer variable called ‘age’, you would use the following code:
int age;
Initializing Variable: After declaring a variable, you can assign a value to it using the
assignment operator (=). For example:
age = 25;
Alternatively, you can declare and initialize a variable in a single line of code, like this:
int age = 25;
This creates a variable called age of type int and initializes it with the value 25.
LAB MANUAL PROGRAMMING FUNDAMENTALS

Note that in C++, variables must be declared before they can be used. This means that you
cannot use a variable in your code until you have declared it.
Example:

Activities:
Activity 1.1: What is meant by the following statement?
“Variables must be declared before they can be used.”
Answer: Please write your answer here.
Activity 1.2: How does the compiler know about the type of the variable?
Answer: Please write your answer here.
Activity 1.3: Can we change the value of variable?
Answer: Please write your answer here.
Activity 1.4: Can we change the value of const at the time of declaration? If yes, then
explain when it cannot be changed.
Answer: Please write your answer here.
Activity 1.5: Declare and initialize all types of variables and show their value on the screen.
Code:
Please write or paste your code snap here.
Output:
Please paste your output screenshot here with your Name and CMS mentioned in it.
Activity 1.6: Differentiate between PascalCase and camelCase, and where should these be
used?
Answer: Please write your answer here.
Activity 1.7: Differentiate between float and double datatypes.
Answer: Please write your answer here.
Activity 1.8: Differentiate between int and unsigned int datatypes.
LAB MANUAL PROGRAMMING FUNDAMENTALS

Answer: Please write your answer here.


Activity 1.9: Differentiate between int and long datatypes.
Answer: Please write your answer here.
Observations:
Please write your observation after conducting this lab, you must write in few lines, what did you
learn in this lab
LAB MANUAL PROGRAMMING FUNDAMENTALS

Rubrics
Absent Student is Student can Student has Student has Student
unable to understand followed constructed perfectly
follow the the provided instructions the implemented
provided laboratory to construct functional/ a working
instructions instructions the working model/
properly. and familiar fundamental schematic/ logic/
The student with the lab schematic/ model/ circuit/
can name environment block block block
the (Trainer/ diagram/ diagram/ diagram/
hardware or software/ code/ model code, and code and
Demonstration simulation IDE), but on the have successfully
platform, cannot protoboard/ successfully executed the
but unable implement trainer/ executed lab objective
to on the simulation the in Realtime
implement platform software. program/ or in a
anything practically run circuit simulation
practically or on the on software environment
or on the software platform and
software produced the
desired
results
Category Ungrade Very Poor Poor Fair Good Excellent
d
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.01 - 0.20 0.21 - 0.40 0.41 - 0.60 0.61 - 0.80 0.81 - 1.0
Date Total Instructor’s Signature
Marks

Report Plagiarized Requirements Observations Appropriate Correctly


not content are listed and are recorded computations drawn
submitted presented experimental along with or numerical conclusion
or procedure is detailed analysis is with
Laboratory
incomplete presented procedure performed exact
Reports
submission results and
complete
report in all
respects
Category Ungrade Very Poor Poor Fair Good Excellent
d
Percentage [0] [1-20] [21-40] [41-60] [61-80] [81-100]
Marks 0.0 0.01 - 0.20 0.21 - 0.40 0.41 - 0.60 0.61 - 0.80 0.81 - 1.0
Date Total Instructor’s Signature
Marks

You might also like