i would like to highlight is that there may be similar elements in an array and
ID: 3552241 • Letter: I
Question
i would like to highlight is that there may be similar elements in an array and therefore more than 1 indexs could be returned for 1st,2nd,3rd or nth smallest value of an array.
Explanation / Answer
#include <stdio.h>
#include <iostream>
using namespace std;
int *mark; // used whehter the elemnts is already found or not
void findMin(double A[], int n){
int i;
int minIndex=0;
for(i=0; i<n; i++){
if(A[i]<A[minIndex]){
minIndex=i;
}
}
cout<<A[minIndex]<<" "<<minIndex<<endl;
mark[minIndex]=1;
}
int findNextMin(double A[], int n){
int i, nextMinI=0;
for(i=0; i<n;i++){
if(mark[i] ==0){
nextMinI=i;
break;
}
}
for(i=0; i<n;i++){
if(A[i]< A[nextMinI] && mark[i] ==0)
nextMinI = i;
}
if(mark[nextMinI] == 0){
cout<<A[nextMinI]<<" "<<nextMinI<<endl;
mark[nextMinI] = 1;
return 1;
}
else
return 0;
}
void simplesort(double A[], int n){
mark = new int[n];
int i;
for(i=0; i<n; i++){
mark[i] =0;
}
findMin(A, n);
while(findNextMin(A, n));
}
int main(void) {
// your code goes here
int n;
cin>>n;
int i;
double A[n];
for(i=0; i<n; i++)
cin>>A[i];
simplesort(A, n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.