Hi, im trying to create a program that will control a dynamic array .
The program have few functions to handle dynamic array.

* Add number to array
* Erase number from array
* Print the array

I have two problems with my code .
The first one is with the EraseNumber function .
I just cant understand why it doesnt work like it should ,
The second is that every time that the menu pops up , that text
Of the menu start to be really messy for some reason .
Maybe one of you would take a look at the code and tell me what he
Thinks that the problem is .

Btw. im using Turbo C , and im runing it on a win2k OS.

#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <conio.h>
// Functions proto-type
void mainMenu();
void memoryTester(int *array);
int* addNumber(int *array,int arraysize,int var);
int* eraseNumber(int *array,int arraysize,int var);
void printfArray(int *array,int arraysize);
// Main function
int main()
{
  int *array = NULL;
  int arraysize = 0;
  int var = 0;
  int choice;
  // Main loop
  while (choice != 4)
  {
    mainMenu();
    printf ("Enter your choice: ");
    scanf ("%d",&choice);
    switch (choice)
    {
    case 1: printf ("Add number: ");
            scanf ("%d",&var);
            arraysize++;
            array = addNumber(array,arraysize,var);
            break;
    case 2: printf ("Erase number: ");
            scanf ("%d",&var);
   arraysize--;
            array = eraseNumber(array,arraysize,var);
            break;
    case 3: putchar('\n');
            printf ("**** PRINTING ARRAY ****\n\n");
            printfArray (array,arraysize);
            getch();
            break;
    case 4: printf ("Press any key to quit...\n");
            getch();
            break;
    default: printf ("Wrong choice!\n");
             printf ("Press any key to continue...\n");
             getch();
             break;
    }
  }
  free (array);
  return 0;
}
// Main menu function
void mainMenu()
{
  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');
}
// Memory test function
void memoryTest(int *array)
{
  if (array == NULL)
  {
    printf ("Failed to allocate memory!\n");
    printf ("Press any key to quit...\n");
    getch();
    exit(1);
  }
}
// Add number function
int* addNumber(int *array,int arraysize,int var)
{
  int *temp,i;
  if (array == NULL)
  {
    array = (int *) malloc (arraysize * sizeof(int));
    memoryTest(array);
    array[0] = var;
  }
  else
  {
    temp = array;
    array = (int *) malloc (arraysize * sizeof(int));
    memoryTest(array);
    for (i=0;i<arraysize-1;i++)
      array[i] = temp[i];
    array[i] = var;
  }
  free (temp);
  return array;
}
// Erase number function
int* eraseNumber(int *array,int arraysize,int var)
{
  int *temp,i,tmp=0;
  if (arraysize == 0)
  {
    array = NULL;
    return array;
  }
  for (i=0;i<arraysize+1;i++)
  {
    if (array[i] == var)
      tmp = 1;
  }
  if (tmp == 0)
  {
    printf ("Error: No such number!\n");
    printf ("Press any key...\n");
    getch();
    return array;
  }
  temp = array;
  array = (int *) malloc (arraysize * sizeof(int));
  memoryTest(array);
  for (i=0;i<arraysize;i++)
  {
    if (temp[i] == var)
      continue;
    else
      array[i] = temp[i];
  }
  free (temp);
  return array;
}
// Print array function
void printfArray(int *array,int arraysize)
{
  int i;
  for (i=0;i<arraysize;i++)
    printf ("%d,",array[i]);
  putchar('\n');
}
// Functions proto-type
void mainMenu();
void memoryTester(int *array);
int* addNumber(int *array,int arraysize,int var);
int* eraseNumber(int *array,int arraysize,int var);
void printfArray(int *array,int arraysize);
// Main function
int main()
{
    int *array = NULL;
    int arraysize = 0;
    int var = 0;
    int choice;
    // Main loop
    while (choice != 4)
    {
        mainMenu();
        printf ("Enter your choice: ");
        scanf ("%d",&choice);
        switch (choice)
        {
        case 1:
            printf ("Add number: ");
            scanf ("%d",&var);
            arraysize++;
            array = addNumber(array,arraysize,var);
            break;
        case 2:
            printf ("Erase number: ");
            scanf ("%d",&var);
            arraysize--;
            array = eraseNumber(array,arraysize,var);
            break;
        case 3:
            putchar('\n');
            printf ("**** PRINTING ARRAY ****\n\n");
            printfArray (array,arraysize);
            getch();
            break;
        case 4:
            printf ("Press any key to quit...\n");
            getch();
            break;
        default:
            printf ("Wrong choice!\n");
            printf ("Press any key to continue...\n");
            getch();
            break;
        }
    }
    free (array);
    return 0;
}
// Main menu function
void mainMenu()
{
    //  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');
}
// Memory test function
void memoryTest(int *array)
{
    if (array == NULL)
    {
        printf ("Failed to allocate memory!\n");
        printf ("Press any key to quit...\n");
        getch();
        exit(1);
    }
}
// Add number function
int* addNumber(int *array,int arraysize,int var)
{
    int *temp,i;
    if (array == NULL)
    {
        array = (int *) malloc (arraysize * sizeof(int));
        memoryTest(array);
        array[0] = var;
    }
    else
    {
        temp = array;
        array = (int *) malloc (arraysize * sizeof(int));
        memoryTest(array);
        for (i=0;i<arraysize-1;i++)
            array[i] = temp[i];
        array[i] = var;
    }
    free (temp);
    return array;
}
// Erase number function
int* eraseNumber(int *array,int arraysize,int var)
{
    int *temp,i,tmp=0;
    if (arraysize == 0)
    {
        array = NULL;
        return array;
    }
    for (i=0;i<arraysize+1;i++)
    {
        if (array[i] == var)
            tmp = 1;
    }
    if (tmp == 0)
    {
        printf ("Error: No such number!\n");
        printf ("Press any key...\n");
        getch();
        return array;
    }
    temp = array;
    array = (int *) malloc (arraysize * sizeof(int));
    memoryTest(array);
    for (i=0;i<arraysize;i++)
    {
        if (temp[i] == var)
            continue;
        else
            array[i] = temp[i];
    }
    free (temp);
    return array;
}
// Print array function
void printfArray(int *array,int arraysize)
{
    int i;
    for (i=0;i<arraysize;i++)
        printf ("%d,",array[i]);
    putchar('\n');
}

Problems I see:
1. Line 84: Very first insertion happens in slot 0 whereas the arraysize passed would be 1 at that time.
For all other insertions (where arraysize passed is > 0) the insertion happens at index arraysize. => Inconsistant.
2. Line 112: According to Line 84, it's possible to have a valid number at index 0. => Consider initializing tmp to -1 instead of 0.
3. Line 95: This free() will crash on first insertion as tmp isn't initialized then. Should be moved inside the else part above.
4. Line 110: Seems like a typo, should be tmp = i.
5. Line 124: Although it is assumed by caller (main()) that one call to eraseNumber() will erase ONE entry (because main() does arraysize-- on line 31), because of condition on line 124, if the array contained multiple entries for same number then all of them will be removed.
So say if before a call to eraseNumber() array size of 20 and number X was repeated 5 times in array. After a call to erase
- the variable arraysize will be 19
- array will contain 15 valid numbers (all except those five X)
- last 4 elements in the array would be junk (uninitialized).

A few suggesions:
>> The second is that every time that the menu pops up , that text Of the menu start to be really messy for some reason .
Use fflush(0) before every call to scanf(). See documentation of fflush() to see why.

1. Do not change the value of arraysize inside main(), pass it by pointer to add/eraseNumber functions and let it be changed there.
2. Consider using memcpy() instead of for-loops to copy from temp-array to real array and vise-versa.
3. Take a decision abt how you wanna handle repetaion of same number. Change add/errase appropriately. If you don't want repetation you can give error from addNumber().

This article has been dead for over six months. Start a new discussion instead.