0% found this document useful (0 votes)
9 views3 pages

OOP Pr-1

The document contains C++ code for two functionalities: swapping two numbers using both a temporary variable and by reference, and implementing a bubble sort algorithm to sort an array of integers. It includes user prompts for input and displays the results after performing the operations. The code demonstrates basic programming concepts such as functions, arrays, and control structures.

Uploaded by

anises208
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)
9 views3 pages

OOP Pr-1

The document contains C++ code for two functionalities: swapping two numbers using both a temporary variable and by reference, and implementing a bubble sort algorithm to sort an array of integers. It includes user prompts for input and displays the results after performing the operations. The code demonstrates basic programming concepts such as functions, arrays, and control structures.

Uploaded by

anises208
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/ 3

//prajwal popalghat SEA-135

//swapping of number

#include<iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"entre first number: ";
cin>>a;
cout<<"entre second number: ";
cin>>b;
temp=a;
a=b;
b=temp;
cout<<a<<endl<<b;
}

Output:

entre first number: 10


entre second number: 9
9
10
//using reference
#include<iostream>
using namespace std;
int a,b,temp;
void swap(int &x,int &y);
int main()
{
cout<<"enter first number:";
cin>>a;
cout<<"enter second number:";
cin>>b;
cout<<"numbers before swapping are a="<<a<<" b="<<b<<endl;
swap(a,b);
cout<<" the numbers aft swapping are a="<<a<<" b="<<b<<endl;
return 0;
}
void swap(int &a,int &b){
temp=a;
a=b;
b=temp;
cout<<"value after swaping a="<<a<<" b="<<b<<endl;
}
Output:
enter first number:20
enter second number:10
numbers before swapping are a=20 b=10
value after swaping a=10 b=20
the numbers aft swapping are a=10 b=20
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp; }
}}}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
bubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;}
Output:
Enter the number of elements: 5
Enter the elements: 9 33 6 91 59
Sorted array: 6 9 33 59 91

You might also like