Home > Software design >  Why array is not printing even after calling viewArray function?
Why array is not printing even after calling viewArray function?

Time:06-06

As you can see in the code I am trying to print the items for the array by calling the viewArray function but it is not printing after the call. I tried to double the capacity of the array by using the doubleArray function and I also tried to halve the size of the Array using halfArray function.

#include<stdio.h>
#include<stdlib.h>

struct DynamicArray
{
    int capacity;
    int lastIndex;
    int *ptr;
};

struct DynamicArray *createArray(int cap)
{
    struct DynamicArray *arr;
    arr=(struct DynamicArray*)malloc(sizeof(struct DynamicArray));
    arr->ptr=(int*)malloc(sizeof(int)*cap);
    arr->capacity=cap;
    arr->lastIndex=-1;
    return arr;
};
// Function to double the capacity of array
void doubleArray(struct DynamicArray *arr)
{
    int *temp;
    temp=(int*)malloc(sizeof(int)*arr->capacity*2);
    for(int i=0; i<=arr->lastIndex; i  )
        temp[i]=arr->ptr[i];
    free(arr->ptr);
    arr->ptr=temp;
    arr->capacity*=2;
}
// Function to half the size of an Array
void halfArray(struct DynamicArray *arr)
{
    int *temp;
    temp=(int*)malloc(sizeof(int)*arr->capacity/2);
    for(int i=0; i<=arr->lastIndex; i  )
        temp[i]=arr->ptr[i];
    free(arr->ptr);
    arr->ptr=temp;
    arr->capacity/=2;
}
//Function to append an element in Array
void append(struct DynamicArray *arr,int data)
{
    if(arr->lastIndex 1>arr->capacity)
    {
        doubleArray(arr);
    }
    arr->ptr[arr->lastIndex 1]=data;
    arr->lastIndex =1;
}
// Function to insert an element in array
void insert(struct DynamicArray *arr,int pos,int data)
{
    if(pos<0 || pos>arr->lastIndex 1)
    {
        printf("\nInvalid Index!!\n");
    }
    else
    {
        if(arr->lastIndex 1>arr->capacity)
        {
            doubleArray(arr);
        }
        for(int i=arr->lastIndex; i>=pos; i--)
            {
                arr->ptr[i 1]=arr->ptr[i];
            }
            arr->ptr[pos]=data;
            arr->lastIndex =1;
}

// Function to delete an element from an array
void delete(struct DynamicArray *arr,int pos)
{
    if(pos<0 || pos>arr->lastIndex 1)
    {
        printf("\nInvalid Index\n");
    }
    else
    {
        if(count(arr)<=arr->capacity/2)
        {
            halfArray(arr);
        }
             for(int i=pos; i<arr->lastIndex; i  )
            {
                arr->ptr[i]=arr->ptr[i 1];
            }
            arr->lastIndex-=1;
}

// Function to print the Array elements
void viewArray(struct DynamicArray *arr)
{
    for(int i=0; i<=arr->lastIndex; i  )
    {
        printf("\n%d\t",arr->ptr[i]);
    }
}

int main()
{
    int choice;
    struct DynamicArray *arr;

    while(1)
    {
        printf("1:-  Create an Array\n");
        printf("2:-  Append an Element in an Array\n");
        printf("3:-  Insert an Element in an Array\n");
        printf("4:-  Delete an Element in an Array\n");
        printf("5:-  View Array\n");

        printf("\nEnter your Choice:-\t");
        scanf("%d",&choice);

        switch(choice)
        {
        case 1:
            arr=createArray(10);
            printf("\nArray Created successfully!!\n\n");
            break;

        case 2:
            append(arr,99);
            append(arr,45);
            append(arr,67);
            append(arr,21);
            append(arr,32);
            printf("\nElement append successfully!!\n\n");
            break;

        case 3:
            insert(arr,2,89);
            break;

        
        case 4:
            delete(arr,2);
            printf("\nElement deleted successfully!!\n\n");
            break;

        case 5:
            viewArray(arr);
            break;
        }
    }

    return 0;

}

CodePudding user response:

Let's start with problems in the structure. You should not use int to index the array. You should use unsigned type for that. What's more I think that the lastIndex is a bad idea - use the size of the array, it is always equal to lastIndex 1, so you will avoid a lot of adding later in the code.

The first function: createArray. You do not check whether malloc returned NULL. What's more your way of calling it is not the best. It's not wrong, but I highly recommend reading this post.

Now, the second function: 'doubleArray'. Have you heard of 'realloc'? You should use it here. The same problem in the the 'halfArray' function.

About append: I am pretty sure that the condition in the if clause is incorrect. It should be >= not >. It would be more obvious if you used size instead of lastIndex.

Insert: You should print errors to stderr with fprintf(stderr, error_message); What's more, the same error in if clause. Delete: The same as insert and I think you should halve the array AFTER you move everything.

After considering everything, the code below should work:

#include<stdio.h>
#include<stdlib.h>

struct DynamicArray
{
    size_t capacity;
    size_t size;
    int *ptr;
};

struct DynamicArray *createArray(size_t cap)
{
    struct DynamicArray *arr = malloc(sizeof *arr);
    if (arr) {
        arr->ptr = calloc(cap, sizeof(*arr->ptr));
        if (arr->ptr) {
            arr->capacity = cap;
            arr->size = 0;
        } else {
            free(arr);
        }
    }
    return arr;
};
// Function to double the capacity of array
void doubleArray(struct DynamicArray *arr)
{
    int *temp = realloc(arr->ptr, sizeof(*arr->ptr) * (arr->capacity * 2));
    if (temp) {
        arr->ptr = temp;
        arr->capacity *= 2;
    } else {
        // You should make it possible to return failure.
    }
}
// Function to half the size of an Array
void halfArray(struct DynamicArray *arr)
{
    int *temp = realloc(arr->ptr, sizeof(*arr->ptr) * (arr->capacity / 2));
    if (temp) {
        arr->ptr = temp;
        arr->capacity /= 2;
    } else {
        // You should make it possible to return failure.
    }
}
//Function to append an element in Array
void append(struct DynamicArray *arr,int data)
{
    if(arr->size >= arr->capacity)
    {
        doubleArray(arr);
    }
    arr->ptr[arr->size] = data;
      arr->size;
}
// Function to insert an element in array
void insert(struct DynamicArray *arr, size_t pos, int data)
{
    if (pos > arr->size)
    {
        // Errors should be reported to stderr
        fprintf(stderr, "\nInvalid Index!!\n");
    }
    else
    {
        if (arr->size >= arr->capacity)
        {
            doubleArray(arr);
        }
        for (size_t i = arr->size; i > pos; --i)
        {
            arr->ptr[i] = arr->ptr[i - 1];
        }
        arr->ptr[pos]=data;
                  arr->size;
        }
}

// Function to delete an element from an array
void delete(struct DynamicArray *arr, size_t pos)
{
    if(pos > arr->size)
    {
        fprintf(stderr, "\nInvalid Index\n");
    }
    else
    {
        // I do not know what "count" is.
        // And I think you should halve an array after moving data.
        /*if(count(arr)<=arr->capacity/2)
        {
            halfArray(arr);
        }*/
        arr->size -= 1;
        for (size_t i = pos; i < arr->size;   i)
        {
            arr->ptr[i] = arr->ptr[i 1];
        }
        }
}

// Function to print the Array elements
void viewArray(struct DynamicArray *arr)
{
    for(size_t i = 0; i < arr->size; i  )
    {
        printf("\n%d\t",arr->ptr[i]);
    }
}

int main()
{
    int choice;
    struct DynamicArray *arr;

    while(1)
    {
        printf("1:-  Create an Array\n");
        printf("2:-  Append an Element in an Array\n");
        printf("3:-  Insert an Element in an Array\n");
        printf("4:-  Delete an Element in an Array\n");
        printf("5:-  View Array\n");

        printf("\nEnter your Choice:-\t");
        scanf("%d",&choice);

        switch(choice)
        {
        case 1:
            arr=createArray(10);
            printf("\nArray Created successfully!!\n\n");
            break;

        case 2:
            append(arr,99);
            append(arr,45);
            append(arr,67);
            append(arr,21);
            append(arr,32);
            printf("\nElement append successfully!!\n\n");
            break;

        case 3:
            insert(arr,2,89);
            break;

        
        case 4:
            delete(arr,2);
            printf("\nElement deleted successfully!!\n\n");
            break;

        case 5:
            viewArray(arr);
            break;
        }
    }

    return 0;

}
  • Related