Home > Software design >  im trying to remove an element from parallel arrays
im trying to remove an element from parallel arrays

Time:01-09

so I have 4 parallel arrays and I'm trying to remove one element from them at the same time and decrease the size of it it by one in the process, however there is something wrong with this code:

void removeAccount(char names[MAXSIZE][MAXSIZE], int stdID[MAXSIZE], float phones[MAXSIZE], int registrationYear[MAXSIZE])
{
    int ti;  // to save the student number entered from the keyboard.
    int tii; // to save the index of the entered student number.
    if (size <= 0)
    {
        printf("the list is empty\n");
    }
    printf("please enter a studnet number\n");
    scanf("%d", &ti);
    for (int f = 0; f < size; f  )
    {
        if (stdID[f] != ti)
        {
            printf("The student doesn't exists\n");
            printf("Please enter a student number\n");
            scanf("%d", &ti);
        }
        if (stdID[f] == ti)
        {
            tii = f;
            break;
        }
    }
    for (int v = tii - 1; v < size - 1; tii  )
    {
        strcpy(names[v], names[v   1]);
        phones[v] = phones[v   1];
        stdID[v] = stdID[v   1];
        registrationYear[v] = registrationYear[v   1];
    }
    size--;

CodePudding user response:

For starters it is a bad design of a function when the function depends on a global variable as your function depends on the global variable size.

This code snippet

printf("please enter a studnet number\n");
scanf("%d", &ti);
for (int f = 0; f < size; f  )
{
    if (stdID[f] != ti)
    {
        printf("The student doesn't exists\n");
        printf("Please enter a student number\n");
        scanf("%d", &ti);
    }
    if (stdID[f] == ti)
    {
        tii = f;
        break;
    }
}

does not make a sense.

Instead you should write

int pos;

do
{
    printf("please enter a student number\n");
    scanf("%d", &ti);

    pos = 0;

    while ( pos < size && stdID[pos] != ti )   pos;

    if ( pos == size )
    {
        printf("The student doesn't exists\n");
    }
} while ( pos == size );

This for loop

for (int v = tii - 1; v < size - 1; tii  )
{
    strcpy(names[v], names[v   1]);
    phones[v] = phones[v   1];
    stdID[v] = stdID[v   1];
    registrationYear[v] = registrationYear[v   1];
}

is also incorrect.

It should look like

while (   pos < size )
{
    strcpy(names[pos - 1], names[pos]);
    phones[pos - 1] = phones[pos];
    stdID[pos - 1] = stdID[pos];
    registrationYear[pos - 1] = registrationYear[pos];
}
  •  Tags:  
  • Related