Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
CS – 114: Fundamentals of Programming
LAB # 06: Arrays in C++
Course Instructor: Dr. Asim Khan
Lab Instructor: Engr. Ayesha Khanam
Sr. No. Student’s Name CMS ID
1 Abdul wahab 544532
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
LAB # 06: Arrays in C++
Software Requirement(s): MS Visual Studio
Lab Objective:
The main objective of this lab is to familiarize students with the fundamental concepts of arrays
in C++. By the end of this lab, students should be able to effectively declare, initialize, and
manipulate arrays to store and process multiple data elements efficiently. Additionally,
students will learn how to traverse arrays using loops, access specific elements, and perform
common operations such as searching, sorting, and updating array values. Through hands-on
exercises, students will develop problem-solving skills by implementing array-based solutions
for various computational problems.
Lab Tasks:
1. Write a C++ program that asks the user to enter 10 integers and stores them in a 1D
array. The program should then use a loop to traverse the array and check each number
using if, else if, and else statements:
● If the number is even, print "Even number found: <number>".
● If the number is odd, print "Odd number found: <number>".
● If the number is negative, print "Negative number found: <number>".
Finally, count and display how many numbers were even, how many were odd, and how
many were negative.
#include <iostream>
using namespace std;
int main()
int evenCount = 0;
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
int oddCount = 0;
int negativeCount = 0;
int arr[10] = { 12, 2, 3, -4, 5, 62, 7, 8, 49, 10 };
for (int i = 0; i < 10; i++)
if (arr[i] % 2 == 0)
cout << arr[i] << " is even." << endl;
evenCount++;
else
cout << arr[i] << " is odd." << endl;
oddCount++;
if (arr[i] < 0)
cout << arr[i] << " is negative." << endl;
negativeCount++;
}
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
cout << "Even numbers :" <<evenCount << endl;
cout << " Odd numbers :" <<oddCount << endl;
cout << " Negative numbers: " <<negativeCount << endl;
system("pause");
return 0;
}
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
2. Write a C++ program that asks the user to enter two 3x3 matrices (2D arrays). The
program should take the user defined input for both matrices and it should:
● Perform matrix addition and store the result in a third matrix.
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
● Perform matrix subtraction and store the result in another matrix.
● Display the original matrices, the sum matrix, and the difference matrix
Make sure to use nested loops for input, addition, subtraction, and displaying the
matrices.
#include <iostream>
using namespace std;
int main()
int A[3][3], B[3][3], Sum[3][3], Diff[3][3];
cout << "Enter first 3x3 matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> A[i][j];
cout << "Enter second 3x3 matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> B[i][j];
}
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Sum[i][j] = A[i][j] + B[i][j];
Diff[i][j] = A[i][j] - B[i][j];
cout << "\nFirst matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << A[i][j] << " ";
cout << endl;
cout << "\nSecond Matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << B[i][j] << " ";
cout << endl;
cout << "\nSum Matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
cout << Sum[i][j] << " ";
cout << endl;
cout << "\nDifference matrix:\n";
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << diff[i][j] << ;
cout << endl; }
system("pause");
return 0;
}
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
3. Write a C++ program that allows the user to enter the marks of N students in M
subjects, where both values are specified by the user. The program should use a 2D
array to store the marks of each student for all subjects. It should then calculate the
total marks for each student by summing the marks of all subjects using nested loops.
Finally, the program should display the total marks for each student in a clear and
structured format.
#include <iostream>
using namespace std;
int main() {
int Marksstudent1 = 0;
int Marksstudent2 = 0;
int Marksstudent3 = 0;
int Subject1 = 0;
int Subject2 = 0;
int Subject3 = 0;
int arr[3][3] = {
{79, 92, 93},
{84, 85, 92},
{97, 98, 94}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
for (int j = 0; j < 3; j++) {
Marksstudent1 += arr[0][j];
}
for (int j = 0; j < 3; j++) {
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
Marksstudent2 += arr[1][j];
}
for (int j = 0; j < 3; j++) {
Marksstudent3 += arr[2][j];
}
cout << "Total marks of student 1: " << Marksstudent1 << endl;
cout << "Total marks of student 2: " << Marksstudent2 << endl;
cout << "Total marks of student 3: " << Marksstudent3 << endl;
for (int i = 0; i < 3; i++) {
cout << "Student " << i + 1 << " marks:\n";
for (int j = 0; j < 3; j++) {
cout << " Subject " << j + 1 << " = " << arr[i][j] << endl;
}
cout << endl;
}
return 0;
}
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
4. Write a C++ program that asks the user to enter a string (character array). The program
should then reverse the string without using any built-in functions and display the
reversed string as output.
#include <iostream>
using namespace std
int main() {
char str[50];
int len = 0;
cout << "Enter a word: ";
cin >> str;
while (str[len] != '\0') {
len++;
cout << "Reversed word: ";
for (int i = len - 1; i >= 0; i--) {
cout << str[i];
}
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi
return 0;