Hi , im trying to create a program that could delete number from an array and resize the array to the correct size .
Array[0] = 32
Array[1] = 30
Array[2] = 40
Array[3] = 31
Array[4] = 61
Deleting the number 40
New array
Array[0] = 32
Array[1] = 30
Array[2] = 31
Array[3] = 61
This is the code that i came up with but i have no idea why it wont work ...
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
int main()
{
int *array,i,size = 5,var,temp=0,*array2;
array = (int *) malloc (size * sizeof(int));
for (i=0;i<5;i++)
{
printf ("Enter number: ");
scanf ("%d",&array[i]);
}
printf ("Enter a number to delete: ");
scanf ("%d",&var);
for (i=0;i<size;i++)
{
if (array[i] == var)
{
array[i] = 0;
temp = 1;
}
}
if ( temp == 0)
{
printf ("No such number in array!\n");
printf ("Press any key to terminate the program...\n");
getch();
free (array);
exit (1);
}
array2 = array;
free (array);
size--;
array = (int *) malloc (size * sizeof(int));
for (i=0;i<size;i++)
{
if (array2[i] != 0)
array[i] = array2[i];
}
putchar ('\n');
printf ("PRINTING ARRAY: ");
for (i=0;i<size;i++)
printf ("%d,",array[i]);
putchar('\n');
getch();
free (array);
free (array2);
return 0;
}