Home › Forums › C Programming › Bubble and Selection sort
- This topic has 0 replies, 1 voice, and was last updated 17 years, 10 months ago by danbinarim.
- AuthorPosts
- February 11, 2007 at 5:08 pm #1961danbinarimParticipant
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 arrayCan anyone help me with this.
Many Thanks.
AndrewHere is the CODE:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include <stdio.h><br />#include <ctype.h><br />#include <stdlib.h><br /><br />#define CITY 4<br />#define TEMPS 4<br /><br />int temp[CITY][TEMPS];<br /><br />void enter_temp(void);<br />int get_temp(int num);<br />void disp_temp(int g[][TEMPS]);<br /><br />void main()<br />{<br />char ch, str[80];<br /><br />for( ; ; ) {<br />do {<br />printf("(E)nter TEMPsn");<br />printf("(R)eport TEMPsn");<br />printf("(Q)uitn");<br />gets(str);<br />ch = toupper(*str);<br />} while(ch!='E' && ch!='R' && ch!='Q' && ch!='S');<br /><br />switch(ch) {<br />case 'E':<br />enter_temp();<br />break;<br />case 'R':<br />disp_temp(temp);<br />break;<br />case 'Q':<br />exit(0);<br />}<br />}<br />}<br /><br />/* Enter the temps. */<br />void enter_temp(void)<br />{<br />int t, i;<br />printf ("City #1 - Diblin, City #2 - Cork, n");<br />printf ("City #3 - Galway, City #4 - Monaghann");<br />for(t=0; t<CITY; t++) {<br />printf("City # %d:n", t+1);<br />for(i=0; i<TEMPS; ++i)<br />temp[t] = get_temp(i);<br />}<br />}<br /><br />/* Read a TEMPs. */<br />int get_temp(int num)<br />{<br />char s[80];<br /><br />printf ("TEMP #1 - 6am, TEMP #2 - 12pm, n");<br />printf ("TEMP #3 - 6pm, TEMP#4 - 12amn");<br />printf("Enter TEMP # %d:n", num+1);<br />gets(s);<br />return(atoi(s));<br />}<br /><br />/* Display temps. */<br />void disp_temp(int g[][TEMPS])<br />{<br />int t, i;<br /><br />for(t=0; t<CITY; ++t) {<br />printf("City # %d:n", t+1);<br />for(i=0; i<TEMPS; ++i)<br />printf("TEMP #%d is %dn", i+1, g[t]);<br />}<br />}
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.