-3

For some reason when I reallocate the size of the array created using calloc it deletes the values that have already been inputted, maybe something else is happening but i don't understand why. I have changed the code so that it includes everything it needs to work, sorry i forgot about that

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

int main(void) {
unsigned int arraySize; // size of array
int moreElements; // stores user input whether more elements are to be 
int additionalElements = 0; // number of elements to be added to array
unsigned int type; // stores the type of array
float sum = 0; // the sum of the elements
float *floatArrPtr; // pointer to a flaot

floatArrPtr = calloc(arraySize, sizeof(float));

for(size_t i = 0; i < arraySize; i++)
{
  printf("Please enter a number for the array: ");
  scanf("%f", &floatArrPtr[i]);
  printf("%f\n", floatArrPtr[i]);
}

for(size_t i = 0; i < arraySize; i++)
{
  sum += *(floatArrPtr+i);
}
sum /= arraySize;

printf("The average of the elements of the array is %lf\n", sum);

do
{
  printf("if there are more elements to be added enter 1 and 0 if not:         ");
  scanf("%d", &moreElements);
} while (moreElements != 0 && moreElements != 1);

if (moreElements == 1) {
  printf("please enter the number of additional elements you want to add: ");
  scanf("%d", &additionalElements);

  floatArrPtr = realloc(intArrPtr,(arraySize+additionalElements) * sizeof(float));
  for (size_t i = arraySize; i < arraySize+additionalElements; i++)
  {
    printf("Please enter a number for the array: ");
  scanf("%f", &floatArrPtr[i]);
  }

  sum = 0;
  for(size_t i = 0; i < arraySize+additionalElements; i++)
  {
    sum += *(floatArrPtr+i);
    printf("%zu, %lf, %d\n", i, floatArrPtr[i], arraySize + additionalElements);
  }
  sum /= (arraySize + additionalElements);

  printf("The average of the elements of the array is %lf\n", sum);
}
}
james
  • 101
  • 1
  • 8
  • 3
    What is 'intArrPtr'? – ThingyWotsit Apr 04 '17 at 20:45
  • 3
    Can't debug incomplete code (e.g. what is `intArrPtr`?). Please provide a [mcve]. – kaylum Apr 04 '17 at 20:45
  • What type is `floatArrPtr`? Is it `float*` or `double*`? I suspect it's `double*`, which means you're not allocating enough memory. – Mark Ransom Apr 04 '17 at 20:46
  • Yeah... data and its types are more important than code. Then there's that callocing of 'floatArrPtr' and reallocing of 'intArrPtr'.... – ThingyWotsit Apr 04 '17 at 20:48
  • That calloc code at the top is wrong. For an arraySize of 1000 it allocates a million floats, or 4MB. Then I assume the real problem is the intArrayPtr that slipped in from earlier code. Use functions, they pay off. – Tom Andersen Apr 04 '17 at 20:51
  • The updated code is still not an MCVE. It does not compile and does not contain crucial code such as the definition of `intArrPtr`. – kaylum Apr 04 '17 at 20:54
  • Im changing the calloc now, thanks! I've updated the code hopefully that answers your questions. intArrPtr is for another part of the program, it does the same thing but for integers – james Apr 04 '17 at 20:54
  • You don't initialize your `arraySize` value, thats going to give you a lot errors – Aroic Apr 04 '17 at 20:55
  • i have initialised it but I'm trying to remove things from it because when i initialise it i get input from the user for other parts as well. Im currently changing the code now, it shouldn't take long, i have to indent the code on here because it doesn't all go as code – james Apr 04 '17 at 21:02

1 Answers1

2

That calloc code at the top is wrong. For an arraySize of 1000 it allocates a million floats, or 4MB. Look that up.

Then I assume the real problem is the intArrayPtr that slipped in from earlier code.

Use functions, they pay off. – I mean make all your code no more than 4 lines or so long per function, this will stop old variables from earlier from slipping in.

The wrong line is

 floatArrPtr = realloc(intArrPtr...

You need

floatArrPtr = realloc(floatArrPtr...

I don't know the purpose of intArrPtr, but it looks like if that code compiles that its coming in from code above.

You have global variables. One must be very careful with them as they are a pain at best, at worst they cause unforeseen edge case bugs, which is what you have.

Make your project two files, one for integer and one for float, and you will see your mistake in the compiler.

Tom Andersen
  • 7,132
  • 3
  • 38
  • 55
  • I do use functions, its just in this case i have to ask the user whether they want the array to be of integers or floats so i decided not to because of that, thank you for spotting out the calloc error but i don't think thats why its not working as it works for the integer version – james Apr 04 '17 at 21:06
  • I have changed it there and i still have the same issue – james Apr 04 '17 at 21:08