I have to write a function that sorts an array and remove the duplicated and return the value as ordered but out of 9 tests I failed on only one of them when the order > size or order < 0 it should return -1 instead of an actual number but after removing the duplicates the size remained the same on test 4 and I don't know what's the best way to decrease the size of the array or to at least make it work.
I'm allowed to work on lowestPrice function only.
#include<stdio.h>
int lowestPrice(int array[], int size, int order){
int tempArray[size];
for (size_t i = 0; i < size; i++)
tempArray[i] = array[i];
for (size_t i = 0; i < size; i++)
for (size_t j = i + 1; j < size; j++)
if (tempArray[j] < tempArray[i]) {
int tmp = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = tmp;
}
int j = 0;
for (size_t i = 0; i < size - 1; i++){
if(tempArray[i] != tempArray[i+1])
{
tempArray[j] = tempArray[i];
j++;
} tempArray[j] = tempArray[i+1];
}
if(order > size || order < 0){
return -1;
}
else{
order--;
return tempArray[order];
}
}
void oracle(int no, int array[], int size, int order,int expected, int actual){
printf("Test %d:\n\t\tPrices: {",no);
for (int i = 0; i < size; i++) {
printf(" %d ",array[i]);
}
printf("}\n\t\tOrder: %d",order);
printf("\n\t\tExpected result: %d",expected);
printf("\n\t\tYour function result: %d",actual);
printf("\n\t\tStatus: %s\n",expected==actual?"passed":"failed");
}
int main(){
// oracles
int tests = 0;
int testA1[] = { 25000, 20000, 29499, 10000, 20000, 29000, 25000, 20000 , 25000 , 10000 };
int order1 = 3;
int res1 = 25000;
int resA = lowestPrice(testA1,sizeof(testA1)/sizeof(int),order1);
oracle(1,testA1,sizeof(testA1)/sizeof(int),order1,res1,resA);
if(res1==resA) tests++;
int testA2[] = { 25000, 20000, 29499, 10000, 20000, 29000, 25000, 20000 , 25000 , 10000 };
int order2 = 5;
int res2 = 29499;
resA = lowestPrice(testA2,sizeof(testA2)/sizeof(int),order2);
oracle(2,testA2,sizeof(testA2)/sizeof(int),order2,res2,resA);
if(res2==resA) tests++;
int testA3[] = { 25000, 20000, 29499, 10000, 20000, 29000, 25000, 20000 , 25000 , 10000 };
int order3 = 4;
int res3 = 29000;
resA = lowestPrice(testA3,sizeof(testA3)/sizeof(int),order3);
oracle(3,testA3,sizeof(testA3)/sizeof(int),order3,res3,resA);
if(res3==resA) tests++;
int testA4[] = { 25000, 20000, 29499, 10000, 20000, 29000, 25000, 20000 , 25000 , 10000 };
int order4 = 7;
int res4 = -1;
resA = lowestPrice(testA4,sizeof(testA4)/sizeof(int),order4);
oracle(4,testA4,sizeof(testA4)/sizeof(int),order4,res4,resA);
if(res4==resA) tests++;
printf("\nYour implementation failed %d test(s).\n\n",(4-tests));
return 0;
}