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

CPP Lab Ramamoorthy

Uploaded by

kanizz2006
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)
21 views

CPP Lab Ramamoorthy

Uploaded by

kanizz2006
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/ 20

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

23CS101- PROBLEM SOLVING USING C++

ACADEMIC YEAR 2024-2025 (ODD)

PRACTICAL RECORD

Name: Ramamoorthy.K

Reg.No: 2403727714921075

Branch: B.E. CSE (Cyber Security)

Class/Section: CYBER - B

Year/Sem: 1st Year/1st Sem


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
23CS101- PROBLEM SOLVING USING C++

ACADEMIC YEAR 2024-2025

(ODD) PRACTICAL RECORD

Name: Ramamoorthy. K Reg.no:2403727714921075

Class: CYBER-B Branch: B.E. CSE (Cyber Security)

BONAFIDE CERTIFICATE

Certified bonafide record of work done by Mr/Ms.

……………………………………… Reg.No during the academic year 2024-

2025(ODD SEM)

Faculty Incharge HOD

Submitted for End Semester practical Examination, held on……………………….

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX- LIST OF PROGRAMS

Pr Date Title of the Program Page No


o
g
N
o

1. Practice of C programming using Branching and


iterative constructs

2.
Programs using arrays and strings

3.
Programs using functions

4.
Programs using Structures and pointers

5.
Programs using classes and objects

6.
Programs using constructors and destructors

7. Programs using method overloading, operator


overloading and polymorphism concepts

8.
Programs using friend class

9.
Programs using virtual function and abstract class

10.
Programs using inheritance concepts

11.
Programs using exception handling concepts

12.
Programs using files
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Rubrics for Evaluating Laboratory
Subject Code : 23CS101

Lab Name : Problem solving using C++

Method: Lab Reports and Observation of Faculty In

charge Outcomes Assessed:

a) Graduates will demonstrate knowledge of mathematical, scientific and multidisciplinary approach for
problem solving.

b) Graduates will be able to apply their knowledge in various programming skills to create solutions for
product based and application based software.

c) Graduates will possess the ability to create real time solutions for different projects by using modern tools
prevailing in the current trends.

e) Graduates attain advanced knowledge in the stream of Computer Science and Engineering to develop and
maintain the simple and complex information systems.
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Reg No:

Name of the Student:

Name of the Lab:

Components Exp No and Date

Ex-1 Ex-2 Ex- Ex- Ex- Ex- Ex- Ex- Ex- Ex-
3 4 5 6 7 8 9 10

Aim & Algorithm


20 Marks

Coding
30 Marks

Compilation
&
Debuggin
g 30
Marks
Execution &
Results
10 Marks
Documentation &
Viva
10 Marks

Total

Staff In-charge
Ex No: 1 Practice of C programming using Branching and iterative constructs

AIM:
To write a C++ program that prints the given number pattern using nested loops .

ALGORITHM:
1. Start.
2. Initialize a variable num = 1 to hold the current number to be printed.
3. Loop through the rows:
● Use a for loop to iterate over rows, where i represents the current row number.

● The number of elements in each row is equal to the current row number i.
4. Loop through the columns within each row:
● Use another for loop inside the first loop to control the number of columns (elements)
printed in each row. The loop runs from 1 to i.
● In each iteration of the inner loop, print the current value of num, then increment num
by 1.
5. After printing each row, move to the next line.
6. End.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int k=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
cout<<k<<" ";
k++;
}
cout<<"\n";
}
return 0;
}
Sample 1 Input
4
Sample 1 Output
1
23
456
7 8 9 10
RESULT:
Thus, the program prints the number pattern in Right angled triangle pattern using nested
loops.
Ex No: 2 Programs using arrays and strings

AIM:
To find the value that occurs the most in a given array filled with values 1, 2, or 3. In the case of a tie,
the smallest value among the most frequent ones is selected.

ALGORITHM:
1. Input: A list (array) consisting of numbers where each element is either 1, 2, or 3.
2. Initialize Counters:
● Set up three counters: count_1, count_2, and count_3 to track occurrences of the values 1, 2,
and 3, respectively.
3. Count the Occurrences:
● Traverse the array using a loop.

● For each element, increase the respective counter based on whether the element is 1, 2, or
3.
4. Compare the Counts:
● Compare the values of count_1, count_2, and count_3.

● If one count is greater than the others, that value is the result.

● If there is a tie (two or more counts are equal), choose the smallest value among them.
5. Output: Print the value that occurs the most or the smallest value in case of a tie.

PROGRAM:
#include <iostream>
using namespace std;
int main() {
int size;
cin>>size;
int arr[size],c1=0,c2=0,c3=0;
for(int ind=0;ind<size;ind++)
{
cin>>arr[ind];
if (arr[ind]==1)
c1+=1;
else if (arr[ind]==2)
c2+=1;
else if (arr[ind]==3)
c3+=1;
}
if (c1>=c2&&c1>=c3)
cout<<1;
else if(c2>=c3)
cout<<2;
else
cout<<3;
return 0;
}

Sample 1 Input
3
121
Sample 1 Output
1

RESULT:
The program successfully identifies the value that occurs the most in the array, and in the case of a tie,
it prints the smaller value.

EX NO: 3 Programs using functions

AIM:
To reverse the digits of a given number using user-defined functions in C++.

ALGORITHM:
1. Input: Read a number from the user.
2. Define a Function:
● Create a user-defined function reverse(int n) to reverse the number.
3. Reverse Logic:
● Initialize a variable reversed to 0.

● Repeat the following steps while the number n is not zero:


o Extract the last digit of n using n % 10.
o Multiply reversed by 10 and add the extracted digit.
o Remove the last digit from n by dividing it by 10 (n = n / 10).
4. Return the Result: Return the reversed number.
5. Output: Call the function in the main program and print the reversed number.

PROGRAM:
#include<iostream>
using namespace std;
int reverse (int n)
{
int reversed=0;
while(n!=0){
reversed=(reversed*10)+n%10;
n=n/10;
}
return reversed;
}

int main() {
int n;
cin>>n;
cout<<reverse(n);
}

Sample 1 Input
1234
Sample 1 Output
4321
RESULT:
The C++ program successfully reverses the digits of a given number using a user-defined function.
EX NO: 4 Programs using Structures and Pointers

AIM:
To write a C++ program to find the maximum element in an array using pointers.

ALGORITHM:
● Input: Read the size of the array and the array elements from the user.

● Initialize Pointers:
● Declare a pointer ptr to point to the first element of the array.
● Initialize a variable max to store the maximum value (initially assign the value of the first
element).
● Pointer Traversal:
● Use the pointer ptr to traverse the array elements.
● Compare the current element (*ptr) with max:
o If *ptr is greater than max, update max.

● Continue the Traversal:


● Move the pointer to the next element by incrementing it.
● Repeat this process for all elements of the array.
● Output: After traversing all elements, print the maximum element found in the array.

PROGRAM:
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int *ptr,max=0;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
*ptr=arr[i];
if(*ptr>max)
max=*ptr;}
cout<<max;
}
Sample 1 Input
5
2
3
6
8
1
Sample 1 Output
8 is the maximum element

RESULT:
The C++ program successfully finds the maximum element in an array using pointers.

EX NO: 5 Programs using class and objects

AIM:
To write a C++ program that sorts objects (representing employees) based on their experience and
displays their details.

ALGORITHM:
● Define a Class:
Create a class Employee with attributes: name, experience, and salary.
● Input:
Read the details (name, experience, salary) of multiple employees from the user.
● Sorting:
Use a sorting algorithm to sort the employees based on their experience in descending
order.
● Display:
After sorting, display the details of the employees in the sorted order.

PROGRAM:
#include <iostream>
using namespace std;

class Employee
{
public:
string name;
string qualification;
string gender;
int experience;

void input()
{
cin >> ws;
getline(cin, name);
getline(cin, qualification);
getline(cin, gender);
cin >> experience;
}
void display() const
{
cout << name << endl;
cout << qualification << endl;
cout << experience << endl;
}
};

void sortEmployees(Employee arr[], int n)


{
for (int i = 0; i < n - 1; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (arr[i].experience < arr[j].experience)
{
Employee temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

int main() {
int n;
cin >> n;

Employee employees[n];

for (int i = 0; i < n; ++i)


{
employees[i].input();
}

sortEmployees(employees, n);

for (int i = 0; i < n; ++i)


{
employees[i].display();
}

return 0;
}
Sample 1 Input
2
ram
Be cse
male
2
pravin
Be ece
male
3

Sample 1 Output
pravin
Be ece
3
ram
Be cse
2
RESULT:
The program successfully sorts the employee objects based on their experience in descending
order and displays their details.

You might also like