0

Hey everyone,
I'm having trouble with a program I'm working on. In this program I'm supposed to sort an array of names. I ask the user how manys names will be inputed. Number of names will not exceed 100. Names will be in the format of "Last,First,age" with no spaces and there will always be three fields. The user will always use this format. The names string will be at most 40 characters. After the user has inputed the names, I ask how are they to be sorted. They are sorted by either last, first, or age. Then I print the names out in sorted order. For people who have the same last name, sort by first name. For people with the same age, sort by last name and if that is also the same sort by first name. For people with the same first name, sort by last name. No two people will have the same first and last name.
There are 3 files, one with main, one with the functions that are called (one function scans the array, another sorts the array, and the last prints it) and another which is the header that contains the function prototypes.
I was able to do the coding for the main program which looks like this:

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


int main(void){


int type,num;
char names[MAX][LEN];


/* Ask user for how many names*/
printf("How many names?: ");
scanf("%d",&num);
if(num <= 0)
exit(0);
if(num>=100){
printf("Entered too many\n");
exit(0);
}


/*Get the names for the users*/
getnames(names,num);


/*Ask user how to sort names*/
printf("How to sort names?\n");
printf("1: Last\n2: First\n3: Age\n");
scanf("%d",&type);


/*Sort the names*/
sort_names(names,num,type);


/*Print the sorted array out*/
print_names(names,num);


return(0);
}

I was also able to do a little bit of the functions:

#include <stdio.h>
#include <strings.h>
#include "names_func.h"


char getnames(char names[], int num){
int i;
for(i=0; i<num; i++){
scanf("%s", names);
}
}
char sort_names(char names, int num, int type){
if(type==1){

(This is the part which I dont know how to do. I am confused about how to sort the 2d array.)

If anyone can help me with the coding for this that would be great.
Thanks for your help.

Edited by deceptikon: Fixed formatting

2
Contributors
1
Reply
3
Views
14 Years
Discussion Span
Last Post by r0ckbaer
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h> // for gettickcount, u could use time(NULL) aswell

#define MAX 99
#define LEN 21 //add 1 for trailing 0 char

int compare(const void *arg1, const void *arg2)
{
   return stricmp((char*)arg1, (char*)arg2);
}

/* this getnames generates random chars and fills them
 * into a 2-dimensional array; in order to read random
 * inputted names from stdin u can modify it to your needs
 * (i'll leave this for you as an easy exercice)
 */
void getnames(char names[][LEN], int num)
{
	int i, j;
	char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

	for (i = 0; i < num; i++)
	{
		j = rand() % 20;
		while (j >= 0)
		{
			names[i][j] = arr[rand() % 36];
			j--;
		}
	}
}

void sort_names(char names[][LEN], int num, int type)
{
	int i, j;
	char temp[MAX][LEN] = {{0}, {0}};

	/* this has been sorted from 0-Z
	 * qsort() is a sorting function
	 * that u can find in stdlib.h
	 */
	qsort((void*)names, (size_t)num, LEN, compare);

	switch (type)
	{
		/* sort from last to first */
		case 1:
			for (i = 0; i < num; i++)
			{
				strcpy(&temp[i], &names[i]);
				memset(&names[i], 0, LEN);
			}
			for (i = 0, j = num - 1; i < num; i++, j--)
				strcpy(&names[i], &temp[j]);
			break;
		/* do nothing all is sorted already */
		case 2:
			break;
		/* sorry but u weren't much clear with this option
		 * so u can implement the sort via age here
		 */
		case 3:
			break;
	}

}
void print_names(char names[][LEN], int num)
{
	int i;

	printf("\n");
	for (i = 0; i < num; i++)
		printf("%s\n", names[i]);
	printf("\n");
}

int main(void)
{
	int type, num;
	char names[MAX][LEN] = {{0}, {0}};

	srand(GetTickCount());

	/* Ask user for how many names*/
	printf("How many names?: ");
	scanf("%d",&num);
	if (num <= 0) exit(0);
	if (num >= 100)
	{
		printf("Entered too many\n");
		exit(0);
	}

	/*Get the names for the users*/
	getnames(names, num);

	/*Ask user how to sort names*/
	printf("How to sort names?\n");
	printf("1: Last\n2: First\n3: Age\n");
	scanf("%d",&type);

	/*Sort the names*/
	sort_names(names, num, type);

	/*Print the sorted array out*/
	print_names(names, num);

	system("PAUSE");

	return 0;
}

i hope this will help u out ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.