Car assignment help

This is a discussion on Car assignment help within the C Programming forums, part of the General Programming Boards category; I am a new user to this forum so i need help with code. I need help on how to ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Car assignment help

    I am a new user to this forum so i need help with code.

    I need help on how to use call by reference or pointer as mentioned in Assignment question paper see attached file. There are 2 files first part and second part. I have made full coding work and i have missed some part which is Pointer. Should i show you all my coding work or some. Do we allow to show in forum?
    Attached Images Attached Images   
    Last edited by anik18; 05-09-2009 at 10:28 PM.

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Basically, when calling a function that requires (a) parameter(s) in C, you have two options; you can pass by value, or by reference.

    When passing by value, the value of the parameter you are caling the function with, will be copied, and will NOT be the same reference.

    Here's code for a small illustration:
    Code:
    void add(int);
    
    int main(int argc, char **argv)
    {
    
        int i = 2;
        add(i);
    
        return 0;
    
    }
    
    void add(int a)
    {
        a += 5;
    }
    In main, after calling the function add(int), passing the local variable 'i' as parameter, 'i's value will not be modified whatsoever, because we are passing 'i' by value, and not by reference; so, therefore, the value of 'i' is copied into the function.

    When passing by reference, the variable passed as a parameter will be modified according to the function.

    Example:
    Code:
    void add(int *);
    
    int main(int argc, char **argv)
    {
    
        int i = 2;
        add(&i);
    
        return 0;
    
    }
    
    void add(int *a)
    {
        (*a) += 5;
    }
    In this application, after calling the add(int*) function, passing 'i' as parameter, the value of 'i' will be changed. '5' will be added to the variable passed in the function (i) and since we are using pass by reference, it will modify the passed argument directly; in this case '1'.
    Last edited by abraham2119; 05-09-2009 at 09:37 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    16
    Thanks Abraham. If i want to add call by reference codes then where in my full coding work above i should put in. Or you can copy my code and add with yours then i will identify and understand it.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by anik18 View Post
    Thanks Abraham. If i want to add call by reference codes then where in my full coding work above i should put in. Or you can copy my code and add with yours then i will identify and understand it.
    That is your job, not mine.

    I gave you a brief explanation on what you asked for, don't push it.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    16
    Is that right? I don't know what is the address operator for pointer. Can you please help now?

    Code:
    #include<stdio.h>
    #include<conio.h>
    using namespace std;
    struct date{
      int day;
      int month;
      int year;
    };
    struct car{
      char make[10];
      struct date man;
      struct date pur;
      double price;
    }fleet[10];
    car addcar();
    date getdate();
    void readfleet(struct car[],int*);
    void showfleet(struct car[],int);
    void showcar(struct car);
    void showdate(struct date);
    void savefleet(struct car[],int);
    bool validate(int,int,int);
    int menu();
    int main()
    {bool done=false;
    int choice,count=0,max=10;
    do
    {
    choice=menu();
    switch(choice)
     {case 1:if(count==10)
                printf("Sorry fleet full\n\n");
             else
                {
                fleet[count]= addcar();
                count++;
                 }
             break;
      case 2:if(count<=0)
                printf("Sorry fleet already empty\n\n");
             else
                count--;
             break;
      case 3:showfleet(fleet,count);
             break;
      case 4:savefleet(fleet,count);
             break;
      case 5: readfleet(fleet,&count);
              break;
      case 6:done=true;
             break;
      }
    }while(!done);
    
    return 0;
    }
    car addcar( )
    {car f;
    printf("Enter car make: ");
    scanf("%s",&f.make);
    printf("Enter manufacture date\n");
    f.man=getdate();
    printf("Enter purchase date\n");
    f.pur=getdate();
    do
    {printf("Enter purchase price: ");
    scanf("%lf",&f.price);
    if(f.price<12000||f.price>50000);
        printf("Price out of range (12000-50000)\n");
    }while(f.price<12000||f.price>50000);
    printf("\n\n");
    return f;
    }
    date getdate()
    {int m,d,y;
    bool val;
    date dd;
    printf("Enter year: ");
    scanf("%d",&y);
    do
      {printf("Enter month: ");
       scanf("%d",&m);
       if(m<1||m>12)
          printf("Illegal entry ");
      }while(m<1||m>12);
    do
      {printf("Enter day: ");
      scanf("%d",&d);
      val=validate(d,m,y);
      if(!val)
         printf("Illegal entry \n");
      }while(!val);
    dd.month=m;
    dd.day=d;
    dd.year=y;
    return dd;
    }
    bool validate(int d,int m, int y)
    {int leap=0;
    int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
      if(y%400==0)
         leap=1;
      else if(y%4==0)
         if(y%100!=0)
            leap=1;
    if(leap==1)
        days[1]++;
    if(d>days[m-1]||d<1)
       return false;
    return true;
    }
    void readfleet(struct car f[],int*count)
    {FILE *in;
    int i;
    in = fopen("cars.txt","r");
    if(in==NULL)
    {printf("Error opening file for input!\n");
            return;
            }
    for(i=0;i<10;i++)
    {fscanf(in,"%s %d %d %d %d %d %d %lf",
                &f[i].make,&f[i].man.day,&f[i].man.month,&f[i].man.year,&f[i].pur.day,&f[i].pur.month,&f[i].pur.year,&f[i].price);        
    if(feof(in))
         break;
    }
    fclose(in);
    *count=i;
    printf("\n\n");   
    return;
    }
    void showfleet(struct car f[],int count)
    {int i;
     printf("           manufactured      purchased      purchase\n");
     printf("  make    day month year   day month year    price\n");
     for(i=0;i<count;i++)
        showcar(fleet[i]);
       printf("\n\n");
    return;
    }
    void showcar(car f)
    {printf("%7s   ",f.make);
    showdate(f.man);
    showdate(f.pur);
    printf("$%8.2lf\n", f.price);
    return;
    }
    void showdate(date d)
    {printf("%2d  %2d    %2d   ",d.day,d.month,d.year);     
    return;
    }
    void savefleet(struct car f[],int count)
    {int i;
    FILE *out;
    out = fopen("cars.txt","w");
    if(out==NULL)
    {printf("Error opening file for output!\n");
            return;
            }
    for(i=0;i<count;i++)
        fprintf(out,"%s %d %d %d %d %d %d %lf\n",
                f[i].make,f[i].man.day,f[i].man.month,f[i].man.year,f[i].pur.day,f[i].pur.month,f[i].pur.year,f[i].price);        
    fclose(out);
    printf("\n\n");   
    return;
    }
    void add(int);
    
    int main(int argc, char **argv)
    {
    
        int i = 2;
        add(i);
    
        return 0;
    
    }
    
    void add(int a)
    {
        a += 5;
    }
    int menu()
    {int choice;
     do
     {printf("1. add a car\n");
      printf("2. delete last car\n");
      printf("3. display the fleet on the screen\n");
      printf("4. save the fleet to a file\n");
      printf("5. read the fleet from a file\n");
      printf("6. exit program\n");
      scanf("%d",&choice);
      if(choice<1||choice>6)
         printf("Illegal entry - try again\n\n");
      }while(choice<1||choice>6);
      return choice;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. OpenGL coordinates
    By Da-Nuka in forum Game Programming
    Replies: 5
    Last Post: 01-10-2005, 11:26 AM
  4. i really need help here
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-09-2002, 11:47 PM
  5. I need help with an algorithm
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-07-2002, 08:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21