DS LAB 01: C++ Revision and Array List: A Beginner-Friendly Lab Guide
Introduction
C++ is a powerful, high-performance programming language widely used in software development, game programming, system applications, and competitive programming. For beginners, understanding the foundational concepts of C++ is essential before moving toward advanced topics.
This lab on C++ Revision and Array List is designed to strengthen students’ understanding of core C++ concepts through simple programs. It focuses on building a solid programming foundation by revisiting syntax, control structures, arrays, and functions, while also introducing the concept of an Array List.
Objective of the Lab
The primary objective of this lab is to help students become familiar with the C++ programming language and its Integrated Development Environment (IDE) through hands-on practice.
By the end of this lab, students will:
-
Understand the basic syntax of C++
-
Write and execute simple C++ programs
-
Gain clarity on control statements, functions, and arrays
-
Learn the fundamental concept of an Array List for storing multiple values efficiently
This lab serves as a strong revision platform for students who are new to C++ or need to refresh their programming basics.
Activity Outcomes
After completing this lab, students will be able to:
-
Understand and write basic C++ syntax
-
Use data types and operators correctly
-
Apply control flow statements for decision-making and looping
-
Implement arrays and functions in C++ programs
Basic Syntax of C++
C++ programs follow a structured syntax. A simple C++ program includes header files, a main function, and executable statements.
Example: Basic C++ Program
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to C++ Programming!";
return 0;
}
Key Points:
-
#include <iostream>is used for input and output -
main()is the entry point of a C++ program -
coutis used to display output on the screen
Data Types and Operators in C++
C++ supports multiple data types to store different kinds of data.
Common Data Types
-
int– stores integers -
float– stores decimal values -
double– stores large decimal values -
char– stores characters -
bool– stores true or false values
Operators in C++
-
Arithmetic Operators:
+,-,*,/,% -
Relational Operators:
==,!=,>,< -
Logical Operators:
&&,||,!
Control Flow Statements in C++
Control flow statements allow the program to make decisions and repeat tasks.
Conditional Statements
- if
- if-else
- switch
Looping Statements
- for
- while
- do-while
Example: If-Else Statement
int number = 10;
if(number > 0) {
cout << "Positive Number";
} else {
cout << "Negative Number";
}
Arrays and Functions in C++
Arrays in C++
An array is a collection of elements of the same data type stored at contiguous memory locations.
int marks[5] = {85, 90, 78, 92, 88};
Arrays are useful when handling multiple values efficiently.
Functions in C++
Functions help divide a program into smaller, reusable blocks.
int add(int a, int b) {
return a + b;
}
Functions improve code readability, reusability, and maintainability.
Concept of Array List in C++
While C++ does not have a built-in structure named Array List like Java, similar functionality can be achieved using dynamic arrays or the vector container from the Standard Template Library (STL).
Example Using Vector (Array List Behavior)
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
for(int i : numbers) {
cout << i << " ";
}
return 0;
}
Advantages of Array List (Vector):
-
Dynamic size
-
Easy insertion and deletion
-
Efficient memory management
Activity 1: Single Line, Multi Line comments and basic Input/ Output in C++.
// Single line Comment
/*
* Multiple line
* comment
*/
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
Output
The output will be “Hello World!”.
Activity 2: Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for example:
• int – stores integers (whole numbers), without decimals, such as 123 or -123
• double – stores floating point numbers, with decimals, such as 19.99 or -19.99
• char – stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotes
• string – stores text, such as “Hello World”. String values are surrounded by double quotes
• bool – stores values with two states: true or false
Solution:
#include <iostream>
using namespace std;
int main ()
{
float a=5.5; // initial value: 5
int b(3); // initial value: 3
int c{2}; // initial value: 2
float result; // initial value undetermined
a = a + b;
result = a - c;
cout << result;
return 0;
}
Output
Value present in result variable will be shown on Screen
Activity 3: Using cin, extraction operator (>>) and cout (<<). cin a program to illustrate enhanced for loop
#include <iostream>
using namespace std;
int main ()
{
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
return 0;
}
Output
Type a number: 100 //suppose user enters 100
Your number is: 100
Activity 4: Write a program to use string data type in C++
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
Output
This is the initial string content
This is a different string content
Activity 5: Write a program to use arithmetic operators in C++
#include <iostream>
using namespace std;
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
}
Output
5
Activity 6: Write a program to use relational operators in C++
#include <iostream>
using namespace std;
int main ()
{
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c << '\n';
}
Output
7
Activity 7: Write a program to use if-else statement in C++
#include <iostream>
using namespace std;
int main ()
{
int x;
cin>>x;
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
}
Output
The ouput depends upon the value of ‘x’ entered by the user, the output will be either x is positive or x is negative or x is 0.
Activity 8: Write a program to use while loop in C++
#include <iostream>
using namespace std;
int main ()
{
int n = 10;
while (n>0) {
cout << n << ", ";
--n;
}
cout << "liftoff!\n";
}
Output
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!
Activity 9: Write a program to use do-wḥile loop in C++
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
do {
cout << "Enter text: ";
getline (cin,str);
cout << "You entered: " << str << '\n';
} while (str != "goodbye");
}
Output
Program repeatedly takes a string as input then displays it saying “You entered….” Until user types goodbye.
Activity 10: Write a program to apply the switch statements.
#include <iostream>
using namespace std;
int main ()
{
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout<<"Enter valid day";
}
}
Output
Outputs “Thursday” as variable day is initialized to 4
Activity 11: Write a program that will add two numbers in a function and call that function in main.
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}
Output
The result is 8
Activity 12: Write a program to use the concept of Arrays in C++
#include <iostream>
using namespace std;
int foo [] = {16, 2, 77, 40, 12071};
int n, result=0;
int main ()
{
for ( n=0 ; n<5 ; ++n )
{
result += foo[n];
}
cout << result;
return 0;
}
Output
12206
3) Graded Lab Tasks
Note: The instructor can design graded lab activities according to the level of difficulty and complexity of the solved lab activities. The lab tasks assigned by the instructor should be evaluated in the same lab
Lab Task 1
Write down a program that find ∑X2, where input for X and starting and stopping value is entered by the user.
Hint:
C++ Program to Find ∑X² (Sum of Squares)
Problem Description
Write a C++ program that takes the starting value and ending value of a variable X from the user and calculates the sum of squares:
∑X2=Xstart2+(Xstart+1)2+⋯+Xstop2\sum X^2 = X_{start}^2 + (X_{start}+1)^2 + \dots + X_{stop}^2
Lab Task 2
Write a computer program to apply the concepts of ArrayList. The array list will include the following functions:
1. Insert the value at end of the list
2. Insert the value at start of the list
3. Insert the value after specific value
4. Insert the value before specific value
5. Display the array list
6. Delete the value from end of the list
7. Delete the value from start of the list
8. Delete specific value
Logic:
Array List Operations – Logic
1. Insert the value at the end of the list
-
Check if there is available space (or increase size dynamically).
-
Place the new value at the current last index.
-
Increment the size of the list by 1.
2. Insert the value at the start of the list
-
Shift all existing elements one position to the right.
-
Insert the new value at index
0. -
Increment the size of the list by 1.
3. Insert the value after a specific value
-
Traverse the list to find the target value.
-
If found:
-
Shift all elements after that position one step to the right.
-
Insert the new value at the next index.
-
Increase the list size by 1.
-
-
If not found, display an appropriate message.
4. Insert the value before a specific value
-
Traverse the list to locate the target value.
-
If found:
-
Shift the target value and subsequent elements one position to the right.
-
Insert the new value at the target index.
-
Increase the list size by 1.
-
-
If not found, display an appropriate message.
5. Display the array list
-
Start from index
0. -
Traverse the list up to the last index.
-
Print each element sequentially.
6. Delete the value from the end of the list
-
Check if the list is not empty.
-
Remove the last element.
-
Decrement the size of the list by 1.
7. Delete the value from the start of the list
-
Check if the list is not empty.
-
Shift all elements one position to the left.
-
Decrease the size of the list by 1.
8. Delete a specific value
-
Traverse the list to find the target value.
-
If found:
-
Shift all elements after the target index one position to the left.
-
Reduce the list size by 1.
-
-
If not found, display an appropriate message.
Lab Task 3
Using while loop apply linear search on the Array List you developed in Lab Task 2
Conclusion
The C++ Revision and Array List lab plays a crucial role in strengthening programming fundamentals. By practicing basic syntax, data types, control flow statements, arrays, and functions, students gain confidence in writing C++ programs. The introduction to Array List concepts using vectors further prepares students for real-world programming scenarios.
This lab ensures a smooth transition from beginner-level understanding to more advanced C++ concepts.