Thread: Need help understand the most basic use of structures, rand(), and functions, please.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Need help understand the most basic use of structures, rand(), and functions, please.

    Hello,

    Can someone please help me understand the most basic use of structures, rand(), and functions?

    I kind of know the basics, but I want to make sure I will be able to understand whats going on, on the upcoming final. So, ill upload code as I try to understand these these topics myself with questions, with hopes of getting some help

    this is an example of a function I just wrote, can someone please let me know if my code is complete or if there is anything else I should add?

    .... I always thought you needed to 'declare' any function in main to let the computer know that there is going to be a function, in order for it compile and run, however my program runs without it...


    Code:
    #include <stdio.h>
    
    
    int main () {
    
    	
    int x=3, y=5, b;
    
    b=f(x*2, y+1);
    
    printf("b= %d\ny=%d\n", b,y);
    
    
    system ("PAUSE");
    return 0;
    
    }
    
    
    
    f(a,b){
    
    a=b+5;  //=11
    b=2*a; //=22
    
    return a+b;	    
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    In my code below I am trying to generate a random number between 30 to 70 inclusive, did I do it right?

    This is how I figure out how to set it up... rand()%(range goes here)+(starting-point+1), is that the right format? also, what usually goes in these parens: rand--->()%100???


    Also, I every time I run my program, I get the SAME random number, does anyone know how to keep that from happening?

    THANKS

    Code:
    #include <stdio.h>
    
    int main () {
    
    int i;	
    
    i=rand()%40+31;
    
    printf("%d\n", i);
    
    
    system ("PAUSE");
    return 0;
    
    }
    Last edited by matthayzon89; 04-24-2010 at 08:39 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matthayzon89 View Post
    Hello,

    Can someone please help me understand the most basic use of structures, rand(), and functions?

    I kind of know the basics, but I want to make sure I will be able to understand whats going on, on the upcoming final. So, ill upload code as I try to understand these these topics myself with questions, with hopes of getting some help

    this is an example of a function I just wrote, can someone please let me know if my code is complete or if there is anything else I should add?

    .... I always thought you needed to 'declare' any function in main to let the computer know that there is going to be a function, in order for it compile and run, however my program runs without it...


    Code:
    #include <stdio.h>
    
    
    int main () {
    
    	
    int x=3, y=5, b;
    
    b=f(x*2, y+1);
    
    printf("b= %d\ny=%d\n", b,y);
    
    
    system ("PAUSE");
    return 0;
    
    }
    
    
    
    f(a,b){
    
    a=b+5;  //=11
    b=2*a; //=22
    
    return a+b;	    
    
    }
    The prototyping rules changed from C89 to C99: prototypes weren't required IF the return type was int and all the parameters were int. The definition starting "f(a,b)" is just barely acceptable in C89 by the same rules. The //, however, makes the program illegal in C89, but is accepted in C99.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matthayzon89 View Post
    In my code below I am trying to generate a random number between 30 to 70 inclusive, did I do it right?

    This is how I figure out how to set it up... rand()%(range goes here)+(starting-point+1), is that the right format? also, what usually goes in these parens: rand--->()%100???


    Also, I every time I run my program, I get a different random number, does anyone know how to keep that from happening?

    THANKS

    Code:
    #include <stdio.h>
    
    int main () {
    
    int i;	
    
    i=rand()%40+31;
    
    printf("%d\n", i);
    
    
    system ("PAUSE");
    return 0;
    
    }
    If you had bothered to read the manual/your textbook on rand, you would see it is declared as
    Code:
    int rand(void);
    meaning that you should never ever ever ever have anything between the parentheses when calling rand (that's what "void" means). Of course, you can't call rand unless you do "#include <stdlib.h>" somewhere in your file. If you had ever bothered to learn how to divide, you would know what the possible values you could get from %40 are (0--39), and what adding 31 will do to each of those values. And we won't believe that you get different values each time. We will believe you if you say instead that you get the same number each time.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Structures are used to hold data relating to a central idea, ie several variables/data types together to make an "object".

    Here is a quick/simple example that shows how to use a structure and how it could be useful for dealing with data that relates to each other:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct employee {
        char * name;
        char * department;
        double salary;
        struct employee * boss; /* pointer to this employees boss */
    };
    
    struct employee * newEmployee(char *,char *,double);
    void removeEmployee(struct employee *);
    void setEmployeeBoss(struct employee *,struct employee *);
    void printEmployee(struct employee *);
    
    int main(void)
    {
        struct employee * stan;
        struct employee * BossMan;
    
        stan = newEmployee("Stan the Man","Mailroom",50);
        BossMan = newEmployee("Mr. Boss","Executive",300);
        setEmployeeBoss(stan,BossMan);
    
        printEmployee(stan);
        putchar('\n');
        printEmployee(BossMan);
    
        /* clean up */
        removeEmployee(BossMan);
        removeEmployee(stan);
    
        return 0;
    }
    
    /* allocate and return pointer to a new employee */
    struct employee * newEmployee(char * name, char * dept, double sal)
    {
        struct employee * emp = NULL;
    
        if (emp = malloc(sizeof *emp)) {
            if (name && dept) {
                emp->name = malloc(strlen(name)+1);
                emp->department = malloc(strlen(dept)+1);
                if (emp->name) strcpy(emp->name,name);
                if (emp->department) strcpy(emp->department,dept);
                emp->salary = sal;
                emp->boss = NULL;
            }
        }
    
        return emp;
    }
    
    /* deallocate memory pointed to by an employee pointer */
    void removeEmployee(struct employee * emp)
    {
        if (emp) {
            free(emp->name);
            free(emp->department);
            free(emp);
        }
    }
    /* set the employee boss pointer */
    void setEmployeeBoss(struct employee * emp, struct employee * boss)
    {
        /* allows setting boss to NULL */
        if (emp)
            emp->boss = boss;
    }
    
    /* print out contents of employee struct */
    void printEmployee(struct employee * emp)
    {
        if (emp)
            printf("%-15s%s\n%-15s%s\n%-15s$%.2lf/hr\n",
                    "Name:",emp->name,"Department:",emp->department,
                    "Salary:",emp->salary);
        if (emp->boss)
            printf("%-15s%s\n","Boss:",emp->boss->name);
    }
    Of course you dont necessarily have to use seperate functions to access the data within the struct, you can access it directly once you have a pointer to the struct. However the whole idea of "data hiding" is lost if you allow direct access to the struct.

    This way users can you use your struct "interface" without knowing whats actually in the structure.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    thanks nonpuz, your post is very useful, and helps my understanding of structures.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by tabstop View Post
    The prototyping rules changed from C89 to C99: prototypes weren't required IF the return type was int and all the parameters were int. The definition starting "f(a,b)" is just barely acceptable in C89 by the same rules. The //, however, makes the program illegal in C89, but is accepted in C99.
    this really isn't that helpful at all, thanks for the forced effort though. I do read and learn from my text book and I was hoping to get extra help from the forums, so if everytime you help you are going to come off as if you were annoyed by my questions and you are going to refer me to my textbook, please don't bother.
    Last edited by matthayzon89; 04-24-2010 at 08:51 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by matthayzon89 View Post
    this really isn't that helpful at all, thanks for the forced effort though. I do read and learn from my text book and I was hoping to get extra help from the forums, so if everytime you help you are going to come off as if you were annoyed by my questions and you are going to refer me to my textbook, please don't bother.
    If you didn't want to know about prototypes, then why did you ask the question "I always thought you needed to 'declare' any function in main to let the computer know that there is going to be a function, in order for it compile and run, however my program runs without it.."

    As to the rest, my goal here is to disabuse you of the notion that "there is no such thing as a stupid question". A question that can be answered by looking the keyword of the question up in the index of your textbook is most definitely a stupid question. If you are, as you say you are, interested in learning and not just passing the test, then recognizing the things you are supposed to be able to do by yourself is a very important step in that process.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Can someone please help me understand how rand()% works, please?

    One simple example of rand() is sufficient. Say from 40-55.

    Thanks in advance.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matthayzon89
    Can someone please help me understand how rand()% works, please?

    One simple example of rand() is sufficient. Say from 40-55.
    Do you understand what the % operator does?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Yes for example if I wrote

    rand()%50 then it would give me a random number between 0-49, right?

    So, if I wanted my range to start from 30 for example and I wanted random numbers from 30-80, thats where I get confused.

    rand()%50+31, is this right?

    Can you tell me if my logic is correct?

    rand()%50<----this is the range of random numbers and -->+31 this is where my random numbers are going to start from (0+31=30 b/c 0 is included in the count) and thats how I get random numbers from 30-80?
    Last edited by matthayzon89; 04-25-2010 at 10:18 AM.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    % is modulo, that is, remainder when divided.

    Assuming that rand() % 50 gave you 0 (lower limit of 0-49), what would you plus to get it to the lower limit of 30-80?

    (hint: To get to 1 from 0, you add 1)

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you really trying to claim that 0 + 31 = 30?

    How many numbers are there in the range 30 to 80? (HINT: NOT 50.)

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    It is 49.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by DeadPlanet View Post
    % is modulo, that is, remainder when divided.

    Assuming that rand() % 50 gave you 0 (lower limit of 0-49), what would you plus to get it to the lower limit of 30-80?

    (hint: To get to 1 from 0, you add 1)

    rand()%50+31? Like I said my first attempt, right?

Popular pages Recent additions subscribe to a feed