Thread: Random numbers

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    Random numbers

    I couldnt run this program. Please help me out.
    Question:
    generate a random numbers in a given range
    count the occurrences of each number and return the total in arry 'a'

    find the maximum integer in the arry 'a' of size 's'
    find the minimum integer in the arry 'a' of size 's'

    and using function to find max and min

    Please view attachemnt for my program.

    thanks.
    NiCoLeHa

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What are these?

    Code:
    #include <DBOS\LIB.h>
    #include <CLIB.H>
    I've never heard of either of these. I also see that you're using the rand() function without time.h, so if one of these has a rand() function, I'd suggest using a more standard library. You're also passing an unitialized variable "s" to your functions. I can't really say more cause I can't compile this without your libraries. I'll make some changes to work with time.h, and I'll assume that date_time_seed() is similar to srand().
    Sent from my iPad®

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    #include <DBOS\LIB.h>
    #include <CLIB.H>

    I was told to use those. I am compiling using Salford plato.
    NiCoLeHa

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    Please help!!

    Please help me check my prgram. I dont seem to run it.

    Please view attachment.

    Many thanks.
    NiCoLeHa

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Please don't bump your thread with almost the exact same code. As I tried to imply, there isn't much anyone can do without those non-standard libraries, so you could either include them in an attachment or explain what the functions in them do.
    Sent from my iPad®

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do people insist on attaching their .c files instead of just pasting the code in?


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

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    your programe logic is not correct, u are checking the array for integers with no values in the array where u get the undefined values.

    what this variable S is doing there. u sending the variable S to the function with no value in it again. which again ends up with the undefined result

    ssharish2005

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why do people insist on attaching their .c files instead of just pasting the code in?
    Dunno, maybe they haven't figured out code tags, or because it's an unsightly mess. People just make it too hard on themselves by writing code in such an undisciplined way.

    Here's the code - formatted by muggins here - with some comments as to what is going wrong
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    /*#include <DBOS\LIB.h>*/
    /*#include <CLIB.H>*/
    
    void count_nums(int *a, int range, int num);
    int my_rand(int);
    int get_max(int *a, int s);
    int get_min(int *a, int s);
    
    int main(void)
    {
        int range, num;
        /*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */
        date_time_seed();
        /*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */
        int a[50];
        char c;
        int s, i;
    
        printf(" * Please press r/R to run or press q/Q to exit *\n");
        /*!! foo.c:20: warning: implicit declaration of function ‘getch’ */
        c = getch();
        /*run by pressing r and exit by pressing q */
    
        if (c == 'r' || c == 'R') { /*accept both lower and upper case */
            /*move to next statment only if the input in range 1 to 20 */
            do {
                printf("Enter the range size(max=20)");
                scanf("%d", &range);
            }
            while (range < 0 || range > 20);
    
            printf("How many numbers do you wish to generate?");
            scanf("%d", &num);
            count_nums(a, range, num);
            for (i = 0; i < range; i++)
                printf("%d:%d\n", i, a[i]);
            printf("Maximum count:%d\n", get_max(a, range));
            /*!! foo.c:37: warning: ‘s’ is used uninitialized in this function */
            printf("Minimum count:%d\n", get_min(a, s));
            printf("Expected: %d\n", (int) (num / range));
        }
        if (c == 'q' || c == 'Q')
            exit(1);
    
        return (0);
    }
    
    int my_rand(int range)
    {
        int r;
        date_time_seed();
        r = 1 + rand() % range;
        return r;
    }
    
    void count_nums(int *a, int range, int num)
    {
        int i;
        for (i = 0; i <= num; i++);
        {
            num = my_rand(range);
            *(a + num) = 0;
        }
    }
    
    /* function to find the max */
    int get_max(int *a, int s)
    {
        int i, max, num;    /*!! foo.c:67: warning: ‘max’ may be used uninitialized in this function */
        num == s;         /*!! foo.c:68: warning: statement with no effect */
        for (i = 0; i < s; i++)
            if (*(a + i) > max)
                max = *(a + i);
        return (max);
    }
    
    /*function to find the min */
    int get_min(int *a, int s)
    {
        int i, min;
    
        min = *a;
        for (i = 0; i < s; i++)
            if (*(a + i) < min)
                min = *(a + i);
        return (min);
    }
    I'm guessing date_time_seed(); is one of those magic functions in your schools library code - but in any event you need to only call it once (and never in my_rand).
    See the FAQ - reseeding the rand() generator just makes it return a constant.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    salem,
    thanks for ur help. I tried to recompile the program. However, it still couldnt work. Can someone please help me out?

    Thanks.
    NiCoLeHa

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well did you fix the warnings about uninitialised variables?

    > it still couldnt work
    Be more specific.
    Does it compile?
    Does it run, or crash
    Does it produce the expected output, how does that differ from what you expect?

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    Yea,
    I did fix those variables. However it still cannot compile. The first two print parts did what it should do. But not after that. It doesnt produce the expected output. =(
    NiCoLeHa

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You did put these back in right?
    /*#include <DBOS\LIB.h>*/
    /*#include <CLIB.H>*/

    Also, paste actual error messages.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    Yes i did. What was wrog was that, they said
    1) cannot compile.......- array on line 45 which is something inside the function void count-nums,
    2) date_time_seed- i removed as u said, but warming regarding that was detected.
    3) in int get_max, ive initialised the variables but they still say my varaibles are undefined. Can u please show me how to go about getting the max. ?

    Thanks.
    NiCoLeHa

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 1) cannot compile.......- array on line 45 which is something inside the function void count-nums,
    When I said paste, I meant paste directly what the compiler tells you, not what you think it said.

    That said, there's another problem I just spotted in count_nums
    > for (i = 0; i <= num; i++);
    The ; at the end means the loop does nothing, except count.
    Then when it exits, you do ONE rand() and assign it to some location past the end of your array.

    > 3) in int get_max, ive initialised the variables
    It should look exactly like get_min, except for the obvious swap of a < for a >

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    error 313- function date_time_seed has not been declared( perhaps u should #include <DBOS\LIB.h>
    error 313_function getch() has not been declared(perhaps u should #include <CLIB.h>)
    Please help. Is my function under viod_count_nums correct?
    NiCoLeHa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM