0

I created a 'sort by date' function that works correctly. However, when trying to create the 'sort by name' function, I assumed by that copying the 'sort by date' function and changing the variable yearTmp to nameTmp it would sort by name. It does not. Program runs but I am seeing these errors in the compiler...

[Warning] assignment makes integer from pointer without a cast 
[Warning] assignment makes pointer from integer without a cast

Code I have so far...

#include <stdio.h>
#include <string.h>

#define MAX 30

void sortByDate( int year[], char *name[], char *states[], int count);
void sortByName(int year[], char *name[], char *states[], int count);

int main()
{
     int year[MAX]; 
     int i, a;
     int count = 0;
     int choice;
     char *name[MAX],
          *states[MAX];
     char b[MAX], c[MAX];

     FILE *inp = fopen("hurricanes.txt","r");               /* defining file input    */

     for(i=0;i<MAX;i++)
     {
         if( feof(inp) )
        {
            break;
        } 

        fscanf(inp, "%d %s %29[^\n]", &a, b, c);
        year[i]=a;
        name[i] = strdup(b);
        states[i] = strdup(c);
        ++count; 

        printf("%d %s %s\n", year[i], name[i], states[i]);
     }

     printf("Press 0 to sort by date or 1 to sort by name: ");
     scanf("%d", &choice);  
     if (choice == 0)
     {
         sortByDate(year, name, states, count); 
     }
     else if ( choice == 1)
     {
          sortByName(year, name, states, count); 
     }

     getch();
     return 0;
}

void sortByDate( int year[], char *name[], char *states[], int count )
{
     int d = 0;
     int c = 0;

     int yearTmp;
     int order[count];
     int tmp = 0;

     FILE *outp = fopen("report.txt","w");                 /* defining file output   */

     for (c = 0; c < count; ++c)
     {
         order[c] = c; 
     } 

     for (c = 0 ; c < ( count - 1 ); c++)
     {
          for (d = 0 ; d < count - c - 1; d++)
          {
               if (year[d] > year[d+1])
               {
                    yearTmp = year[d];
                    year[d] = year[d+1]; 
                    year[d+1] = yearTmp; 

                    tmp = order[d];
                    order[d] = order[d+1];
                    order[d+1] = tmp;   
              }
          }
     }

     for (c = 0; c < count; ++c)
     {
          printf("%d %-10s %s\n",  year[c], name[order[c]], states[order[c]]); 
     } 
}

void sortByName(int year[], char *name[], char *states[], int count)
{
     int d = 0;
     int c = 0;

     char nameTmp;
     int order[count];
     int tmp = 0;

     FILE *outp = fopen("report.txt","w");                 /* defining file output   */

     for (c = 0; c < count; ++c)
     {
         order[c] = c; 
     } 

     for (c = 0 ; c < ( count - 1 ); c++)
     {
          for (d = 0 ; d < count - c - 1; d++)
          {
               if (name[d] > name[d+1])
               {
                    nameTmp = name[d];
                    name[d] = name[d+1]; 
                    name[d+1] = nameTmp; 

                    tmp = order[d];
                    order[d] = order[d+1];
                    order[d+1] = tmp;   
              }
          }
     }

     for (c = 0; c < count; ++c)
     {
          printf("%d %-10s %s\n",  year[order[c]], name[c], states[order[c]]); 
     } 
}

The hurricanes.txt file....

1960 Donna FL, NC
1969 Camille MS
1972 Agnes FL
1983 Alicia TX
1989 Hugo SC,NC
2005 Katrina FL, LA, MS
2005 Rita TX, LA
2005 Wilma FL
2008 Ike TX
2009 Ida MS
2011 Irene NC, NJ, MA, VT
2012 Isaac LA
1992 Andrew FL, LA
1995 Opal FL, AL
1999 Floyd NC
2003 Isabel NC, VA
2004 Charley FL, SC, NC
2004 Frances FL
2004 Ivan AL
2004 Jeanne FL
2
  • nameTmp = name[d]; : Type of name[d] is char*, but Type of nameTmp is char. Also if (name[d] > name[d+1]) , Use strcmp instead of. Commented Apr 15, 2016 at 2:03
  • 1
    I'm sensing you don't know how to compare strings in C. Read the manpage for strcmp Commented Apr 15, 2016 at 2:05

1 Answer 1

1

You started down right path, but you can't treat c-string like integers.

A string in C is an array of characters terminated by the null terminator '\0' character. In order to compare them, you have to check every element of the array. Luckily, a function exists that does that for you , namely strcmp()

What you pass into your function is an array of char *. Each one of these indexes points to an array of characters representing your strings. To compare these index you can do the following.

// Returns 0 if the are equal
// Returns something > 0 if pName[d] is lexicographically greater than pName[d+1]
// Returns something < 0 if pName[d] is lexicographically less than pName[d+1]
if(strcmp(pName[0], pName[d+1]) > 0)

Its very important that both strings are null terminated or this function has undefined behavior.

To execute the swap part of the sort algorithm you swap the pointers.

char * pTemp = pName[d];
pName[d] = pName[d+1];
pName[d+1] = pTemp;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.