Using C++ The main purpose of this lab is to create functions using call-by-valu
ID: 3585610 • Letter: U
Question
Using C++
The main purpose of this lab is to create functions using call-by-value and call- by-reference parameters.
Activity #1
The first activity will be to write a function that sort an array of numbers (passed as a parameter) in ascending order. The input and output for the program are as follows:
Input/Output:
Enter 5 integers: 1 23 4 54 3
The array of integers sorted in ascending order is: 1 3 4 23 54
Include a function swapper(int& a, int& b) that can be used in your sort function. Save the source code in a file named assignment1.cpp.
Activity #2
Once you have completed the first activity, you should modify the program created in Activity #1 to take randomly generated input instead of command-line input. Save the source code in a file namedassignment2.cpp.
Use the “time” command to detect how long it takes to sort 1000 numbers and 10,000 numbers. Include the time result as a comment in your program
Explanation / Answer
assignment1.cpp
#include<iostream>
#include<cstdlib>
using namespace std;
void bubbleSort(int *, int);
void swapper(int &, int &);
int main(int argc, char *argv[])
{
if(argc != 2)
{
cerr<<" Enter array size as second argument of command line ";
return (-1);
}
int n = atoi(argv[1]), *arr = NULL;
arr = (int *)malloc(n * sizeof(int));
if(arr == NULL)
{
cerr<<" Dynamic memory allocation failed ";
return (-2);
}
cout<<" Enter "<<n<<" integers : "<<endl;
for(int i = 0; i < n; i++)
cin>>arr[i];
cout<<" Array before sorting ";
for(int i = 0; i < n; i++)
cout<<arr[i]<<" ";
cout<<endl<<endl;
bubbleSort(arr, n);
cout<<" Array after sorting ";
for(int i = 0; i < n; i++)
cout<<arr[i]<<" ";
cout<<endl<<endl;
free(arr), arr = NULL;
return 0;
}
void bubbleSort(int *arr, int n)
{
int flag = 0;
for(int i = 0; i < n - 1 && !flag; i++)
{
flag = 1;
for(int j = 0; j < n - 1 - i; j++)
{
if(arr[j] > arr[j + 1])
{
flag = 0;
swapper(arr[j], arr[j + 1]);
}
}
}
}
void swapper(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
assignment2.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#define size1 1000
#define size2 10000
using namespace std;
void bubbleSort(int *, int);
void swapper(int &, int &);
int main(int argc, char *argv[])
{
srand(time(NULL));
int *arr = NULL;
arr = (int *)malloc(size1 * sizeof(int));
if(arr == NULL)
{
cerr<<" Dynamic memory allocation failed ";
return (-2);
}
for(int i = 0; i < size1; i++)
arr[i] = rand();
clock_t start = clock();
bubbleSort(arr, size1);
clock_t end = clock();
cout<<" Time taken to sort "<<size1<<" numbers : "<<((double)(end - start) / CLOCKS_PER_SEC)<<"s"<<endl<<endl;//0.015s
free(arr), arr = NULL;
arr = (int *)malloc(size2 * sizeof(int));
if(arr == NULL)
{
cerr<<" Dynamic memory allocation failed ";
return (-3);
}
for(int i = 0; i < size2; i++)
arr[i] = rand();
start = clock();
bubbleSort(arr, size2);
end = clock();
cout<<" TIme take to sort "<<size2<<" numbers : "<<((double)(end - start) / CLOCKS_PER_SEC)<<"s"<<endl<<endl;//1.313s
free(arr), arr = NULL;
return 0;
}
void bubbleSort(int *arr, int n)
{
int flag = 0;
for(int i = 0; i < n - 1 && !flag; i++)
{
flag = 1;
for(int j = 0; j < n - 1 - i; j++)
{
if(arr[j] > arr[j + 1])
{
flag = 0;
swapper(arr[j], arr[j + 1]);
}
}
}
}
void swapper(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.