sorting 2d arrays

Closed Thread

Join Date: Nov 2003
Posts: 1
Reputation: ucdchick is an unknown quantity at this point 
Solved Threads: 0
ucdchick ucdchick is offline Offline
Newbie Poster

sorting 2d arrays

 
0
  #1
Nov 15th, 2003
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.
Quick reply to this message  
Join Date: Dec 2003
Posts: 55
Reputation: r0ckbaer is an unknown quantity at this point 
Solved Threads: 6
r0ckbaer r0ckbaer is offline Offline
Junior Poster in Training

Re: sorting 2d arrays

 
0
  #2
Dec 5th, 2003
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h> // for gettickcount, u could use time(NULL) aswell
  5.  
  6. #define MAX 99
  7. #define LEN 21 //add 1 for trailing 0 char
  8.  
  9. int compare(const void *arg1, const void *arg2)
  10. {
  11. return stricmp((char*)arg1, (char*)arg2);
  12. }
  13.  
  14. /* this getnames generates random chars and fills them
  15.  * into a 2-dimensional array; in order to read random
  16.  * inputted names from stdin u can modify it to your needs
  17.  * (i'll leave this for you as an easy exercice)
  18.  */
  19. void getnames(char names[][LEN], int num)
  20. {
  21. int i, j;
  22. char arr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  23.  
  24. for (i = 0; i < num; i++)
  25. {
  26. j = rand() % 20;
  27. while (j >= 0)
  28. {
  29. names[i][j] = arr[rand() % 36];
  30. j--;
  31. }
  32. }
  33. }
  34.  
  35. void sort_names(char names[][LEN], int num, int type)
  36. {
  37. int i, j;
  38. char temp[MAX][LEN] = {{0}, {0}};
  39.  
  40. /* this has been sorted from 0-Z
  41. * qsort() is a sorting function
  42. * that u can find in stdlib.h
  43. */
  44. qsort((void*)names, (size_t)num, LEN, compare);
  45.  
  46. switch (type)
  47. {
  48. /* sort from last to first */
  49. case 1:
  50. for (i = 0; i < num; i++)
  51. {
  52. strcpy(&temp[i], &names[i]);
  53. memset(&names[i], 0, LEN);
  54. }
  55. for (i = 0, j = num - 1; i < num; i++, j--)
  56. strcpy(&names[i], &temp[j]);
  57. break;
  58. /* do nothing all is sorted already */
  59. case 2:
  60. break;
  61. /* sorry but u weren't much clear with this option
  62. * so u can implement the sort via age here
  63. */
  64. case 3:
  65. break;
  66. }
  67.  
  68. }
  69. void print_names(char names[][LEN], int num)
  70. {
  71. int i;
  72.  
  73. printf("\n");
  74. for (i = 0; i < num; i++)
  75. printf("%s\n", names[i]);
  76. printf("\n");
  77. }
  78.  
  79. int main(void)
  80. {
  81. int type, num;
  82. char names[MAX][LEN] = {{0}, {0}};
  83.  
  84. srand(GetTickCount());
  85.  
  86. /* Ask user for how many names*/
  87. printf("How many names?: ");
  88. scanf("%d",&num);
  89. if (num <= 0) exit(0);
  90. if (num >= 100)
  91. {
  92. printf("Entered too many\n");
  93. exit(0);
  94. }
  95.  
  96. /*Get the names for the users*/
  97. getnames(names, num);
  98.  
  99. /*Ask user how to sort names*/
  100. printf("How to sort names?\n");
  101. printf("1: Last\n2: First\n3: Age\n");
  102. scanf("%d",&type);
  103.  
  104. /*Sort the names*/
  105. sort_names(names, num, type);
  106.  
  107. /*Print the sorted array out*/
  108. print_names(names, num);
  109.  
  110. system("PAUSE");
  111.  
  112. return 0;
  113. }

i hope this will help u out
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC