SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering
Computer Engineering Department
Program: B.Tech Integrated Sem V
Course: Basic Data Structures
2025
Experiment No.03
PART A
(PART A: TO BE REFFERED BY STUDENTS)
A.1 Aim: To study concepts of 2D array, Structure
Task 1: In a class, there are six students, each having grades for four subjects. Write a program
using a 2D array to calculate (i) the total grades for each student and (ii) the average grade for
each subject.
Task 2: In a small company there are five salesmen. Each salesman is supposed to sell three
products. Write a program using a 2D array to print (i) the total sales by each salesman and (ii)
total sales of each item.
Task 3: An industrial plant tracks the energy consumption of five machines across four different
shifts. Write a program using a 2D array to find (i) the total energy consumption for each
machine and (ii) the total energy consumption for each shift.(iv)Identifies the machine with the
lowest average energy consumption across all shifts (i.e., most energy-efficient).(v)Identifies the
shift with the highest total energy consumption.
Task 4: Create a structure that store details of BankAccount with account number, balance and
customer name.
Task 5: Write a C\C++ program-using concept of structures, to store details of BankAccount
with account number, balance and customer name. The program must input details from the user
and display the details.
Task 6: Modify the program written for task 2. The program must store details for n accounts.
User gives the value of n. The program must provide a search function that searches details of
particular bank account.
A.2 Prerequisite:
Knowledge of Recursion and Pointer
A.3 Outcome:
After successful completion of this experiment students will be able to
1. Demonstrate the use of recursion and pointers
A.4 Theory:
Recursive Function:
A recursive function is one which calls itself. This is another complicated idea which you are
unlikely to meet frequently. Recursive functions are useful in evaluating certain types of
mathematical function. You may also encounter certain dynamic data structures such as linked
lists or binary trees. Recursion is a very useful way of creating and accessing these structures.
Advantages and Disadvantages of Recursion
Recursion is more elegant and requires few variables which make program clean. Recursion can
be used to replace complex nesting code by dividing the problem into same problem of its sub-
type. On other hand,memory usage of recursion is higher as it uses stack memory.
Pointer is variable that contains memory address of another variable. From the definition we can
say that the pointer is a variable and you can assign different values of the pointer variable. The
value contained by the pointer must be an address that indicates the location of another variable
in the memory.
Left Value & Right Value
For instance, after the integer variable x is declared and assigned to a value like this:
int x;
X=7;
The variable x now has two values:
Left value: 1000
Right value: 7
Where 1000 is the address of the variable x.
The general form of a pointer declaration is
data-type *pointer-name;
The following shows different types of pointers:
char *ptr_c; /* declare a pointer to a character */
int *ptr_int; /* declare a pointer to an integer */
float *ptr_flt; /* declare a pointer to a floating-point */
Example 1:
#include <stdio.h>
int main ()
{
int var1;
char var2[10];
printf("Address of var1 variable: %x\n", &var1 );
printf("Address of var2 variable: %x\n", &var2 );
return 0;
}
Output -
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6
Example 2:
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Output –
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
NULL Pointers in C
It is always a good practice to assign a NULL value to a pointer variable in case you do not have
exact address to be assigned. This is done at the time of variable declaration. A pointer that is
assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries.
Consider the following program:
#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0; }
Output –
The value of ptr is 0
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Portal)
Roll No.C063 Name:Vidhan Shah
Program:BTI Division:C
Semester:5 Batch :1
Date of Experiment: 1/8/25 Date of Submission: 2/8/25
Grade :
Task 1:
#include<iostream>
using namespace std;
int main(){
int students=6,sub=4,i,j;
int g[students][sub];
for(i=0;i<students;i++){
cout<<"\nstudent "<<i+1<<" :- "<<endl;
int sum=0;
for(j=0;j<sub;j++){
cout<<"Enter marks for subject "<<j+1<<" :- "<<endl;
cin>>g[i][j];
sum= sum + g[i][j];
cout<<"Your sum is :- "<<sum<<endl;
for(j=0;j<sub;j++){
int subtotal=0;
float avg=0;
for(i=0;i<students;i++){
subtotal= subtotal + g[i][j];
avg=subtotal/students;
cout<<"Your avg for subject "<<j+1<<" is :- "<<avg<<endl;
return 1;
}
Output:-
Task 2:
#include<iostream>
using namespace std;
int main(){
int salesm=5,prod=3,i,j;
int g[salesm][prod];
for(i=0;i<salesm;i++){
cout<<"\nSalesMan "<<i+1<<" :- "<<endl;
int sum=0;
for(j=0;j<prod;j++){
cout<<"Enter price for product "<<j+1<<" :- "<<endl;
cin>>g[i][j];
sum= sum + g[i][j];
cout<<"Your Total sales is :- "<<sum<<endl;
for(j=0;j<prod;j++){
int subtotal=0;
float avg=0;
for(i=0;i<salesm;i++){
subtotal= subtotal + g[i][j];
cout<<"Your total sales for product "<<j+1<<" is :- "<<subtotal<<endl;
}
return 1;
Output:-
Task3:
using namespace std;
int main(){
int mach=5,shift=4,i,j;
int lowmach,highshif,mini,maxi;
int g[mach][shift];
for(i=0;i<mach;i++){
cout<<"\nMachine "<<i+1<<" :- "<<endl;
int sum=0;
float avg=0;
for(j=0;j<shift;j++){
cout<<"Enter energy consumption for Shift "<<j+1<<" :- "<<endl;
cin>>g[i][j];
sum= sum + g[i][j];
cout<<"Your Total Energy consumption is :- "<<sum<<endl;
avg=sum/mach;
if(avg<mini){
mini=avg;
lowmach=i;
}
}
cout<<"\nThe lowest Average Energy consumed is by machine "<<lowmach+1<<endl;
for(j=0;j<shift;j++){
int subtotal=0;
float avg=0;
for(i=0;i<mach;i++){
subtotal= subtotal + g[i][j];
cout<<"\nYour total Energy consumption for shift "<<j+1<<" is :- "<<subtotal<<endl;
subtotal= subtotal + g[i][j];
if(subtotal>maxi){
maxi=subtotal;
highshif=j;
cout<<"\nThe Highest Energy consumed is by shift "<<highshif+1<<endl;
return 1;
Output:-
Task 4:
#include <iostream>
#include <string>
using namespace std;
struct BankAccount {
int accountNumber=4141434546;
int balance=100999999;
string customerName= "Vidhan Shah";
};
int main() {
BankAccount account;
cout << "\n--- Bank Account Details ---\n";
cout << "Account Number: " << account.accountNumber << endl;
cout << "Customer Name : " << account.customerName << endl;
cout << "Balance : " << account.balance << endl;
return 0;
Output:-
Task 5:
#include <iostream>
#include <string>
using namespace std;
struct BankAccount {
int accountNumber;
double balance;
string customerName;
};
int main() {
BankAccount account;
cout << "Enter account number: ";
cin >> account.accountNumber;
cout << "Enter customer name: ";
cin.ignore();
getline(cin, account.customerName);
cout << "Enter account balance: ";
cin >> account.balance;
cout << "\n--- Bank Account Details ---\n";
cout << "Account Number: " << account.accountNumber << endl;
cout << "Customer Name : " << account.customerName << endl;
cout << "Balance : " << account.balance << endl;
return 0;
Output:-
Task 6:
#include<iostream>
#include<string>
using namespace std;
struct BankAccount {
int acnumber,money;
string name;
};
int main(){
int i,n;
BankAccount* accounts = new BankAccount[n];
cout<<"Enter number of accounts :- "<<endl;
cin>>n;
for(i=0;i<n;i++){
cout<<"\nEnter for Account "<<i+1<<endl;
cout<<"\nEnter Customer Name :- "<<endl;
cin>>accounts[i].name;
cout<<"\nEnter Account Number :- "<<endl;
cin>>accounts[i].acnumber;
cout<<"\n Bank Balance :- "<<endl;
cin.ignore();
getline(cin, accounts[i].name);
int searchi;
cout << "\nEnter account number to search: ";
cin >> searchi;
bool found = false;
for (int i = 0; i < n; i++) {
if (accounts[i].acnumber == searchi) {
cout << "\nAccount found:\n";
cout << "Account Number: " << accounts[i].acnumber << endl;
cout << "Customer Name : " << accounts[i].name << endl;
cout << "Balance : " << accounts[i].money << endl;
found = true;
break;
}
if (!found) {
cout << "Account with number " << searchi << " not found." << endl;
delete[] accounts;
return 1;
Output:-
B.5 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above
and learning/observation noted in section B.3)
I learned essential C++ concepts: using 2D arrays to handle tabular data for calculations like
totals and averages, defining and managing multiple records using structures, and applying
recursion to solve classic problems like GCD and Fibonacci series. These fundamentals
strengthen your skills in data organization, memory management, and algorithmic thinking.
Mastering these topics provides a strong base for further programming challenges and real-world
applications in C++.
B.6 Question of Curiosity:
Write a C\C++ program with recursive functions to perform following
a) Find the greatest common divisor (GCD) of two integers.
b) Print Fibonacci series up to n term
a)
input:-
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
int result = gcd(num1, num2);
cout << "GCD is: " << result << endl;
return 0;
}
output:-
b)
input:-
#include <iostream>
using namespace std;
void printFibonacci(int n, int first = 0, int second = 1) {
if (n == 0)
return;
cout << first << " ";
printFibonacci(n - 1, second, first + second);
int main() {
int n;
cout << "Enter number of terms: ";
cin >> n;
printFibonacci(n);
cout << endl;
return 0;
output:-