Question: I am trying to make a program that takes numbers from the user and stores them in an array. And program stops when the user

I am trying to make a program that takes numbers from the user and stores them in an array.

And program stops when the user enters a negative number and prints the array.

Can anyone tell me what is wrong with this code?

This is done in C language.

#include  #include  #define INITIAL_SIZE 5 int main() { int length = 0, currentNumber = 0; int size = INITIAL_SIZE; int *array = (int *)malloc(size * sizeof(int)); scanf("%d", ¤tNumber); while(currentNumber>0){ length++; array = resizeArrayIfNeeded(array, length, &size); array[length - 1] = currentNumber; scanf("%d", ¤tNumber); } for (int i = 0; i < length; i++){ printf("A[%d]=%d ", i, array[i]); } free(array); int *resizeArrayIfNeeded(int *array, int *usedLength, int *arraySize) { if(usedLength <= *arraySize){ return array; } else{ printf("needed to resize! "); *arraySize *= 2; return (int *)realloc(array, *arraySize * sizeof(int)); } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!