Advertisement
Guest User

note_compare.c

a guest
Apr 5th, 2020
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.30 KB | None | 0 0
  1. // Compare two Note structs
  2. // list_compare.c
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8.  
  9. // A struct note * IS a Note
  10. typedef struct note *Note;
  11.  
  12. //There are 10 octaves (0 to 9) and 12 notes (0 to 11)
  13. struct note {
  14.     int octave;
  15.     int key;
  16.     struct note *next;
  17. };
  18.  
  19. int note_compare(Note a, Note b);
  20.  
  21. //Returns 1 if a is higher than b
  22. //       -1 if b is higher than a
  23. //        0 if they are equal
  24. int note_compare(Note a, Note b) {
  25.     //TODO: Change this return.
  26.     int value = 0;
  27.     if (a->octave > b->octave) {
  28.         value = 1;
  29.     } else if (a->octave < b->octave) {
  30.         value = -1;
  31.     } else {
  32.         if (a->key > b->key) {
  33.             value = 1;
  34.         } else if (a->key < b->key) {
  35.             value = -1;
  36.         } else {
  37.             value = 0;
  38.         }
  39.     }
  40.     return value;
  41. }
  42. int main(void) {
  43.  
  44.     int octave, key;
  45.     scanf("%d %d", &octave, &key);
  46.     struct note a = {octave, key, NULL};
  47.     scanf("%d %d", &octave, &key);
  48.     struct note b = {octave, key, NULL};
  49.     int compared = note_compare(&a, &b);
  50.     if (compared == 1) {
  51.         printf("a is higher than b\n");
  52.     } else if (compared == -1) {
  53.         printf("b is higher than a\n");
  54.     } else {
  55.         printf("a and b are equal\n");
  56.     }
  57.    
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement