kan3
kan3

Reputation: 33

how to resize an array?

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;
}

Upvotes: 0

Views: 329

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126203

You can't. Arrays have a fixed size in C which is set when the array is created and will not change. The best you can do is

  1. Have an array with a fixed size that is "more than enough" and keep track of how much of the array is actually in use with another variable. When you do this you often have size and capacity variables (or fields) -- size tracks how much of the array is in use and capacity tracks its total size.
  2. Create a new array with a larger size and copy the elements from the old array. If your array is on the heap (allocated with malloc/calloc), you may be able to do this in one step with realloc. For static or stack (auto) arrays, you need to create new actual array.

You might combine these two -- have an initial array capacity (perhaps allocated with malloc) and when the size gets up to capacity, create a new array (perhaps with realloc) with a larger capacity and copy the data.


In your code it looks like you are getting an array as a parameter, along with a size parameter which is both the size and capacity of the array. So you can freely decrease the size variable and have the effect of decreasing the size of the array, without bothering to track the capacity. As long as you only decrease size (never increase) it will never exceed the capacity.

Upvotes: 1

Related Questions