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

cpp file

Hi

Uploaded by

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

cpp file

Hi

Uploaded by

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

Lab Report

of
PROBLEM SOLVING AND PROGRAMMING (24241104)

SUBMITTED BY

Ankit Gurjar
BTIR24O1007
I semester
Information Technology (Artificial Intelligence and Robotics)

SUBMITTED TO

Dr. Rajni Ranjan Singh Makwana


Associate Professor
Prof. Pooja Tripathi
Assistant Professor

CENTRE FOR ARTIFICIAL INTELLIGENCE


Madhav Institute of Technology & Science
Gwalior - 474005 (MP) est. 1957
Session: 2024-25
INDEX

SERIAL NAME OF THE PROGRAM Page Remark


NO. No.
01. Write program to calculate factorial of a number using function 1

02. Write program to calculate factorial of a number using recursion 2

03. Write program to calculate sum of N natural number using recursion 3

04. Write program to write Fibonacci series using recursion: 4

05. Write program to convert binary to decimal number 5

06. Write program to sort array 6

07. Write program to to search an element in given array 7

08. Write program to find frequency of a element in given array 8,9

09. Write program to find the largest and smallest number in a given array 10

10. Write a program of array of pointer : 11

11. Write a program for pointer to array 12

12. Write a program for constant pointer 13

13. Write a program for pointer to constant : 14

14. Program to find frequency of a variable : 15

15. Program to reverse a string 16

16. Program to reverse the array : 17

17. Write a program to Store Numbers using MALLOC 18

18. Write a program to Store Numbers using CALLOC 19

19. Write a program to Store Data of 3 Students using structure. 20


10. Array of pointers
Code
#include <iostream>
using namespace std;

int main() {

int* numPointers[5];

for(int i = 0; i < 5; i++) {

numPointers[i] = new int;

*numPointers[i] = i * 10;
}

cout << "Values stored in the array of pointers:" << endl;


for(int i = 0; i < 5; i++) {
cout << "numPointers[" << i << "] points to value: "
<< *numPointers[i] << endl;
}

cout << "\nMemory addresses stored in the array:" << endl;


for(int i = 0; i < 5; i++) {
cout << "Address in numPointers[" << i << "]: "
<< numPointers[i] << endl;
}

for(int i = 0; i < 5; i++) {


delete numPointers[i];
}

return 0;
}
Output
Values stored in the array of pointers:
numPointers[0] points to value: 0
numPointers[1] points to value: 10
numPointers[2] points to value: 20
numPointers[3] points to value: 30
numPointers[4] points to value: 40

Memory addresses stored in the array:


Address in numPointers[0]: 0xfa6d88
Address in numPointers[1]: 0xfa6d98
Address in numPointers[2]: 0xfa6da8
Address in numPointers[3]: 0xfa6db8
Address in numPointers[4]: 0xfa6dc8
14. Program to find frequency of a variable :
Code
#include <iostream>
using namespace std;

int main() {
int arr[100], freq[100];
int size, i, j;
bool counted;

cout << "Enter the size of array: ";


cin >> size;

cout << "Enter " << size << " elements: ";
for(i = 0; i < size; i++) {
cin >> arr[i];
freq[i] = -1;
}

for(i = 0; i < size; i++) {


if(freq[i] != -1)
continue;

int count = 1;

for(j = i + 1; j < size; j++) {


if(arr[i] == arr[j]) {
count++;
freq[j] = 0;
}
}
freq[i] = count;
}
cout << "\nFrequency of each element:\n";
for(i = 0; i < size; i++) {
if(freq[i] != 0) {
cout << arr[i] << " occurs " << freq[i]
<< " time(s)" << endl;
}
}
return 0;
}
Output
Enter the size of array: 5
Enter 5 elements: 2 5 4 6 7

Frequency of each element:


2 occurs 1 time(s)
5 occurs 1 time(s)
4 occurs 1 time(s)
6 occurs 1 time(s)
7 occurs 1 time(s)
15.Program to reverse a string
Code :
#include <iostream>
#include <string>
using namespace std;

int main() {
string str;

cout << "Enter a string: ";


getline(cin , str);

int length = str.length();

for (int i = 0; i < length / 2; i++) {


char temp = str[i];
str[i] = str[length - 1 - i];
str[length - 1 - i] = temp;
}

cout << "Reversed string: " << str << endl;

return 0;
}

Output
Enter a string: hello world
Reversed string: dlrow olleh
16.Program to reverse a array
Code
#include <iostream>
using namespace std;

int main() {
int size;

cout << "Enter the size of array: ";


cin >> size;

int arr[size];

cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}

cout << "\nOriginal array: ";


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}

for (int i = 0; i < size/2; i++) {


int temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}

cout << "\nReversed array: ";


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}

return 0;
}
Output
Enter the size of array: 5
Enter 5 elements: 2 4 5 7 6

Original array: 2 4 5 7 6
Reversed array: 6 7 5 4 2
17.Write a program to Store Numbers using MALLOC:
Code
#include <iostream>
using namespace std;

int main() {
int size;
int* numbers;

cout << "How many numbers do you want to store? ";


cin >> size;

numbers = new int[size];

cout << "Enter " << size << " numbers:\n";


for(int i = 0; i < size; i++) {
cout << "Number " << i+1 << ": ";
cin >> numbers[i];
}

cout << "\nStored numbers are: ";


for(int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}

delete[] numbers;

return 0;
}

Output
Enter 3 numbers:
Number 1: 10
Number 2: 15
Number 3: 42

Stored numbers are: 10 15 42


18. Write a program to Store Numbers using CALLOC
Code
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
int size;
int* numbers;

cout << "How many numbers to store? ";


cin >> size;

numbers = (int*)calloc(size, sizeof(int));

if (numbers == NULL) {
cout << "Memory allocation failed!";
return 1;
}

cout << "Enter " << size << " numbers:\n";


for(int i = 0; i < size; i++) {
cout << "Number " << i+1 << ": ";
cin >> numbers[i];
}

cout << "\nStored numbers are: ";


for(int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}

free(numbers);

return 0;
}

Output
Enter 3 numbers:
Number 1: 51
Number 2: 42
Number 3: 65

Stored numbers are: 51 42 65


19. Write a program to Store Data of 3 Students using structure.
Code
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int rollNo;
float marks;
};

int main() {

Student students[3];
for(int i = 0; i < 3; i++) {
cout << "\nEnter details for student " << i+1 << ":\n";

cout << "Enter name: ";


cin >> students[i].name;

cout << "Enter roll number: ";


cin >> students[i].rollNo;

cout << "Enter marks: ";


cin >> students[i].marks;
}
cout << "\nStored Student Details:\n";
for(int i = 0; i < 3; i++) {
cout << "\nStudent " << i+1 << ":\n";
cout << "Name: " << students[i].name << endl;
cout << "Roll Number: " << students[i].rollNo << endl;
cout << "Marks: " << students[i].marks << endl;
}

return 0;
}
Output
Enter details for student 1:
Enter name: ankit
Enter roll number: 01
Enter marks: 56

Enter details for student 2:


Enter name: arnav
Enter roll number: 02
Enter marks: 95

Enter details for student 3:


Enter name: shatiya
Enter roll number: 03
Enter marks: 75
Lab Report
of
PROBLEM SOLVING AND PROGRAMMING (24241104)

SUBMITTED BY

Arnav Soni
BTIR24O1015
I semester
Information Technology (Artificial Intelligence and Robotics)

SUBMITTED TO

Dr. Rajni Ranjan Singh Makwana


Associate Professor
Prof. Pooja Tripathi
Assistant Professor

CENTRE FOR ARTIFICIAL INTELLIGENCE


Madhav Institute of Technology & Science
Gwalior - 474005 (MP) est. 1957
Session: 2024-25
INDEX

SERIAL NAME OF THE PROGRAM Page Remark


NO. No.
01. Write program to calculate factorial of a number using function 1

02. Write program to calculate factorial of a number using recursion 2

03. Write program to calculate sum of N natural number using recursion 3

04. Write program to write Fibonacci series using recursion: 4

05. Write program to convert binary to decimal number 5

06. Write program to sort array 6

07. Write program to to search an element in given array 7

08. Write program to find frequency of a element in given array 8,9

09. Write program to find the largest and smallest number in a given array 10

10. Write a program of array of pointer : 11

11. Write a program for pointer to array 12

12. Write a program for constant pointer 13

13. Write a program for pointer to constant : 14

14. Program to find frequency of a variable : 15

15. Program to reverse a string 16

16. Program to reverse the array : 17

17. Write a program to Store Numbers using MALLOC 18

18. Write a program to Store Numbers using CALLOC 19

19. Write a program to Store Data of 3 Students using structure. 20


10. Array of pointers
Code
#include <iostream>
using namespace std;

int main() {

int* numPointers[5];

for(int i = 0; i < 5; i++) {

numPointers[i] = new int;

*numPointers[i] = i * 10;
}

cout << "Values stored in the array of pointers:" << endl;


for(int i = 0; i < 5; i++) {
cout << "numPointers[" << i << "] points to value: "
<< *numPointers[i] << endl;
}

cout << "\nMemory addresses stored in the array:" << endl;


for(int i = 0; i < 5; i++) {
cout << "Address in numPointers[" << i << "]: "
<< numPointers[i] << endl;
}

for(int i = 0; i < 5; i++) {


delete numPointers[i];
}

return 0;
}
Output
Values stored in the array of pointers:
numPointers[0] points to value: 0
numPointers[1] points to value: 10
numPointers[2] points to value: 20
numPointers[3] points to value: 30
numPointers[4] points to value: 40

Memory addresses stored in the array:


Address in numPointers[0]: 0xfa6d88
Address in numPointers[1]: 0xfa6d98
Address in numPointers[2]: 0xfa6da8
Address in numPointers[3]: 0xfa6db8
Address in numPointers[4]: 0xfa6dc8
14. Program to find frequency of a variable :
Code
#include <iostream>
using namespace std;

int main() {
int arr[100], freq[100];
int size, i, j;
bool counted;

cout << "Enter the size of array: ";


cin >> size;

cout << "Enter " << size << " elements: ";
for(i = 0; i < size; i++) {
cin >> arr[i];
freq[i] = -1;
}

for(i = 0; i < size; i++) {


if(freq[i] != -1)
continue;

int count = 1;

for(j = i + 1; j < size; j++) {


if(arr[i] == arr[j]) {
count++;
freq[j] = 0;
}
}
freq[i] = count;
}
cout << "\nFrequency of each element:\n";
for(i = 0; i < size; i++) {
if(freq[i] != 0) {
cout << arr[i] << " occurs " << freq[i]
<< " time(s)" << endl;
}
}
return 0;
}
Output
Enter the size of array: 5
Enter 5 elements: 2 5 4 6 7

Frequency of each element:


2 occurs 1 time(s)
5 occurs 1 time(s)
4 occurs 1 time(s)
6 occurs 1 time(s)
7 occurs 1 time(s)
15.Program to reverse a string
Code :
#include <iostream>
#include <string>
using namespace std;

int main() {
string str;

cout << "Enter a string: ";


getline(cin , str);

int length = str.length();

for (int i = 0; i < length / 2; i++) {


char temp = str[i];
str[i] = str[length - 1 - i];
str[length - 1 - i] = temp;
}

cout << "Reversed string: " << str << endl;

return 0;
}

Output
Enter a string: hello world
Reversed string: dlrow olleh
16.Program to reverse a array
Code
#include <iostream>
using namespace std;

int main() {
int size;

cout << "Enter the size of array: ";


cin >> size;

int arr[size];

cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}

cout << "\nOriginal array: ";


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}

for (int i = 0; i < size/2; i++) {


int temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}

cout << "\nReversed array: ";


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}

return 0;
}
Output
Enter the size of array: 5
Enter 5 elements: 2 4 5 7 6

Original array: 2 4 5 7 6
Reversed array: 6 7 5 4 2
17.Write a program to Store Numbers using MALLOC:
Code
#include <iostream>
using namespace std;

int main() {
int size;
int* numbers;

cout << "How many numbers do you want to store? ";


cin >> size;

numbers = new int[size];

cout << "Enter " << size << " numbers:\n";


for(int i = 0; i < size; i++) {
cout << "Number " << i+1 << ": ";
cin >> numbers[i];
}

cout << "\nStored numbers are: ";


for(int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}

delete[] numbers;

return 0;
}

Output
Enter 3 numbers:
Number 1: 10
Number 2: 15
Number 3: 42

Stored numbers are: 10 15 42


18. Write a program to Store Numbers using CALLOC
Code
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
int size;
int* numbers;

cout << "How many numbers to store? ";


cin >> size;

numbers = (int*)calloc(size, sizeof(int));

if (numbers == NULL) {
cout << "Memory allocation failed!";
return 1;
}

cout << "Enter " << size << " numbers:\n";


for(int i = 0; i < size; i++) {
cout << "Number " << i+1 << ": ";
cin >> numbers[i];
}

cout << "\nStored numbers are: ";


for(int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}

free(numbers);

return 0;
}

Output
Enter 3 numbers:
Number 1: 51
Number 2: 42
Number 3: 65

Stored numbers are: 51 42 65


19. Write a program to Store Data of 3 Students using structure.
Code
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int rollNo;
float marks;
};

int main() {

Student students[3];
for(int i = 0; i < 3; i++) {
cout << "\nEnter details for student " << i+1 << ":\n";

cout << "Enter name: ";


cin >> students[i].name;

cout << "Enter roll number: ";


cin >> students[i].rollNo;

cout << "Enter marks: ";


cin >> students[i].marks;
}
cout << "\nStored Student Details:\n";
for(int i = 0; i < 3; i++) {
cout << "\nStudent " << i+1 << ":\n";
cout << "Name: " << students[i].name << endl;
cout << "Roll Number: " << students[i].rollNo << endl;
cout << "Marks: " << students[i].marks << endl;
}

return 0;
}
Output
Enter details for student 1:
Enter name: ankit
Enter roll number: 01
Enter marks: 56

Enter details for student 2:


Enter name: arnav
Enter roll number: 02
Enter marks: 95

Enter details for student 3:


Enter name: shatiya
Enter roll number: 03
Enter marks: 75

You might also like