Thread: rand() and srand() question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    rand() and srand() question

    sorry if this post looks crappy, this is my first try. ok, I'm having trouble with an assignment in my programming class. here are the assignment instructions:

    "The purpose of this program is to find the two largest elements
    of each of several integer arrays containing 50 elements. The program will use
    functions with the prototypes:
    void two_largest(int[], int, int*, int*);
    void read_array(int[], int);
    void print_array(int[], int);
    The program is supposed to be just one do/while loop, which will be repeated
    until a number -1 is entered from a keyboard. On every step of this loop:
    1) use the function read_array to initialize a new array of integers
    (this function will initialize array elements to random numbers from 1 to 500
    with the help of functions rand() and srand(time(NULL)));
    2) use the function print_array to display the current array;
    3) call the function two_largest to find the two largest numbers in the array;
    4) display these numbers in main."

    I have the program pretty much finished except for two problems. I don't understand how to terminate the loop by entering -1 and I'm not exactly sure how to make the rand() and srand() loop I have into a do/while loop because we haven't covered the two functions in class. we were only given example programs to figure out how these two functions work. Here is my code for what I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define N 50
    
    void two_largest(int [], int, int *, int *);
    
    void read_array(int [], int);
    
    void print_array(int [], int);
    
    int main ()
    {
      int a[N], b, c, d, e;
      
      read_array(a, b); 
    
      print_array(a, b);
    
      two_largest(a, b, &c, &d);
    
    printf("The largest value is: %d\n", c);
    printf("The second largest value is: %d\n", d);
    
    return 0;
    }
    
    void read_array(int a[], int b)
    {
      srand(time(NULL));
      for(b=0; b<N; b++)
        a[b] = rand() % 500;
    }
    
    void print_array(int a[], int b)
    {
      for(b=0; b<N; b++)
        printf("%3d%c", a[b], b % 10 == 9 ? '\n' : ' ');
    }
    
    void two_largest(int a[], int b, int *x, int *y)
    {
      int i;
    
      *y=a[0];
      *x=a[0];
      for(b=0; b<N; b++)
        { if (a[b] > *x)
           *x = a[b];
         }  
      for(i=0; i<N; i++)   
       { if (a[i] > *y && a[i] < *x)
              *y=a[i]; }
               
    }
    just wondering if anyone had some suggestions? I'm not very good with this stuff since this is a beginning level class so please try to keep it simple.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    srand() should only be called once during the program entire execution, so having it buried in the read_array() function is the wrong place. Put it at the start of main(), is one suggestion.

    In order to stop on entry of -1, you'll need to actually get the input from the user. You can use fgets() to get the line, then compare against -1.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    The loop structure would be something like this concept:
    Code:
    do 
    {
      read_array
      print_array
      display_largest
    } while (AskUserIfWeWantToGoAgain())
    There are many ways to do this, have a play and see what you can come up with. Personally, I hate the syntax for do/while loops, and would avoid using them, but that's only a personal preference.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    Ok, thank you. I understand what you're telling me but how would I make it test so that some variable is not equal to -1? Would this work?

    Code:
    int main ()
    { 
      int a[N], b, c, d, e;
      
      srand(time(NULL))
    
      do {
      read_array(a, b); 
    
      print_array(a, b);
    
      two_largest(a, b, &c, &d);
    }
    while (
      { printf("Type any key to continue or type -1 to terminate program: ");
        scanf("%d\n", &e);
          if (e=-1)
             return 0;
    }
    also, I'm not familiar with 'fget()'. I don't think I can use any functions that we haven't learned.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    = is the assignment operator. Used to assign a value to a variable.
    == is the equality operator. Used to test to see if things are equal.

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

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    If you turn on your compiler warnings it would probably warn you.
    Code:
    itsme@itsme:~/C$ cat hw7.c
    int main(void)
    {
      int a;
    
      if(a = -1)
        ;
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ gcc -Wall hw7.c -o hw7
    hw7.c: In function `main':
    hw7.c:5: warning: suggest parentheses around assignment used as truth value
    itsme@itsme:~/C$
    It's not exactly clear because the compiler doesn't know exactly what you're trying to do, but at least it points out that there may be something not quite right with that line.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed