Thread: Data structure question.

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    Data structure question.

    This program is meant to find the model numbers in rental car company which where rented the highest number of days. I have one structre for all rentals records, one structure for all car stock records, and one sturcture to hold the results of the function & display them efficiantly. The problems I'm facing now are:
    1. When I asign the rentals structure to the total_rental_time structure (line 78- 79), I get "left operand must be l-value"
    2. When I sort the total_rental_time, the structure is sorted & I get unsorted results.
    BTW: I have posted another version of this code under a different title, but didn’t get to solve it all at that session…


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define TOTAL_RENTALS 10
    #define ALL_CARS      4
    
    typedef enum areas {north = 1, center, south} areas;
    
    typedef struct {
      int model;
      areas area;
      int rental_length;
      int day_pay;
      int age;
    } rentals;
    
    rentals all_rentals[TOTAL_RENTALS] =
    {
      { 1111, north,   11, 20, 21 },
      { 2222, south,   15, 25, 35 },
      { 3333, center, 106, 23, 63 },
      { 4444, north,   11, 25, 57 },
      { 2222, center,  10, 23, 45 },
      { 4444, center,   3, 20, 20 },
    };
    
    typedef struct{
      int model;
      char mnfctr[20];
      char model_name[15];
      int stock;
    } car;
    car cars[ALL_CARS] =
      {
        { 1111, "Rols-Roys", "R-1", 010 },
        { 2222, "Mercedes",  "M-1", 020 },
        { 3333, "Mercedes",  "M-2", 057 },
        { 4444, "Jajuar",    "J-1", 103 }
      };
    
    typedef struct  {
      int ModelNo;
      int TotalTime;
      char MnfctrNm[20];
      char ModelNm[15];
    }total_rental_time_strct;
    total_rental_time_strct total_rental_time[TOTAL_RENTALS] ;
    
    int process[TOTAL_RENTALS] = { 0 };
    void hi_rent_model(void);
    void title (void);
    
    int main(void)
    {
      hi_rent_model();
    
      
      return 0;
    }
    
    void hi_rent_model(void)
    {
      int i, j,temp;
    total_rental_time_strct temp_rent;
    
      for(i = 0;i < TOTAL_RENTALS;i++)
      {
        if(!process[i])
        {
          total_rental_time[i].TotalTime += all_rentals[i].rental_length;
          for(j = i+1;j < TOTAL_RENTALS;j++)
          {
            if(all_rentals[i].model == all_rentals[j].model)
            {
              total_rental_time[i].TotalTime += all_rentals[j].rental_length;
              process[j] = 1;
            }
    		total_rental_time[i].ModelNo = cars[i].model;
    		total_rental_time[i].MnfctrNm = cars[i].mnfctr;
    		total_rental_time[i].ModelNm = cars[i].model_name;
    
          }
        }
      }
    
    for  (i=0;i<TOTAL_RENTALS;++i)
    if(process[i]!=1)
    {
    for (j=i+1;j<TOTAL_RENTALS;++j)  
    {
    if(process[j]!=1)
    {
      if (total_rental_time[i].TotalTime < total_rental_time[j].TotalTime)
      {
       temp=total_rental_time[j].TotalTime;
       total_rental_time[j].TotalTime=total_rental_time[i].TotalTime;
       total_rental_time[i].TotalTime=temp;
    
       temp_rent=total_rental_time[i];
       total_rental_time[i]=total_rental_time[j];
       total_rental_time[j]=temp_rent;
      }
    }
    }
    
      
    }
      for(i = 0;i < TOTAL_RENTALS;i++)
      {
        if(total_rental_time[i].TotalTime != 0)
          printf("Model %d had a total of %d days\n", total_rental_time[i].ModelNo, total_rental_time[i].TotalTime);
      }
    
       title();
    printf ("\n %2d %17s %13s %8d\n ",cars[0].model, cars[0].model_name, cars[0].mnfctr, total_rental_time[i].TotalTime );
    }
    
    
    
    void title (void)
    {
    puts ("============================================================\n\r");
    puts ("model number | model name | manufacturer | total rental days\n\r");
    puts ("============================================================\n\r");
    }

    TIA,

    Ronen

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    1. shorten the names (of everything)
    2. don't name everything with close to the same name
    3. useCapitalLetters toSeperate words the make up the variable name... not_under_scores
    4. before you learn anything else, learn how to create functions

    line 78:
    total_rental_time[i].MnfctrNm = cars[i].mnfctr;

    you're trying to assign a an array to an array....doesn't work
    use
    #include <string.h>
    strcpy(total_rental_time[i].MnfctrNm, cars[i].mnfctr);

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    1. shorten the names (of everything)
    2. don't name everything with close to the same name
    3. useCapitalLetters toSeperate words the make up the variable name... not_under_scores
    4. before you learn anything else, learn how to create functions
    thanx for the tips.


    fixed lines 78 - 79.

    What do you think of my sorting issue question:

    2. When I sort the total_rental_time, the structure is sorted & I get unsorted results.
    Last edited by ronenk; 10-02-2004 at 01:55 PM.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    if i am right you re sorting in hi_rent_model
    first u r adding the rents of the models by all of its manufacturers
    storing them into a temporary struct varable
    then u r sorting them according to their total rental time

    correct me if i am wrong

    let me see it once again
    Syra
    Amateur's urge to master C/C++

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    U R correct. fixed that. sorting is working good.

    Now the feature of displaying more then one timr the results:
    wrote this code
    Code:
    for  (i=0;i<TOTAL_RENTALS;++i)
    	{
    		for (j=i+1;j<TOTAL_RENTALS;++j)  
    			if (total_rental_time[0].TotalTime==total_rental_time[i].TotalTime)
    	   printf ("\n %2d %17s %13s %8d\n ",total_rental_time[i].ModelNo, 
    	   total_rental_time[i].ModelNm, total_rental_time[i].MnfctrNm, total_rental_time[i].TotalTime );
    	}

    which print results TOTAL_RENTALS * times.
    How do I make it run only once for each record?

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    This is a reposting message, as the last post of aldajlo (big thanks, aldajlo!) has somewhat overlapped my message & my next question...

    if i am right you re sorting in hi_rent_model
    first u r adding the rents of the models by all of its manufacturers
    storing them into a temporary struct varable
    then u r sorting them according to their total rental time

    correct me if i am wrong
    U R correct. fixed that. sorting is working good.

    Now the feature of displaying more then one timr the results:
    wrote this code
    Code:

    for (i=0;i<TOTAL_RENTALS;++i)
    {
    for (j=i+1;j<TOTAL_RENTALS;++j)
    if (total_rental_time[0].TotalTime==total_rental_time[i].TotalTime)
    printf ("\n %2d %17s %13s %8d\n ",total_rental_time[i].ModelNo,
    total_rental_time[i].ModelNm, total_rental_time[i].MnfctrNm, total_rental_time[i].TotalTime );
    }


    which print results TOTAL_RENTALS * times.
    How do I make it run only once for each record?

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    i suppose that in yr code add a temp variable to store the result of previous comparision and compare it with the new result
    if it equals don't print it

    hope so i understood ur question !
    Syra
    Amateur's urge to master C/C++

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    I guess I wasn't clear enough. the output I get from this code:

    Code:
    for  (i=0;i<TOTAL_RENTALS;++i)
    	{
    		for (j=i+1;j<TOTAL_RENTALS;++j)  
    			if (total_rental_time[0].TotalTime==total_rental_time[i].TotalTime)
    	   printf ("\n %2d %17s %13s %8d\n ",total_rental_time[i].ModelNo, 
    	   total_rental_time[i].ModelNm, total_rental_time[i].MnfctrNm, total_rental_time[i].TotalTime );
    	}
    when I have two models (2222 & 3333) equal in total_rental_time[i].TotalTime

    is:

    model number | model name | manufacturer | total rental days

    ================================================== ==========


    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    2222 M-1 Mercedes 25

    3333 M-2 Mercedes 25

    3333 M-2 Mercedes 25

    3333 M-2 Mercedes 25

    3333 M-2 Mercedes 25

    3333 M-2 Mercedes 25

    3333 M-2 Mercedes 25

    3333 M-2 Mercedes 25

    3333 M-2 Mercedes 25

    How do I print each of them once?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Loop with the loop counter being the time. Then any model which has its rental time being the same as the loop counter, print it. Give a man an answer... Teach a man to think...

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    quzah, you are tough! I like it & I absolutely agree with your way... after reading your answer for at list 100 times & trying to implement it in code, didn't came to conclusion what I'm suppose to do... please give another clue.
    Thanx!
    Last edited by ronenk; 10-03-2004 at 12:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data structure for french-english dictionary
    By officedog in forum C Programming
    Replies: 9
    Last Post: 03-20-2009, 01:43 AM
  2. question regarding outputting data to adobe flash
    By hoax464 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2008, 01:08 PM
  3. data structure question
    By miami_victor in forum C++ Programming
    Replies: 13
    Last Post: 12-31-2004, 12:56 AM
  4. advice on a data structure
    By ventolin in forum C++ Programming
    Replies: 7
    Last Post: 03-23-2004, 10:34 PM
  5. Help require for some data structure topics
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 12-15-2002, 07:09 PM