How to solve this?

This is a discussion on How to solve this? within the C Programming forums, part of the General Programming Boards category; I'm get stuck at question 2 below. Got no idea! Anyone can give me some orientation? thanks A company wants ...

  1. #1
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12

    How to solve this?

    I'm get stuck at question 2 below. Got no idea! Anyone can give me some orientation? thanks


    A company wants to develop a program to manage their 12 cars
    data about each car includes:
    -Car's ID: it is a string which has 3 characters
    -Car's status: its value is:
    + 1 if the car is hiring by a customer
    + 0 if the car hasn't been hired yet
    -Customer's name: It is a string which contains full name of customer who is hiring the car.
    If the car hasn't been hired yet, the string shouldn't have a value


    Q1: Create a structure template that contains data of one car. Then use this template to
    declare an array of structures that stores data of all company's cars (12 cars)

    Q2: Create a function to assign the car's ID for 12 cars in the array of structures as the
    follows:
    - the first car in the list should have the ID : "C01"
    - the second car in the list should have the ID : "C02"
    ...
    - the last car in the list should have the ID : "C12"

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    What exactly do you have problem with? Did you try writting anything?

  3. #3
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    question 2: Create a function to assign the car's ID for 12 cars in the array.
    I don't know how do do this

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Have you done the first question?
    .
    Just GET it OFF out my mind!!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Forum policy on homework is that *you* need to post up what you've tried, in code, or pseudo code, and show or tell us, what the problem is.

    When you just say "I don't know", that's no help to us. We don't know why you don't know.

    We're not here to do your homework for you. If you're not willing to try, we're not willing to help (with few exceptions).

    So, post up what you have for your program, and tell us what's got you stumped.

  6. #6
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    What I don't know is just the algorithm, the way to do it. Because don't know the algorithm, I cannot write the code. Code for the Q1(I think it's easy, so didn't post it) :

    #include<stdio.h>
    typedef struct car
    {
    struct id;
    int status;
    char cus[20];
    }car;
    car list[12];
    [/code]

  7. #7
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    Finally find out the way I used nested structure

    Code:
    #include<stdio.h>
    #include<conio.h>
    void assign_id(void);
    struct id
           {
            char st;
            int nd;
            int rd;
           };
    typedef struct car 
    {
           struct id no;
           int status; 
           char cus[20];
    }car;
    car list[12];
    int main(void)
    {
        assign_id();
        getch();
        return 0;
    }
    
    void assign_id(void)
    {
         int i;
         for(i=0;i<9;i++)
          {
           list[i].no.st='C';
           list[i].no.nd=0;
           list[i].no.rd=i+1;
          }
         for(i=9;i<12;i++)
         {
          list[i].no.st='C';
          list[i].no.nd=1;
          list[i].no.rd=i-9;
         }
         for(i=0;i<12;i++)
         {
          printf("ID of car #%d: ",i+1);
          printf("%c%d%d\n",list[i].no.st,list[i].no.nd,list[i].no.rd);
         }
    }

    I tried to type char st='C';(instead of red words above) so that I would'nt have to assign values by using for loop (as you see in the assign_id function). But there were some errors & warning:

    [Warning] no semicolon at end of struct or union
    syntax error before '=' token
    syntax error before '}' token
    field `no' has incomplete type

    Did I forget any rules of declaring/assigning values?
    @Adak: Sorry for not posting anything. I just want to have some orientation, not the code (now I found it on my own, but is there any shorter way?) I'm a newbie and had no intention to break the law

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your struct id etc. is just defining a type; there are no variables created. You cannot initialize any of the fields of the variable until you actually define a variable.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by fenikkusu View Post
    Finally find out the way I used nested structure
    ...
    but is there any shorter way?
    IMHO you don't need a nested structure unless it's required of you and there's a shorter way by using a 4 char array as number of cars is limited to 12.
    Code:
    #include <stdio.h>
    
    void assign_id(void);
    
    typedef struct car 
    {
           char st[4];
           int status; 
           char cus[20];
    }car;
    
    car list[12];
    
    int main(void)
    {
        assign_id();
        return 0;
    }
    
    void assign_id(void)
    {
         int i;
    
         for (i=0; i<12; i++)
             snprintf(list[i].st,sizeof list[i].st,"C%02d",i+1);
    
         for (i=0; i<12; i++)
             printf("ID of car #%d: %s\n",i+1,list[i].st);
    }
    Last edited by itCbitC; 01-07-2009 at 02:19 PM.

  10. #10
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    Thank you all. But I have never heard about this function snprintf(list[i].st,sizeof list[i].st,"C%02d",i+1); Can you clarify it for me?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need nested structs, you don't need snprintf(), (whatever that is), which I would love to say is a special printf() function for printing out auto vin (serial) numbers, but you probably wouldn't laugh, anyway.

    You need an array of structs, with various fields in them, and that's most of what you need. I can't believe you know how to define a struct, but not how to actually create one.

    Post your most current code, and we'll see what's up with it. The sooner the better. Back in a bit.

    30 minutes, and no response. Despite the "online now" footer, maybe you aren't here, after all. Another time, perhaps.
    Last edited by Adak; 01-07-2009 at 08:34 PM.

  12. #12
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    I'm not sure I understand your idea.
    I just change snprintf into sprintf, and see that it also works (forgot that we have such a function to assign values to a string)
    Code:
    #include <stdio.h>
    
    void assign_id(void);
    
    typedef struct car 
    {
           char id[3];
           int status; 
           char cus[20];
    }car;
    
    car list[12];
    
    int main(void)
    {
        assign_id();
        getch();
        return 0;
    }
    
    void assign_id(void)
    {
         int i;
    
         for (i=0; i<12; i++)
             sprintf(list[i].id,"C%02d",i+1);
    
         for (i=0; i<12; i++)
             printf("ID of car #%d: %s\n",i+1,list[i].id);
    }

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My idea was to see whether you had things sorted out or not, and if not, to suggest a struct that met your needs, without the complexity of a nested struct.

    Looks like you're set.

  14. #14
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12
    OMG! You are like my teacher, often make me confused and nervous (because i'm not really good at this subject)

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fenikkusu View Post
    OMG! You are like my teacher, often make me confused and nervous (because i'm not really good at this subject)
    OMG! You are like my students, often make me confused and nervous (because i'm not really good at just what they need to be really good at this subject)

    j/k, I'm not a teacher.

    Your last post before mine was a question, so I didn't want to see your problem remain unresolved.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone solve this for me? (look inside)
    By johnsonswww in forum C Programming
    Replies: 10
    Last Post: 03-02-2009, 10:24 AM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. How to handle in software to solve this block diagram?
    By vnrabbit in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:45 PM
  4. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM

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