Oop Assignmnet 1
Oop Assignmnet 1
PROGRAMING]
20F-0114_BCS_2C
Question no.1
#include<iostream>
#include<iomanip>
#include<ctime>
#include<cstdlib>
using namespace std;
void main()
{
int List[10], sum = 0, average;
int* ptr;
ptr = List;
srand(time(0));
cout << "Elements of array are :";
cout << endl;
for (int i = 0; i < 10; i++)
{
*ptr = rand() % 10; //random number
cout <<setw(2)<< *ptr;
sum = sum + *ptr; //sum
ptr++; // Address of next element
}
cout << endl;
cout << "sum of array: " << sum;
average = sum / 10; //Average
cout << endl;
cout <<setprecision(3)<< "Average of array: " <<average;
cout << endl;
system("pause");
}
Question no.2
Insertion sorting
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
void main()
{
int n, arr[100];
srand(time(0));
cout << "Enter size of array :";
cin >> n; //Size integer
cout << "Elements of array: " ;
for (int i = 0; i < n; i++)
{
arr[i]=rand()%10; //Genrating random numbers
cout << arr[i] << " ";
}
cout << endl;
cout << "Array after Sorting: ";
insertionSort(arr, n); //Calling the insertion function
display(arr, n);//Calling the displayfunction
system("pause");
}
Bubble sorting
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
void main()
{
int n, arr[20];
srand(time(0));
cout << "Enter size of array :";
cin >> n; //Size integer
cout << "Elements of array: ";
for (int i = 0; i <n; i++)
{
arr[i] = rand() %10; //Genrating random numbers
cout << arr[i] << " ";
}
bubble(arr,n); //Calling the bubble funcation function
cout << endl;
system("pause");
}
Selection sorting
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;
void main()
{
int n, arr[100];
srand(time(0));
cout << "Enter size of array :";
cin >> n; //Size integer
cout << "Elements of array: \n";
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 10; //Genrating random numbers
cout << arr[i] << " ";
}
cout << endl;
selection(arr, n); //Calling the section function
cout << endl;
system("pause");
}
Question no.3
#include <iostream>
#include <iomanip>
using namespace std;
Question no.4
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int** main_arr, row, * col;
cout << "Insert Number of Rows of Array :";
cin >> row;
cout << endl;
main_arr = new int* [row];
col = new int[row];
for (int i = 0; i < row; i++)
{
cout << "Insert size of column " << i + 1<<" :"; //Inserting number of
columns
cin >> col[i];
cout << endl;
main_arr[i] = new int[col[i]]; //Daynamic array
}