Thread: Could someone help me with randnum()?

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    39

    Could someone help me with randnum()?

    I'm in an intro to C class and my task is to print 100 random integers between the interval [0,1].

    I know how to do the above part, but the second part is what I don't understand.

    Using an array, I need to compute the total number of times 1 appeared as a random number.

    Here's what I have so far:Could someone help me with randnum()?-yee-png

    I don't know how to use hashtags to post code, I'm new here.

    Thank you for your help!

  2. #2
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Our professor really hasn't gone over arrays with us. The program I have so far generates 100 random numbers between [0,1] -- so each number is either a 1 or a 0.

    The part I'm stuck on is printing how many times the integer 1 appears in the 100 numbers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    New maybe, but most people seem to figure out this, rather than make excuses.
    Posting code? Read this first!

    > The part I'm stuck on is printing how many times the integer 1 appears in the 100 numbers.
    You add a counter, and an if statement.

    Or since it's only ever 0 or 1, simply adding together all your rands works too.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Salem,

    Thank you for the response! I will look at the code tags and get better at posting for help.
    How do I go about adding up the random numbers calculated by the while loop?

    Sorry, I'm not very familiar with C.

    Thanks for the help!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you managed t = t - 1;

    Surely you can manage to add another variable and throw in +1
    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.

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    The trouble is, I'm only allowed two int variables and one int array. I'm almost positive I need an array, but I'm really unfamiliar how to use it with rand numbers.

    I'm supposed to store them in an array and calculate how many times 1 appears.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well an it array looks like
    Code:
    int myRandomNumberArray[100] = { 0 };
    If you started t at 99, and used >= 0 as the test, you could use it as a subscript.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    I've declared the array and changed the while conditions and started t at 99. How do I go about associating an array with the loop to calculate the number of ones?

    Here's what i have so far: (Not sure if I did code tags right)


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    int randNum;
    int myRandomNumberArray[100] = { 0 }; 
    int t = 99	;
    srand((int)time(0)); // Seeds Random Numbers
    
    
    
    
    	while(t >= 0) // Will iterate the conditions until counter hits 0.
    	{
    		randNum = rand()%2; // Finds random numbers on interval [0,1]
    		printf("\n Random Number is %d", randNum); // Will display Randon Number found.
    		t = t-1; // Counter for while condition
    	}
    	
    	
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by csmith03
    How do I go about associating an array with the loop to calculate the number of ones?
    Write a second loop, a for loop preferably, that loops over the array element by element. On each iteration of the loop, it checks if the current element has the value of 1, and if so, it increments a counter that was set to 0 before the loop.

    Note that if not for the requirement to use an array, you could do this counting within the while loop that generates the pseudorandom numbers. Of course, you can still do this within that while loop, but it defeats the purpose of the array, so it is likely not the expected approach.
    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

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    if statement to check its value
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
     
     
    int main(void)
    {
    int randNum;
    int myRandomNumberArray[100] = { 0 }; 
    int t = 99  ;
    
    //srand((int)time(0)); // Seeds Random Numbers
     
      /* rand() plays on its own */
     
     
        while(t >= 0) // Will iterate the conditions until counter hits 0.
        {
            randNum = rand()%2; // Finds random numbers on interval [0,1]
            if (randNum == 1)
            printf("\n #element %d Random Number is %d",t, randNum); // Will display Randon Number found.
            t--; // = t-1; // Counter for while condition
        }
         
         
    }
    simple as that. cheers! where does that Array come into play?
    Last edited by userxbw; 10-15-2017 at 04:22 PM.

  11. #11
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    I'm limited on my knowledge of for loops, but I created this. I'm not exactly sure how to connect the array and the randNum variable. Also, how do I print the number of times one appears out of the 100 random integers?

    Thanks for the help.

    Code:
    for(t=0; t<=99; t++)
    	{
    		myRandomNumberArray[100] = rand()%2;
    
    
    	}
    		printf("\nNumber one occurs %d times\n", myRandomNumberArray[t]);

  12. #12
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    I tried that code, it just gave me an infinite loop where the random number was one every time. My task was to generate 100 random numbers on the interval [0,1] (so basically just 0 or 1) and print how many times the number one appears.

    Thanks for the help!

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by csmith03 View Post
    I tried that code, it just gave me an infinite loop where the random number was one every time. My task was to generate 100 random numbers on the interval [0,1] (so basically just 0 or 1) and print how many times the number one appears.

    Thanks for the help!
    you got your length 0 ... 99;
    Code:
    array[ number to indicate which element you want the value set inside of it ] = value;
    use your counter to set the element = value
    Code:
    // seeings how you set t = 99, to counter act that you could do this
    int t = 99 ,g = 0;
     
     
    for ( t = 99; t > 0; t--)
    {
      myRandomNumberArray[g++] = (rand()%2);
    }
    or just start high ( 99 ) like you got it set and work your way down to zero 0
    Code:
    int t = 99;
     
     
    for ( t = 99; t > 0; t--)
    {
      myRandomNumberArray[t] = (rand()%2);
    }
    but does that actually get you to 100 using 99?

    run it two ways to see how this effects your count:
    Code:
    for ( t = 99; t > 0; t--)
     array[ t ] = t;
    
    // vs
    for ( t = 99; t >= 0; t--)
     array[ t ] = t;
    then loop out its value to check your element count.
    Code:
    for ( t = 0; t < sizeof(array)/sizeof(array[0]); t++)
    {
     
           printf("t = %d number element is %d\n",t, array[t]); 
     
    }
    Last edited by userxbw; 10-15-2017 at 04:53 PM.

  14. #14
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Thank you, I understand the for statement a little better now.

    Right now, it's printing the 100 random integers, then the second print statement is printing "Number one occurs one time", but due to the count it's iterating the print statement 100 times. Is there a way to take the number of times one occurs and print that number in ONE print statement? If that makes sense.

    Thanks for the help by the way, I'm new to C and am really just trying to familiarize myself.



    Here's my entire code so far:
    Code:
    int randNum;
    int myRandomNumberArray[100] = { 0 }; 
    int t = 99, g = 0	;
    srand((int)time(0)); // Seeds Random Numbers
    
    
    
    
    	while(t >= 0) // Will iterate the conditions until counter hits 0.
    	{
    		randNum = rand()%2;		// Finds random numbers on interval [0,1]
    		printf("\n Random Number is %d", randNum); // Will display Randon Number found.
    		t = t-1; // Counter for while condition
    	}
    	
    	for(t=99; t>0; t--)
    	{
    		myRandomNumberArray[g++] = (rand()%2);
    		printf("\nNumber one occurs %d times\n", myRandomNumberArray[g++]);
    
    
    	}

  15. #15
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    I'm not sure what that means I tried that code and it just printed "t = 1, element is 1". I need the print statement to print "The number of times the integer one occurs from your random numbers is %d"... I just don't know how to associate the array with the 100 random integers on the interval [0,1] and find how many times 1 occurs.

Popular pages Recent additions subscribe to a feed

Tags for this Thread