Thread: Random number syntax in a struct

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    5

    Random number syntax in a struct

    I can’t figure out the syntax on how to generate random numbers for two members of a struct. It seems so simple because I know what I want to do, I just can’t make it work. I have the random generators I want but I don’t know how to pull it over into my struct.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    struct student {
      int id;
      int score;
    };
    
    
    struct student *allocate()
    {
      /*Allocate memory for ten students */
      struct student *r = malloc(sizeof(struct student) * 10);
    
    
      /*return the pointer */
      return r;
    }
    
    
    void generate(struct student *students, int *rid, int *rscore)
    {
      /*Generate random ID and scores for ten students, ID being between 1 and 10, scores between 0 and 100 */
      *rid = rand() % 10 + 1;       //ids
      *rscore = rand() % 101;       //scores
    }
    
    
    void output(struct student *students)
    {
      /*Output information about the ten students in the format:
         ID1 Score1
         ID2 score2
         ID3 score3
         ...
         ID10 score10 */
      /*int rid, rscore;
         gernerate(students, &rid, &rscore);
         (*students).id = rid;   (*students).score = rscore;
         students->id = rid;
         students->score = rscore; */
    }
    
    
    void summary(struct student *students)
    {
      /*Compute and print the minimum, maximum and average scores of the ten students */
      output(students);
      //printf("%d", *students);
    
    }
    
    
    void deallocate(struct student *stud)
    {
      /*Deallocate memory from stud */
      free(stud);
    }
    
    
    int main()
    {
      struct student *stud = NULL;
    
      /*call allocate */
      allocate();
    
      /*call generate */
      //generate(stud, &rid, &rscore); //I am aware that these are also ........ty. How do I properly call them
    
      /*call output */
      output(stud);
    
      /*call summary */
      summary(stud);
    
      /*call deallocate */
      deallocate(stud);
    
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > COLOR=#000000 FONT=Helvetica #include <stdlib.h> /FONT /COLOR
    Please "paste as text" in future, so the board code formatter can make a decent job of formatting the code (which I just fixed for you).

    > /*call allocate */
    > allocate();
    Do you see the return statement on line 19
    Where do you think that result is going?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    How can I "return the pointer" ? I thought the malloc does something like this for me?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your function returns a pointer returned by malloc(). If you assigned what malloc() returned to a pointer, and you returned that pointer in kind, then what do you think you need to do with the return value of your function?

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    I took out return r and I get an error on line 20 "control reaches end of non-void function"
    Last edited by Sea711; 01-18-2014 at 10:30 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I took out return r and I get an error on line 20 "control reaches end of non-void function"
    You mean you didn't consider say
    stud = allocate();
    in main?

    Another question.

    If you had
    struct student students[10];
    in place of calling malloc, could you solve the rest of the problem?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  3. Strange struct syntax; type name:number
    By OnionKnight in forum C Programming
    Replies: 1
    Last Post: 11-19-2006, 08:23 PM
  4. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

Tags for this Thread