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;
}

Recommended Answers

All 8 Replies

You're doing a shallow copy of array to array2. When you free the memory for array you also loose the memory you were pointing to in array2. Do a deep copy of array to array2 by assigning independent memory to array2 using malloc() like you did for array, and then use a loop to copy array to array2 element by element. You'll also need different variables to keep track of the indexes in array and array2 when you copy array2 back to array without the element you removed.

I dont get it , what do you mean by "diffrent variables to keep track of the indexes" ?
can you give me an example ?
thanks.

??

Bumping your posts would do you no good. It would only result in a longer delay and is not encouraged. Patience wins the best help.

I dont get it , what do you mean by "diffrent variables to keep track of the indexes" ?can you give me an example ?
thanks.

You just have to make sure that the element you have to remove doesn't make it into the new array.

This depends on the way you want the removal to happen. For eg. you can do position based removal or value based removal.

And btw, are you sure you have to create a new array? Normally these kinds of exercises require you to make changes in the original array itself using realloc .

well i thought i should create a new array cause i have no idea how i can remove a number from the array using realloc ...

well i thought i should create a new array cause i have no idea how i can remove a number from the array using realloc ...

locate the element that contains the value you want removed. Then move all lower elemenets up one place to overwrite that elemement. That will leave the last element of the array unused. Then call realloc() to allocate the array 1 element smaller then it was before.

Finally ive solved it , thanks for the realloc . its just that if i resize the array to array[n-1] the last number is beeing deleted , so what i did is pushing the number that the user wishes to erase to the end of the array and resize if to array [n-1]. ive also used realloc for the add function , it makes the function really smaller and understandable , . so thanks !!!.
anyway here is the code and its FINALLY working.

#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>
void memoryTest(int *array)
{
 if (array == NULL)
 {
  printf ("Error: Failed to allocate memory!\n");
  printf ("Press any key to terminate the program...\n");
  getch();
  exit(1);
 }
}
int* AddNumber (int *array,int size,int var)
{
 array = (int *) realloc (array,sizeof(int) * size);
 memoryTest(array);
 array[size-1] = var;
 return array;
}
int* EraseNumber (int *array,int size,int var)
{
 int i,tmp=0;
 for (i=0;i<size+1;i++)
 {
  if (array[i] == var)
   tmp = 1;
 }
 if (tmp == 0 || array == NULL)
 {
  printf ("Error: No such number in array!\n");
  printf ("Press any key to return into the main menu...\n");
  getch();
  return array;
 }
 tmp = 0;
 for (i=0;i<size;i++)
 {
  if (array[i] == var)
  {
   tmp = array[i+1];
   array[i+1] = array[i];
   array[i] = tmp;
  }
 }
 array = (int *) realloc (array,sizeof(int) * size);
 memoryTest(array);
 return array;
}
void PrintArray(int *array,int size)
{
 int i;
 putchar ('\n');
 printf ("***** PRINTING ARRAY *****\n");
 putchar ('\n');
 for (i=0;i<size;i++)
  printf ("%d,",array[i]);
 putchar('\n');
        getch();
}
int main()
{
 int *array;
 int size=0;
 int var;
 int choice;
 while (choice != 4)
 {
  clrscr();
  printf ("***** MAIN MENU *****\n\n");
  printf ("1. Add number.\n");
  printf ("2. Erase number.\n");
  printf ("3. Print array.\n");
  printf ("4. Quit.\n");
  putchar ('\n');
  printf ("Enter your choice: ");
  scanf ("%d",&choice);
  switch(choice)
  {
  case 1: printf ("Add number: ");
       scanf ("%d",&var);
    size++;
    array = AddNumber (array,size,var);
    break;
  case 2: printf ("Erase number: ");
       scanf ("%d",&var);
    size--;
    array = EraseNumber (array,size,var);
    break;
  case 3: PrintArray(array,size);
       break;
  case 4: printf ("Press any key to quit...\n");
       getch();
    break;
  default: printf ("Error: Wrong choice !\n");
        printf ("Press any key to return into the main menu...\n");
     getch();
     break;
  }
 }
 free (array);
 return 0;
}

ive just saw your post Ancient Dragon , but i already did it . so thanks anyway ...
Big thanks for all of you guys !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.