********I have already completed activity #1, you can find the code below, can y
ID: 3585676 • Letter: #
Question
********I have already completed activity #1, you can find the code below, can you help me with activity #2?********
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:
Activity 1 code:
#include<iostream>
using namespace std;
// function that swaps 2 numbesr
void swapper(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
// declaring variables
int fiveInts[5], i, j;
int n = 5;
// taking user input
cout<<"Enter 5 integers: ";
for(i=0; i<n; i++)
{
cin>>fiveInts[i];
}
// Bubble sort technique to sort elements
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(fiveInts[j]>fiveInts[j+1])
{
swapper(fiveInts[j],fiveInts[j+1]);
}
}
}
// printing output
cout<<"The array of integers sorted in ascending order is: " ;
for(i=0; i<n; i++)
{
cout<<fiveInts[i]<<" ";
}
}
Using C++
The main purpose of this lab is to create functions using call-by-value and call- by-reference parameters.
Activity #2 *****I need help with this part ******
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 named assignment2.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
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
// function that swaps 2 numbesr
void swapper(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
// declaring variables
int i, j;
cout<<"Enter the number of elements ";
int n;
int fiveInts[n + 1];
cin>>n;
for(i=0; i<n; i++)
fiveInts[i] = rand() % n + 1;
// store time in milliseconds
long double start = time(0) * 1000;
// Bubble sort technique to sort elements
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(fiveInts[j]>fiveInts[j+1])
{
swapper(fiveInts[j],fiveInts[j+1]);
}
}
}
// store time in milliseconds
long double end = time(0) * 1000;
// printing output
/*cout<<"The array of integers sorted in ascending order is: " ;
for(i=0; i<n; i++)
{
cout<<fiveInts[i]<<" ";
}*/
cout<<" Time taken : "<<(end - start);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.