I need this code that would be able to do this and a screen shot of the code run
ID: 3597496 • Letter: I
Question
I need this code that would be able to do this and a screen shot of the code running.
10. Consider an array of length n containing positive and negative integers in random order. Write Ct+ code that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use a. O(n2) operations b. O(n) operations OGRAMMING PROBLEMS For the following programming problems, you need to time a section of code in C+t. For example, the following statements time the execution of the function doSomething: #include clock-t start = clock(); doSomethingO; clock t finish clockO double overal ITime = static-cast (finish -start) / CLOCKS-PER-SEC;Explanation / Answer
#include <iostream>
#include <ctime>
using namespace std;
void Arrange_n2(int a[],int n){
//int n=10;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
for(int k=0;k<100;k++){
}
if(a[j+1]<a[j]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<" N^2 Operations ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
void Arrange_n(int a[],int n){
int np=0;
int pp=n-1;
int b[n];
for(int i=0;i<n;i++){
if(a[i]<0){
b[np++]=a[i];
}
else{
b[pp--]=a[i];
}
}
cout<<" N Operations ";
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
int a[10]={1,2,3,4,5,-6,-7,-8,-9,-10};
int b[10]={1,2,3,4,5,-6,-7,-8,-9,-10};
clock_t start1=clock();
Arrange_n2(a,10);
clock_t finish1=clock();
double overallTime1 = static_cast<double>(finish1-start1);
cout<<" Time for N^2 Operatios: "<<overallTime1;
clock_t start2=clock();
Arrange_n(b,10);
clock_t finish2=clock();
double overallTime2 = static_cast<double>(finish2-start2);
cout<<" Time for N Operatios: "<<overallTime2;
return 0;
}
Output
****************************************************
N^2 Operations
-10 -9 -8 -7 -6 1 2 3 4 5
Time for N^2 Operatios: 0
N Operations
-6 -7 -8 -9 -10 5 4 3 2 1
Time for N Operatios: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.