int main(void) { int i,j,k,l; struct listnode *node, *tmpnode; node = NULL; for( i=0; i < 10; i++) { for( j=0; j < 10; j++) { for( k=0; k < 10; k++) { tmpnode = node; node = (struct listnode *) malloc( sizeof(struct listnode)); node->word = (char *) malloc( 50*sizeof(char)); sprintf (node->word, "st %d %d %d", (3*i)%10, (3*j)%10, (7*k)%10 ); node->next = tmpnode; } } } tmpnode = node; node = (struct listnode *) malloc( sizeof(struct listnode)); node->word = "first string"; node->next = tmpnode; tmpnode = node; node = (struct listnode *) malloc( sizeof(struct listnode)); node->word = "second string"; node->next = tmpnode; tmpnode = node; node = (struct listnode *) malloc( sizeof(struct listnode)); node->word = "the last string"; node->next = tmpnode; printf("\n prepared list, now starting sort\n"); node = stringsort(node); printf("\n checking sorted list\n"); printf("1: %s\n", node->word); node = node->next; printf("2: %s\n", node->word); node=node->next; for( l=0; l < 1000; l++) { if( node == NULL ) { printf("List ended early\n"); exit(0); } sscanf( node->word, "st %d %d %d", &i, &j, &k); if( 100*i + 10*j +k != l ) { printf("Node contains wrong value\n"); exit(0); } node = node->next; } printf("tested %d strings, found in sorted order.\n",l); printf("last: %s\n", node->word); if( node->next != NULL ) { printf("List too long\n"); exit(0); } exit(0); }
main() { int ar[MAXROW][MAXCOL],n, m, i,j, col; char cflag, names[MAXROW][MAXCOL]; do { printf("Enter the number of students and the number of scores \n"); scanf("%d%d", &n, &m); } while (n > MAXROW || m > MAXCOL); for (i = 0; i < n; i++) { printf("Enter the last name of student %d (no spaces in the name) followed by Enter\n",i+1); scanf ("%s", names[i]); printf("Enter %d integer scores for the student %s \n",m, names[i]); for (j = 0; j < m; j++) { scanf ("%d", &ar[i][j]); } } printf("Now we will see if the data got entered correctly\n"); dataprint(names, ar, n, m); getchar(); /* this consumes the leftover ENTER character */ for (;;) { printf("Enter N if you want to sort on the names, or C if you want to sort on a column, or E to exit\n"); cflag = getchar(); getchar(); printf ("You entered %c\n", cflag); if (cflag == 'E') return; if (cflag == 'C') { printf("Enter a column number between 1 and %d to sort on: ", m); scanf (" %d", &col); getchar(); printf("The entry was %d\n ", col); twodsort (ar, names, n, m, col-1); /* for (i = 0; i < n; i++) { printf("%s ", names[i]); } */ printf("\n\nNow we will see if the array got sorted correctly on column %d\n", col); dataprint(names, ar, n, m); } else { stringsort(ar, names, n, m); printf("\n\nNow we will see if the array got sorted correctly on the names\n"); dataprint(names, ar, n, m); } } }