Re: uniqness in array

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 10/03/04


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


Relevant Pages

  • Re: Replace NaNs in mixed Cell Array
    ... dealt with developing a regression substitution model. ... easy to modify this to remove NANs from a dataset array. ... % This data set contains information regarding 406 different cars. ...
    (comp.soft-sys.matlab)
  • Re: What am I doing wrong?
    ... that problem seems to occur when I instantiate an array of type ... I have 17 cars that have specific properties like one has Cars.Wheels ...
    (microsoft.public.dotnet.general)
  • Re: object creation and naming at runtime?
    ... Such a container is ideally suited for the kind of mapping you ... wanting to the name the objects of the CARS class with an actual name. ... >After you have filled the map, you can treat is as an array that is ... The only limitations is the limitation ...
    (alt.comp.lang.learn.c-cpp)
  • listing from a database
    ... I have a database of cars and models, I didn't use two tables because I ... I wasn't sure if to query the ... distinct categories, then within that loop, query and use loops to print the ... array, then loop through that array using it to make seperate queries for ...
    (alt.php)
  • Re: Sorting a variant array
    ... Anyway, the point is tempList will become a 2D array, even if its one column ... move the Variant array into a String array which is what I thought might ... try and move them to a string array I get no debug output and ... Even if I don't try the movement to a String Array the sorting algorithm ...
    (microsoft.public.excel.programming)