Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Knowledge Discovery In Databases

Authors: Animesh Adhikari, Jhimli Adhikari

1st Edition

3319132121, 9783319132129

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago