Deleting an element in an array!

peps
peps used Ask the Experts™
on
Hi....

I lets say u have an array

char Name[100][15+1];


The user enters a name and it is stored in the array... how would you delete an element in the array? say you delete the 1st element

0 Hi
1 Hello
2 Bye

after deleted...
0 Hi
1 Bye

Thanks alot... in advanced bye!!!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This sounds like a homework problem...

To delete the nth element just copy the elements in the n+i positions to n+i-1 spots.

So arr[n] = arr[n+1], arr[n+1] = arr[n+2], ...

Actually coding left to student :-)

Author

Commented:
well actaully here is the code...

I just wanted to see the concept on deleteing...

/* Include definitions*/
#include <stdio.h>
# include <string.h>
/* Global definitions*/
#define SIZE 100
/* Globall variable definitions*/
char Fname[SIZE][15+1];
char Lname[SIZE][15+1];
char Address[SIZE][40+1];
char Phone[SIZE][10 + 1];
int count = 0;
/* function prototypes  void */
void add_record();
void delete_record();
void edit_record();
void search_record();
void print_dir();
int menu();

/*This function is the main program function and calls all the other functions*/
main() {
      int x;
      do {
            x = menu();
            /*assings returned value opt to x*/
            switch (x) {
                  /* function calls inside each case*/
                  case 1 :
                        add_record();
                        break;
                  case 2 :
                        delete_record();
                        break;
                  case 3 :
                        edit_record();
                        break;
                  case 4 :
                        search_record();
                        break;
                  case 5 :
                        print_dir();
                        break;
                  case 6 :
                        exit(0);
            }
            /* closes switch */
      } while (x != 6);
      /* closes do while (will only exit if 6 is typed, otherwise menu will be displayed again*/
}
/* closes main*/

/*This function will is the main menu*/
int menu() {
      int opt;
      system("clear");
      printf("\nMenu\n");
      printf("1. Add Record\n");
      printf("2. Delete Record\n");
      printf("3. Edit Record\n");
      printf("4. Search Record\n");
      printf("5. Print Directory\n");
      printf("6. Exit\n");
      printf("Please Enter a valid Option\n");
      scanf("%d", & opt);
      return opt;
      /*returns value at opt to main*/
}

/*This function will add a record when ever it is asked from the main menu*/
void add_record() {
      char c;
      do {
            printf("Enter your information \n"); //Asks for the information
            gets(Fname[count]); //null
            printf("First Name: "); //Asks for your first name
            gets(Fname[count]); //stores the entered First name in Fname at the count
            printf("\nLast Name: "); //Asks for your last name
            gets(Lname[count]); //stores the entered Last name in Lname at the count
            printf("\nAddress: "); //Asks for your address
            gets(Address[count]); //stores the entered Address in Address at the count
            printf("\nPhone Number: "); //Asks for your phone number
            gets(Phone[count]); //stores the entered Phone number in Phone at the count
            count++; // increments the counter      
            printf("Continue to add record (y/n) ");
            c = getchar();
      } while (c != 'n');
}

/*This function will delete a record when ever it is asked from the main menu*/
void delete_record() {
      int flag = 0, l = 0;
      char c;
      char ptemp[10 + 1];
      printf("Please Enter an EXISTING Phone number: ");
      gets(ptemp);
      gets(ptemp);
      for (; flag < count; flag++) {
            if (strcmp(ptemp, Phone[flag]) == 0) {
                  l++;
                  printf("The record was found and deleted");
            }
      }
      if (l == 0) {
            printf("The record was Not found, Not deleting record");
      }
      printf("\nPlease press Enter to continue");
      c = getchar();
}

/*This function will edit a record when ever it is asked from the main menu*/
void edit_record() {
      int flag = 0, l = 0; //Delcares flag and l to 0
      char c, eopt; //delcaring c and eopt as chars
      char ptemp[10 + 1]; //delcaring ptemp as a char with 11 elements
      printf("Please Enter an EXISTING Phone number: "); //asking for an existing phone nubmer
      gets(ptemp); //stores the number in the temp
      gets(ptemp); //null
      for (; flag < count; flag++)
            //loop goes untill it searches all the records which is COUNT
            {
            if (strcmp(ptemp, Phone[flag]) == 0)
                  //Compares ptemp, with Phone at the flag element
                  {
                  printf("Last Name: %s\nFirst Name: %s\nAddress: %s\nPhone Number: %s\n", Lname[flag], Fname[flag], Address[flag], Phone[flag]); //printing a found record
                  l++; //incrementing the l count
                  printf("Do you wish to edit this Record? y/n: "); //asks if you want to edit that record
                  eopt = getchar(); //gets the answer
                  //eopt=getchar ();//null      
                  if (eopt == 'y')
                        //if option equals yes
                        {
                        printf("\nEnter A NEW First name: "); //asks for a new first name
                        gets(Fname[flag]); //Stores the new one in the old position
                        gets(Fname[flag]); //null
                        printf("\nEnter A NEW Last name: "); //asks for a new last name
                        gets(Lname[flag]); //Stores the new one in the old position
                        printf("\nEnter A NEW Address: "); //asks for a new address
                        gets(Address[flag]); //Stores the new one in the old position
                        printf("\nEnter A NEW Phone Number: "); //asks for a new phone number
                        gets(Phone[flag]); //Stores the new one in the old position
                  }
            }
      }
      if (l == 0)
            //if the l count equals 0
            {
            printf("%s is not found\n", ptemp); //It prints the name was not found
      }
      printf("Please press Enter to continue"); //Asks to continue
      c = getchar(); //gets the key hit ENTER
}

/*This function will search for a record bye first,last Name, or by address when ever it is asked from the main menu*/
void search_record() {
      int sopt, flag = 0, l = 0; //Declaring sopt l and flag to 0
      char c; //Declaring c as a char
      char lntemp[15+1], fntemp[15+1], atemp[40+1]; //Delcaring lntemp, fntemp, and atemp  as char array with 15 and 40 elements
      printf("1. Search using Last Name\n"); //Asks what they want to search bye
      printf("2. Search using First Name\n"); //Asks what they want to search bye
      printf("3. Search using Address\n"); //Asks what they want to search bye
      printf("4. Back TO MAIN MENU\n"); //Asks to go to main menu
      printf("Please enter a valid option:\n "); //asks for the option
      scanf("%d", & sopt); //gets the option
      if (sopt == 1)
            //if option equals 1
            {
            printf("Enter Last Name: "); //asks for the last name
            gets(lntemp); //stores it in lntemp
            gets(lntemp); //null
            for (; flag < count; flag++)
                  //Checks with all the record in the COUNT
                  {
                  if (strcmp(lntemp, Lname[flag]) == 0)
                        //Compares lntemp with Lname and element flag and if it equals 0
                        {
                        printf("%s\t%s\t%s\t%s\n", Lname[flag], Fname[flag], Address[flag], Phone[flag]); //prints the record
                        l++; //increments the l counter
                  }
            }
            if (l == 0)
                  //if l equals 0
                  {
                  printf("%s is not found\n", lntemp); //The record was not found
            }
            printf("Please press Enter to continue"); //Asks to continue
            c = getchar(); //gets the key hit ENTER
      }
      if (sopt == 2)
            //if option equals 2
            {
            printf("Enter First Name: "); //asks for the first name
            gets(fntemp); //stores it in fntemp
            gets(fntemp); //null
            for (; flag < count; flag++)
                  //Checks with all the record in the COUNT
                  {
                  if (strcmp(fntemp, Fname[flag]) == 0)
                        //Compares fntemp with Fname and element flag and if it equals 0
                        {
                        printf("%s\t%s\t%s\t%s\n", Lname[flag], Fname[flag], Address[flag], Phone[flag]); //prints the record
                        l++; //increments the l counter
                  }
            }
            if (l == 0)
                  //if l equals 0
                  {
                  printf("%s is not found\n", fntemp); //The record was not found
            }
            printf("Please press Enter to continue"); //Asks to continue
            c = getchar(); //gets the key hit ENTER
      }
      if (sopt == 3)
            //if option equals 3
            {
            printf("Enter Address: "); //asks for the address
            gets(atemp); //stores it in atemp
            gets(atemp); //null
            for (; flag < count; flag++)
                  //Checks with all the record in the COUNT
                  {
                  if (strcmp(atemp, Address[flag]) == 0)
                        //Compares atemp with Address and element flag and if it equals 0
                        {
                        printf("%s\t%s\t%s\t%s\n", Lname[flag], Fname[flag], Address[flag], Phone[flag]); //prints the record
                        l++; //increments the l counter
                  }
            }
            if (l == 0)
                  //if l equals 0
                  {
                  printf("%s is not found\n", atemp); //The record was not found
            }
            printf("Please press Enter to continue"); //Asks to continue
            c = getchar(); //gets the key hit ENTER
      }
      if (sopt == 4) {
            printf("Please press Enter to continue\n"); //Asks to continue
            c = getchar(); //gets the key hit ENTER
      }
}

/*This function will print all the records when ever it is asked from the main menu*/
void print_dir() {
      int i = 0; //Declaring i as an int equlas to 0
      char c; //Declaring c as a char
      printf("LAST NAME          FIRST NAME          ADDRESS         PHONE NUMBER\n"); //Prints the Title Bar
      for (i = 0; i < count; i++)
            //prints all the records at the count
            {
            printf("%s          %s         %s              %s                 \n", Lname[i], Fname[i], Address[i], Phone[i]); //prints the record at i
            printf("\n"); //Skips a line
      }
      printf("Please press Enter to continue"); //Asks to continue
      c = getchar(); //gets the key hit ENTER
      c = getchar(); //null
}

Author

Commented:
and how to format a name...
like in java you put name,12

moves 12 spacees!!!

lates!!!1
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
there are two ways
first:
starting from the index u want to delete start shifting the elements right to left like this

int arr[100];
int size;
// number of elements in ur array ( = last index +1)

int i,idx;

idx = index to be deleted,

for(i=idx;i<size-2;i++){
arr[idx]=arr[idx+1];
}
this above is time consuming BUT preserves the order

SECOND:
arr[idx]=arr[size-1];
size--;
put the last element and  decrease the size of the array by one
this will delete the element in one step .. but does not preserve the original order of the elemtns in the array

Commented:
If you aren't constrained to using arrays, you might consider using a linked list instead. This type of operation is both quick and easy with linked lists. (Check any of the many intro programming techniques books available for implementation details.)

Commented:
One possible way to do this is to change the value in Phone[] to null string.

But, the Better method is to use char pointer array for Phone.
Your declaration would be as below:

char *Phone[SIZE];

In the add_record() function, before getx(Phone[count]), allocate memory to it as below:

Phone[count] = (char *) malloc(25);

Now for deletion, you can just free up the pointer at that place and make it NULL.
A NULL would indicate that there is no data at that place.

The other possible way to do this is to use linked list, which will make the process a little more complicated.

Hope one of the methods work for you...
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to akshayxx

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial