Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting array-can't see where I'm going wrong! 1

Status
Not open for further replies.

Ireland1978

Programmer
Sep 29, 2005
17
0
0
GB
I've only been doing C for a few weeks, and could really do with some help with this please.

I'm attempting to write code that will take in first names, last names and ages, then prints them alphabetically to the screen, with the correct first names, last names and ages.

I'm just trying to sort the last name array first, but I can't see where I'm going wrong in this, when I run it I just get rubbishy output. When I debug it and step through it, it seems to be comparing the letters in the fname array.

Code:
/*    Store lists of names (surname first) and ages in parallel arrays 
   Sort the names into alphabetical order 
   keeping the ages with the correct names. */ 

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 
#define CLASS_SIZE 3 
#define NAME_LEN 15 

int strncmp(const char *s1, const char *s2, size_t n); 
int main (void) { 

   char lname[CLASS_SIZE][NAME_LEN];    /*Declare array of char*/ 
   char fname[CLASS_SIZE][NAME_LEN], temp; 
   int age[CLASS_SIZE]; 
    
    
   for(int i=1; i<=CLASS_SIZE; i++){ 
    
       printf("Enter first name, last name and age please:\n"); 
       scanf("%s %s %d", fname, lname, &age); 

   } 

   for (int x=0; x<CLASS_SIZE; x++) {  /*Outer loop to go through each element of array*/ 

       for (int y=1; y<NAME_LEN; y++){  /* Loop through the elements of each name */ 

           if ((strncmp(lname[y-1], lname[y],NAME_LEN )>0)){ 
           temp = *lname[y];                /* Compare strings, swap if necessary */ 
           *lname[y] = *lname[y-1]; 
           *lname[y-1] = temp; 
           } 

       } 

   } 

   for(int a=1; a<=CLASS_SIZE; a++) 
    
       printf("%s\n", lname[a] );  /* Print ordered names to screen */ 
    

   return 0; 
}

I haven't even STARTED trying to work out how to get the other two arrays to match the lname one once they're sorted!

Any help greatly appreciated! Thanks
 
You need to look again at your pointers in the section
Code:
           temp = *lname[y];                /* Compare strings, swap if necessary */
           *lname[y] = *lname[y-1];
           *lname[y-1] = temp;

What you require, assuming your logic is correct, is
Code:
char *temp;
....
....
....
temp = lname[y];
lname[y] = lname[y-];
lname[y-1] = temp;
*lname[y] is equivalent (sort of) to lname[y][0] and is a char, not a pointer to a char and it's the pointers you want to swap.

Columb Healy
 
Thanks for that. I put your code in, and got the following error messages:

error C2106: '=' : left operand must be l-value
error C2440: '=' : cannot convert from 'char *' to 'char [15]'


So my complete code now looks like this:

Code:
/*	Write a program that stores lists of names (surname first) and ages in parallel arrays 
	and sorts the names into alphabetical order 
	keeping the ages with the correct names. */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define CLASS_SIZE 3
#define NAME_LEN 15

int strncmp(const char *s1, const char *s2, size_t n);
int main (void) {

	char lname[CLASS_SIZE][NAME_LEN];	/*Declare array of char*/
	char fname[CLASS_SIZE][NAME_LEN];
	int age[CLASS_SIZE];
	char *temp;
	
	
	for(int i=1; i<=CLASS_SIZE; i++){
	
		printf("Enter first name, last name and age please:\n");
		scanf("%s %s %d", fname[i], lname[i], &age[i]);

	}

	for (int x=0; x<CLASS_SIZE; x++) {

		for (int y=1; y<NAME_LEN; y++){

			if (strncmp(lname[y-1], lname[y],NAME_LEN)>0){
			  temp = lname[y];
			  lname[y] = lname[y-1];
			  lname[y-1] = temp;
	
			}
		}
	}

	for(int a=1; a<=CLASS_SIZE; a++)
	
		printf("%s\n", lname[a] );
	

	return 0;
}
 
Arrays don't like being assigned. It is OK for variables but string arrays use strcpy.
Code:
char temp[NAME_LEN];
...
strcpy (temp, lname[y]);
strcpy (lname[y], lname[y-1]);
strcpy (lname[y-1], temp);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top