Home Forums C Programming Bubble and Selection sort

Viewing 0 reply threads
  • Author
    Posts
    • #1961
      danbinarim
      Participant

      Hi everyone.

      I have a C program (see code below) which I need to urgrade, but I’m new to C programming and after hour of trying to do this I finally gave up and decided to ask some one for HELP.
      Here is what I need to do:

      a. Extract the temps for the City #1 and store in a one dimensional array, display the array.
      b. Sort the one dimensional array of City #1 temps using a selection sort and display the sorted array
      c. Extract the temps recorded from all Cities as 3rd entry and store in a one dimensional array, display the array
      d. Sort the one dimensional array of temps taken as 3rd entry using a bubble sort and display the sorted array

      Can anyone help me with this.
      Many Thanks.
      Andrew

      Here is the CODE:

      #include 
      #include
      #include

      #define CITY 4
      #define TEMPS 4

      int temp[CITY][TEMPS];

      void enter_temp(void);
      int get_temp(int num);
      void disp_temp(int g[][TEMPS]);

      void main()
      {
      char ch, str[80];

      for( ; ; ) {
      do {
      printf("(E)nter TEMPsn");
      printf("(R)eport TEMPsn");
      printf("(Q)uitn");
      gets(str);
      ch = toupper(*str);
      } while(ch!='E' && ch!='R' && ch!='Q' && ch!='S');

      switch(ch) {
      case 'E':
      enter_temp();
      break;
      case 'R':
      disp_temp(temp);
      break;
      case 'Q':
      exit(0);
      }
      }
      }

      /* Enter the temps. */
      void enter_temp(void)
      {
      int t, i;
      printf ("City #1 - Diblin, City #2 - Cork, n");
      printf ("City #3 - Galway, City #4 - Monaghann");
      for(t=0; t printf("City # %d:n", t+1);
      for(i=0; i temp[t] = get_temp(i);
      }
      }

      /* Read a TEMPs. */
      int get_temp(int num)
      {
      char s[80];

      printf ("TEMP #1 - 6am, TEMP #2 - 12pm, n");
      printf ("TEMP #3 - 6pm, TEMP#4 - 12amn");
      printf("Enter TEMP # %d:n", num+1);
      gets(s);
      return(atoi(s));
      }

      /* Display temps. */
      void disp_temp(int g[][TEMPS])
      {
      int t, i;

      for(t=0; t printf("City # %d:n", t+1);
      for(i=0; i printf("TEMP #%d is %dn", i+1, g[t]);
      }
      }

Viewing 0 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.