Advertisement
regzarr

sorting and searching

Mar 17th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define NAME_LENGTH 100
  6. /* 2. Write a program to show the top students from a class given
  7.  * their names and grades. The program should read the names of
  8.  * the students and their grades from the keyboard. The program
  9.  * will display a menu and allow the user to sort the students by
  10.  * alphabetic order or by grades. For example the menu can look like
  11.  * this:
  12.  
  13.  0. Exit program
  14.  1. Give N, number of students
  15.  2. Enter the students
  16.  3. Display class in alphabetic order
  17.  4. Display class creating a top based on grades
  18.  5. Display the first 3 students according to their grades
  19.  
  20.  * but feel free to improve.
  21.  */
  22.  
  23. // Representation of table (first row represents the array index/position)
  24. // +-----------------+------------------------------------------+
  25. // | 0               | 1-99                                     |
  26. // +-----------------+------------------------------------------+
  27. // | grade (1 to 10) | String of name (including '\0') |
  28. // +-----------------+------------------------------------------+
  29.  
  30. /* @table: table of students (array of strings)
  31.  * @m: number of rows
  32.  */
  33.  
  34. void addStudent(char (*table)[100], unsigned *m) {
  35.     char s[100];
  36.     unsigned i = 0, j;
  37.     table = realloc(table, sizeof(char[*m][NAME_LENGTH]));
  38.     printf("Type the name of the student:\n");
  39.     // read until newline ('\n')
  40.     fgets(&table[*m - 1][1], 99, stdin);
  41.     // remove the newline
  42.     //table[*m - 1][strlen(table[*m - 1])] = '\0';
  43.     printf("Type the grade of the student:\n");
  44.     int grade;
  45.     do {
  46.         scanf("%d", &grade); // might improve later
  47.     } while (grade > 10 || grade < 1);
  48.     // placing the grade in the table
  49.     table[*m - 1][0] = grade;
  50.     // placing the name in the table
  51.     //memcpy(&table[*m-1][1], s, strlen(s) + 1); // +1 for the terminating NULL character
  52.     *m = *m+1;
  53. }
  54.  
  55. int main() {
  56.     unsigned size = 1;
  57.     // allocating 1 byte because if size is 0, the return value depends on the particular library implementation (it may or may not be a null pointer)
  58.     char (*students)[100] = malloc(1 * sizeof(char));
  59.     addStudent(students, &size);
  60.     printf("Name of first student:\n%c",(*(students+0)+1));
  61.     //addStudent(students, &size);
  62.     free(students);
  63.     return 0;
  64. }
  65.  
  66. //getdelim(char **lineptr, size_t *n, int delim, FILE *stream); (?): why does it take n as a pointer instead of simply n?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement