Re: uniqness in array
From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 10/03/04
- Next message: Val: ""Meatless MVC" seems success but new problem ahead."
- Previous message: White Wolf: "Re: C++ vs C executable file size"
- In reply to: Ronen Kfir: "Re: uniqness in array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 03 Oct 2004 20:42:33 +0200
Ronen Kfir wrote:
>
> "B. v Ingen Schenau" <bart@ingen.ddns.info> wrote in message news:<2s7cb6F1i4oapU1@uni-berlin.de>...
> > Ronen Kfir wrote:
> >
> > > I have ended up with some other algorythem, which works fine and it is
> > > implemented in the following code. When I run it in this program, the
> > > sum is always 2-3 lower then the real value, (though the whole array
> > > is initialized to 0).
> >
> > Because you specify the numbers with a leading 0, the compiler
> > interprets the numbers in octal (base 8) format, instead of decimal.
> > Try giving one of the cars a rental length of 019 or 009 and watch the
> > compiler complain.
> > The solution for this is to replace the leading 0-s with spaces.
> >
> > > Actually the previous question I sent was a simulation to part of a
> > > program I write which, eventually has to find the most rented car
> > > model, name & manufacturer out of rentals struct. In order to do that
> > > I want to sort array sum_rental_length, which summarize all rentals &
> > > take the indexes of highest value (if more then one- all of them) &
> > > print them out.
> > > The problem are:
> > > 1. The sorting algorithm, though I think it good, but for some reason
> > > array is not sorted.
> >
> > Take a good hard look at the continuation condition of the loops in your
> > sorting algorithm, and note the difference.
> >
> > > 2. How do take the model no. found in sorting action & find the model
> > > name & manufacturer?
> >
> > After you have fixed the sorting algorithm, you will find that even the
> > connection between model number and total rental time is broken. If you
> > think about how this connection was established and what happens during
> > sorting, you will know why.
> >
> > This is best solved by using a structure
> > struct total_rental_time {
> > int ModelNo;
> > int TotalTime;
> > };
> > An array of these structures can then be sorted on the field TotalTime.
> >
> > Once you have the correct model number, you can retrieve the rest of the
> > information about the car with a simple search.
> >
> > >
> > > TIA,
> > >
> > > Ronen
> > >
> > >
> > <snip - code>
> >
> > Bart v Ingen Schenau
>
> Didn't exactly understund the idea you posted.
> ModelNo veriable will host all model numbers?
No. Just one model number.
The proposed structure represents the connection between one model
and the time it was rented (summed up from all rentals). So if you
sort an array of those structures you will immediatly know what model
was rented the most time *and*, more important, which model was it.
As I said earlier. Do some simulations on paper first.
> what is the TotalTime? Did you mean asign it with result of sum of
> rentuls days (what I called sum_rental_length)?
Exactly. But knowing the total time won't help you very much, does it?
You also need to know which model accounted for that total time. Thus
the structure consists of
ModelNo
TotalTime
It is pretty much what I had in my original reply: The sum table consisted
of 2 entries per line: which model accounted for what time.
> Please remember I have to display the model name & manufaturer as well
> how would I do that?
No problem. Once you know the model number, you can use that number to search
the model table for it and from there you get the model name and the manufacturer.
In fact this is why the proposed structure consisted exactly of those 2 fields:
you need to time, because this is what you need to sort to get the highest.
you need the model number, becuase this is what you need to search for in the cars
array to get the model name for output
As said: use paper and pencil. It's not that hard.
You now have
all_rentals:
1111, north, 11, 20, 23
2222, south, 15, 25, 35
3333, center, 16, 23, 39
4444, north, 11, 25, 57
2222, center, 10, 23, 45
4444, center, 3, 20, 52
This rentals you summ up in a second array, which is an array of total_rental_time
all_times
1111 11
2222 25
3333 16
4444 11
You now sort this array according to the total rental time:
all_times
2222 25
3333 16
1111 11
4444 11
Thus you now know, that model 2222 was rented most. But what was 2222?
To figure this out, you use the array cars to search for it:
cars
1111, "Rols-Roys", "R-1", 10
2222, "Mercedes", "M-1", 20
3333, "Mercedes", "M-2", 57
4444, "Jajuar", "J-1", 103
and there you find: 2222 is the model number for "M-1", which is
manufactured by "Mercedes". And this is what you output.
Once you understand this on paper and can explain all the required
steps to your 7 year old sister and she can do it also by just
following your explanations in a 'cook book recipe' way, you are
ready to start programming. But not earlier!
-- Karl Heinz Buchegger kbuchegg@gascad.at
- Next message: Val: ""Meatless MVC" seems success but new problem ahead."
- Previous message: White Wolf: "Re: C++ vs C executable file size"
- In reply to: Ronen Kfir: "Re: uniqness in array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|