multidimensional array pointers

0

The following code is supposed to order the surnames alphabetically but for some reason it stops, the compiler does not mark any warning, nor error so if someone knows what happens explain to me what happens please

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
double performancecounter_diff(LARGE_INTEGER *a, LARGE_INTEGER *b)
{
  LARGE_INTEGER freq;
  QueryPerformanceFrequency(&freq);
  return (double)(a->QuadPart - b->QuadPart) / (double)freq.QuadPart;
}


int main()
{
    char apellidos[10][10] = {"Perez", "Bartolome", "Lopez", "Gonzalez", "Jimenez", "McDonald", "Hernandez", "Pulido", "Trejo", "Cernas"};
    char apellidos2[10][10] = {"Perez", "Bartolome", "Lopez", "Gonzalez", "Jimenez", "McDonald", "Hernandez", "Pulido", "Trejo", "Cernas"};

    double tExeIns,tExeShe,c;


    printf("Ordenamiento por Insercion\n\n");
    insert(&apellidos, tExeIns);

    system("pause");
    return 0;
}


int compare(char *cadena1, char *cadena2)
{
    int a, b;
    for(a = 0; a < 10; a++)
    {
        if(*(cadena1 + a) > *(cadena2 + a))
        {
            b = 1;
            return b;
        }

    }
    return 0;
}



int insert(char *array, double *secs)
{
  LARGE_INTEGER t_ini, t_fin;


  QueryPerformanceCounter(&t_ini);

    int i, a;
  for (i=1; i < 10; i++) 
  {
    char index[10];// = array[i];
    strcpy(index, &array[i]);
    a = i-1;
    while (a >= 0 && compare(&array[a], index)) 
    {
      array[a + 1] = array[a];
      a--;
    }

    strcpy(&array[a+1], index);
    //array[a+1] = index;
  }

  QueryPerformanceCounter(&t_fin);

  *secs = performancecounter_diff(&t_fin, &t_ini);
  printf("%.16g milliseconds\n", *secs * 1000.0);
  return 0;
}
    
asked by Manuel Fuentes M 31.05.2017 в 18:54
source

1 answer

0

The program had several errors. I attach the corrected version. Ideally, you should compare it meticulously. What I changed is:

  • The array (that is, surnames) is an array of char arrays, so I changed the header of the insert function from int insert(char *array, double *secs) to int insert(char array[][10], double *secs)
  • In the same function I eliminated the & that should not go on the lines strcpy(index, &array[i]); and strcpy(&array[a+1], index); .
  • The invocation to the function was also wrong: insert(&apellidos, tExeIns); must be insert(apellidos, &tExeIns);
  • Finally, the function compare is wrong because it lacks the else to finish returning zero when it finds a character of the first string that is "less" than its corresponding in the second. So I eliminated this function and used strcmp that already exists in string.h, but for this I had to explicitly add the condition > 0 on line while (a >= 0 && compare(&array[a], index)) that became while (a >= 0 && strcmp(array[a], index) > 0) .
  • As verification, after ordering I added the cycle to show the results:

for (int i=0; i<10;i++) printf("%s\n", apellidos[i]);

This is the modified version that works:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
double performancecounter_diff(LARGE_INTEGER *a, LARGE_INTEGER *b)
{
  LARGE_INTEGER freq;
  QueryPerformanceFrequency(&freq);
  return (double)(a->QuadPart - b->QuadPart) / (double)freq.QuadPart;
}

int insert(char array[][10], double *secs)
{
  LARGE_INTEGER t_ini, t_fin;


  QueryPerformanceCounter(&t_ini);

  int i, a;
  char index[10];
  for (i=1; i < 10; i++) 
  {
    strcpy(index, array[i]);
    a = i-1;
    while (a >= 0 && strcmp(array[a], index) > 0) 
    {
      strcpy(array[a + 1],  array[a]);
      a--;
    }

    strcpy(array[a+1], index);
  }

  QueryPerformanceCounter(&t_fin);

  *secs = performancecounter_diff(&t_fin, &t_ini);
  printf("%.16g milliseconds\n", *secs * 1000.0);
  return 0;
}


int main()
{
    char apellidos[10][10] = {"Perez", "Bartolome", "Lopez", "Gonzalez", "Jimenez", "McDonald", "Hernandez", "Pulido", "Trejo", "Cernas"};
    char apellidos2[10][10] = {"Perez", "Bartolome", "Lopez", "Gonzalez", "Jimenez", "McDonald", "Hernandez", "Pulido", "Trejo", "Cernas"};

    double tExeIns,tExeShe,c;


    printf("Ordenamiento por Insercion\n\n");
    insert(apellidos, &tExeIns);
    for (int i=0; i<10;i++)
        printf("%s\n", apellidos[i]);

    system("pause");
    return 0;
}
    
answered by 12.06.2017 в 20:37