Newbie needs help on simple problem 
Author Message
 Newbie needs help on simple problem

Hi,
I'm in an Intro to C course and I'm studying for a final exam, anyway
this is the only sample problem that I'm having difficulty with. I'm
having a bit of problem creating this program, specifically how would I
sort the population column:

Write a complete program that displays a city name, and requests the
User to enter the population for that city.  This will be done five
times, for the following cities, and in the following order:  Miami,
Boston, Lansing, Detroit, and  Chicago.  The program will then display
the cities and their population in order of decreasing population.  For
example, the final display for a particular set of population data could
look like:

CITY   POPULATION (millions)

Chicago   3.4
Boston    2.7
Miami    2.6
Detroit    1.2
Lansing   0.4

Of course your population data and the order of the listing will be
different, depending on the values of population data entered from the
keyboard by the User.

I would appreciate any clues or ideas, this is what I have so far (as
you can see I'm stuck on the sorting part)
#include <vcl.h>
#include <stdio.h>
#include <conio.h>

void main (void)
{  float chicago, boston,detroit,miami,lansing;
printf("Please enter the population of Miami:  ");
scanf("%d", &miami);
printf("Please enter the population of Boston:  ");
scanf("%d", &boston);
printf("Please enter the population of Lansing:  ");
scanf("%d", &lansing);
printf("Please enter the population of Detroit:  ");
scanf("%d", &detroit);
printf("Please enter the population of Chicago:  ");
scanf("%d", &chicago);

printf("CITY         Population");

getch();

Quote:
}

Thanks
Mike


Sun, 05 Jun 2005 16:08:42 GMT  
 Newbie needs help on simple problem

Quote:

> I'm in an Intro to C course and I'm studying for a final exam, anyway
> this is the only sample problem that I'm having difficulty with. I'm
> having a bit of problem creating this program, specifically how would I
> sort the population column:
> Write a complete program that displays a city name, and requests the
> User to enter the population for that city.  This will be done five
> times, for the following cities, and in the following order:  Miami,
> Boston, Lansing, Detroit, and  Chicago.  The program will then display
> the cities and their population in order of decreasing population.  For
> example, the final display for a particular set of population data could
> look like:
> CITY   POPULATION (millions)
> Chicago   3.4
> Boston    2.7
> Miami    2.6
> Detroit    1.2
> Lansing   0.4
> Of course your population data and the order of the listing will be
> different, depending on the values of population data entered from the
> keyboard by the User.
> I would appreciate any clues or ideas, this is what I have so far (as
> you can see I'm stuck on the sorting part)
> #include <vcl.h>

Non-standard header.

Quote:
> #include <stdio.h>
> #include <conio.h>

Non-standard header...

Quote:
> void main (void)

int main( void )

Quote:
> {  float chicago, boston,detroit,miami,lansing;
> printf("Please enter the population of Miami:  ");

fflush( stdout );

is needed here or the user may only see the question much later.

Quote:
> scanf("%d", &miami);

Since 'miami' is a float variable you need a '%f' format specifier.
And let's just pray that the user always enters a number or
scanf() might do unexpected things - it would be better to read in
the line using fgets() and then analyzing what you really got...

Quote:
> printf("Please enter the population of Boston:  ");
> scanf("%d", &boston);
> printf("Please enter the population of Lansing:  ");
> scanf("%d", &lansing);
> printf("Please enter the population of Detroit:  ");
> scanf("%d", &detroit);
> printf("Please enter the population of Chicago:  ");
> scanf("%d", &chicago);

I guess you could make things a lot simpler by using an array of
structures similar to this:

struct {
    const char *name;
        float inhabitants;

Quote:
} cities[  ] = { { "Miami"     0.0 },

                 { "Boston",   0.0 },
                 { "Lansing",  0.0 },
                 { "Detroit",  0.0 },
                 { "Chicago",  0.0 } };

Then you can use qsort() to sort the array of structures according
to the 'inhabitants' field of the structure (you just need to write
a very simple function for the comparison) and then print out the
result using a simple loop (also reading user input can then done
in a simple loop).
                                 Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring



Sun, 05 Jun 2005 19:08:09 GMT  
 Newbie needs help on simple problem

Quote:
Mike sk543 writes:
>I'm in an Intro to C course and I'm studying for a final exam, anyway
>this is the only sample problem that I'm having difficulty with. I'm
>having a bit of problem creating this program, specifically how would I
>sort the population column:

>Write a complete program that displays a city name, and requests the
>User to enter the population for that city.  This will be done five
>times, for the following cities, and in the following order:  Miami,
>Boston, Lansing, Detroit, and  Chicago.  The program will then display
>the cities and their population in order of decreasing population.  

<snip>

This takes a bit of mind reading. Your instructor expects you to

a.  write a bubble sort
b.  use qsort() in <stdlib.h>

Perhaps talking to classmates will help you figure out what he expects.  If he
described bubble sort in class or assigned reading that is almost certainly
what he wants.  

If he alluded to passing a function to a function he probably wants qsort.  

But regardless of the choice (a or b) here, you need an array of struct as a
starting point.  You don't have that in the code I snipped.  In short: start
over.  



Sun, 05 Jun 2005 21:02:30 GMT  
 Newbie needs help on simple problem

Quote:

> Hi,
> I'm in an Intro to C course and I'm studying for a final exam, anyway
> this is the only sample problem that I'm having difficulty with. I'm
> having a bit of problem creating this program, specifically how would I
> sort the population column:

[snip]

Quote:
> I would appreciate any clues or ideas, this is what I have so far (as
> you can see I'm stuck on the sorting part)

There are many diffrent sorting algorithms; do a search for 'bubble sort'
for a simple but slow one (but with only 5 cities it dosen't matter).

--
Replace spamtrap with bd to reply.
"Life is a garment we continuously alter, but which never seems to fit."
-- David McCord



Fri, 10 Jun 2005 08:59:25 GMT  
 Newbie needs help on simple problem

Quote:


>> Hi,
>> I'm in an Intro to C course and I'm studying for a final exam, anyway
>> this is the only sample problem that I'm having difficulty with. I'm
>> having a bit of problem creating this program, specifically how would I
>> sort the population column:

> [snip]

>> I would appreciate any clues or ideas, this is what I have so far (as
>> you can see I'm stuck on the sorting part)

> There are many diffrent sorting algorithms; do a search for 'bubble sort'
> for a simple but slow one (but with only 5 cities it dosen't matter).

True in a way, but the OP should beware: it doesn't take very much data
before bubble sort becomes almost the worst sort in existence.

If ever you are sorting more than half a dozen items, use a different sort,
such as insertion sort, shell sort, or (suitably modified) Quicksort.

Alternatively, trust your implementation and use qsort.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Fri, 10 Jun 2005 15:11:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie needs help with something simple!

2. Newbie needs urgent help, simple question

3. Simple problem across processes - need simplest answer

4. Need help on simple problem

5. need help with simple rand problem

6. Very Simple Problem - NEED HELP PLEASE

7. A Simple Problem, Need Help

8. (Newbie needs help: Pointer problem)

9. Newbie in need: Simple Application Design

10. NewBie in C needs help - C simple questions(not for me) help help help help!

11. Help C newbie need help please

12. Help C newbie need help please

 

 
Powered by phpBB® Forum Software