5 Replies - 636 Views - Last Post: 30 September 2011 - 01:48 AM Rate Topic: -----

#1 Aperson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-September 11

[C] Sorting a matrix of strings, with reference parameters

Posted 30 September 2011 - 12:50 AM

Hey
I've been trying to sort an array of strings by using bubble sort.

I don't think i understand very well how pointers are working here, but after several attempts i managed to capture the stings into the matrix but the bubble sort algorithm is doing infinite loop, most likely because it's not moving the values properly..

Here's the function


void sort(char name[][50],float prom[],int cred[], int count)
{   int c,continue=1,x,y;
    char *temp[50];
    clrscr();

    while (continue)
    {
        continue=0;

        for(c=0;c<3;c++)
        {   printf("%d\n",strcmpi(nom[c],name[c+1]));

            if(strcmpi(name[c],name[c+1])>0) 
            {
                continue=1;
                temp[0]=name[c];
                name[c][0]=name[c+1][0];
                name[c+1][0]=temp[0];
            }
        }
    }

    printf("Sorted List\n\r");

    for(c=0;c<3;c++)
        printf("%s\n",name[c]);

    getch();
}



Since the functions strcmpi and strcpy worked only when i used name[x] instead of name[x][0] (as it is a matrix) i figured out this was better and that the second dimension was not necessary, but it loops on the bubble sort algorithm whether there are 2 dimensions or not...

Also, should *temp have a * at all? It keeps giving different errors when i try to change variables and when i use 1 or 2 dimensions on the matrix.

Any insight on what i am doing wrong and/or how the dimension thingy works would be greatly appreciated.

PD This is a Turbo C/C++ code, i am forced to use it at school @[email protected]

This post has been edited by Aperson: 30 September 2011 - 12:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [C] Sorting a matrix of strings, with reference parameters

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: [C] Sorting a matrix of strings, with reference parameters

Posted 30 September 2011 - 01:11 AM

Welcome to DIC.
Great first post.

Please give us a main() that is just enough to drive this function and that produces the error that you are having trouble with.
Was This Post Helpful? 0
  • +
  • -

#3 Aperson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-September 11

Re: [C] Sorting a matrix of strings, with reference parameters

Posted 30 September 2011 - 01:19 AM

View Postjanotte, on 30 September 2011 - 01:11 AM, said:

Welcome to DIC.
Great first post.

Please give us a main() that is just enough to drive this function and that produces the error that you are having trouble with.


Thanks ;D

Sorry, should have posted it...

Here it is

void main()
{   char name[max][50],op;
    int mejor, cred[max],cont=0;
    float prom[max];

    do
    {   clrscr();
        puts("1)Registrar\n2)Borrar\n3)Consulta");
        puts("4)Mejor promedio\n5)Ver alumnos\n6 Salir");
        op=getche();

        switch(op)
        {
            case '1': registro (name,prom,cred,&cont); break;
            case '2': /*baja (name,prom,cred,&cont);*/ break;
            case '3': /*consulta (name,prom,cred,cont);*/ break;
            case '4': /*mejor=mejorPromedio (prom,cont);
                      printf("El promedio mas alto es %.2f y lo tiene %s",prom
                      getch(); */
                      break;
            case '5': ordena (nom,prom,cred,cont); break;
            case '6': break;
        }
    }while(op!='6');
}



That's all the main does..

The program is going to be a small program to register students, so it's just a menu with functions.

This post has been edited by Aperson: 30 September 2011 - 01:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: [C] Sorting a matrix of strings, with reference parameters

Posted 30 September 2011 - 01:19 AM

Change nom[] to name[], for one thing.
printf("%d\n",strcmpi(nom[c],name[c+1]));


If you put a printf(), and a getchar(), into the middle of your swap code, you can easily see what is going on (right or wrong).

And I second the welcome to the forum!

This post has been edited by Adak: 30 September 2011 - 01:20 AM

Was This Post Helpful? 0
  • +
  • -

#5 Aperson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-September 11

Re: [C] Sorting a matrix of strings, with reference parameters

Posted 30 September 2011 - 01:22 AM

Thanks ;D

Yeah, sorry that nom thing is because i translated the nom(nombre) to english name, but missed that one q:

Yeah i use the prtinf() thing a lot to check whats going on, but i was having too many problems with the matrix thing and the other variables, so i figured out it was just better to try to understand that first x_x

This post has been edited by Aperson: 30 September 2011 - 01:26 AM

Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: [C] Sorting a matrix of strings, with reference parameters

Posted 30 September 2011 - 01:48 AM

Okay if I hack away the nasty Turbo stuff so I can get meaningful compiler messages then this is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void sort(char name[][50],float prom[],int cred[], int count)
{   int c,theContinue=1,x,y;
    char *temp[50];
    // clrscr();

    while (theContinue)
    {
        theContinue=0;

        for(c=0;c<3;c++)
        {   printf("%d\n",strcmp(name[c],name[c+1]));

            if(strcmp(name[c],name[c+1])>0)
            {
                theContinue=1;
                temp[0]=name[c];
                name[c][0]=name[c+1][0];
                name[c+1][0]=temp[0];
            }
        }
    }

    printf("Sorted List\n\r");

    for(c=0;c<3;c++)
        printf("%s\n",name[c]);

    getchar();
}

void main()
{   char name[max][50],op;
    int mejor, cred[max],cont=0;
    float prom[max];

    do
    {
        // clrscr();
        puts("1)Registrar\n2)Borrar\n3)Consulta");
        puts("4)Mejor promedio\n5)Ver alumnos\n6 Salir");
        op=getchar();

        switch(op)
        {
            case '1': registro (name,prom,cred,&cont); break;
            case '2': /*baja (name,prom,cred,&cont);*/ break;
            case '3': /*consulta (name,prom,cred,cont);*/ break;
            case '4': /*mejor=mejorPromedio (prom,cont);
                      printf("El promedio mas alto es %.2f y lo tiene %s",prom
                      getch(); */
                      break;
            case '5': ordena (nom,prom,cred,cont); break;
            case '6': break;
        }
    }while(op!='6');
}



I get these messages:
clang -Wall dic.c
dic.c:22:29: warning: incompatible pointer to integer conversion assigning to
      'char' from 'char *'
                name[c+1][0]=temp[0];
                            ^~~~~~~~
dic.c:6:25: warning: unused variable 'x' [-Wunused-variable]
{   int c,theContinue=1,x,y;
                        ^
dic.c:6:27: warning: unused variable 'y' [-Wunused-variable]
{   int c,theContinue=1,x,y;
                          ^
dic.c:35:1: error: 'main' must return 'int'
void main()
^
dic.c:36:15: error: use of undeclared identifier 'max'
{   char name[max][50],op;
              ^
dic.c:37:21: error: use of undeclared identifier 'max'
    int mejor, cred[max],cont=0;
                    ^
dic.c:38:16: error: use of undeclared identifier 'max'
    float prom[max];
               ^
3 warnings and 4 errors generated.



Which leads me to this question.
What is this line:
char *temp[50];

and this line:
name[c+1][0]=temp[0];

trying to do?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1