Thread: I hate school...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    I hate school...

    can someone tell me why this doesnt generate anything but 0's.

    Code:
    void read()
    {
     for(suba = 0; suba < 4; suba++)
     {
      for(subb = 0; subb < 8; subb++)
      {
       x = rand()%51;
       if(x > 50 || x < 1)
       {
        num[suba][subb] = x;
        continue;
       }
      printf("%4d", num[suba][subb]);
      }
     printf("\n");
     }
    }
    there is an array declared above called num[4][8] as well as the other variables.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    x = rand()%51;
    if(x > 50 || x < 1)
    Because here your valid random ranges are 0 to 50. Since you prevent anything other than zero from making it past the if check, that's all you get.

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

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    Now all I get are lines and spaces where there used to be 0's...
    Code:
    if(x < 50 || x > 1)
    Last edited by escarrina; 04-02-2004 at 10:00 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What number are you trying to do here? You are doing one of two things in your loop:
    1) Putting the number 1 through 50 in an array. Then you continue, skipping printf.
    2) Calling printf if the number is a zero.

    Work through your code line by line in your head or on paper, or what not, and you'll see the problem. Try posting exactly what the program is supposed to do the next time you have a question also.

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

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    you if statement is the probelm it should read
    if(x < 50 && x > 1)

    when you want to have a number in a ceratain range you need to have and instead of or cause if the first part of your or condition is false the if statment will never execute, but if you use and then both condition have to be vaild.

    Hope this help you

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    Im not thinking at all anymore. I am so fed up with this calss I just want to drop itand take knitting or something... No one taught us to use rand() but they are telling us to use it with no reference to it other than rand(). I need a random number between 1 and 50 put into each place in the array. then it has tp print out each number from the array in 4 X 8 format (the array is [4][8])
    Code:
    #include <stdio.h>
    
    
    
    int num[4][8];
    int suba, subb;
    int ctr, ctr2, x;
    
    
    void eoj();
    void read();
    
    void main()
    {
     read();
    }
    
    void read()
    {
     for(suba = 0; suba < 4; suba++)
     {
      for(subb = 0; subb < 8; subb++)
      {
       x = rand()%51;
       if(x < 50 || x > 1)
       {
        num[suba][subb] = x;
        continue;
       }
      printf("%4d", num[suba][subb]);
      }
     printf("\n");
     }
    } 
    
    
    void eoj()
    {
     name();
     printf("EXECUTION COMPLETED... Press ENTER to contiune.\n");
     getchar();
    }
    Last edited by escarrina; 04-02-2004 at 10:23 PM.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    oh i forgot one more thing when you are using the rand function you have to do following

    at the top of your program put

    #include <time.h>

    and before you call the rand() put

    srand(time(NULL));

    this will help in generating random numbers, don't ask me why, but it works

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    one more thing i forgot, take out the continue, it is considered bad programming habit, and you should never use it cause you can write loops such as a for, while or do while that can control your program.

    Good Luck

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    There are comments throught out the following code that you might want to take note of - a lot of them are not to do with your immediate problem, but good to know, nonetheless.

    Code:
    #include <stdio.h>
    #include <stdlib.h> /* need this to use rand() */
    #include <time.h>   /* for srand() */
    
    int num[4][8];
    
    int suba = 0;
    int subb = 0;
    int ctr = 0;
    int ctr2 = 0;
    int x = 0;
    
    void eoj(void);   /* if function takes no parameters use void */ 
    void read(void);
    int getrand(int range);
    
    int main(void) /* main returns an int - always */ 
    {
        read();
        return 0;  /* should return status to the caller */ 
    }
    
    void read(void)
    {
    
        srand(time(NULL));
        for (suba = 0; suba < 4; suba++) {
    	for (subb = 0; subb < 8; subb++) {
    	    x = getrand(50); /*get random numbers, argument is to set range of numbers */
    	    num[suba][subb] = x;
    	....}
        }
    
    
        printf("\n");
    
    }
    
    
    void eoj(void)
    {
        name();
        printf("EXECUTION COMPLETED... Press ENTER to contiune.\n");
        getchar();
    }
    
     /* getrand will get you numbers between 0 and 50 */ 
    
    int getrand(int range)
    {
        return (int) ((double) rand() / ((double) RAND_MAX + 1) * range);
    }
    You might also try 'man 3 rand' if you are on a *nix computer.

    I will leave it up to you to figure out how to properly print all of the elements out.

    HTH

    ~/

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Re

    That did the trick. Thanks. Now I am stuck on how to get it to do a basic search of the array and print whether or not an integer entered is in the array. It prints every single time it loops, I only want one answer, that the number is in the array or it is not, and then the location (eg num[4][0]). Here is the code so far:
    Code:
    void diff()
    {
     char ans;
     int s;
     printf("\n\nSearch for a number in the array?");
     fflush(stdin);
     scanf("%c", &ans);
     if(ans == 'y' || ans == 'Y')
     {
      printf("\n\nKey in integer to be searched for:");
      fflush(stdin);
      scanf("%d", &s);
      for(suba = 0; suba < 4; suba++)
      {
       for(subb = 0; subb < 8; subb++)
       {
        if(num[suba][subb] == s)
        {
         printf("\n\n%d is in the array\n", s);
         break;
        }
        else
        {
         printf("\n\n%d is not in the array", s);
        }
        if(ans == 'N' || ans == 'n')
        {
         eoj();
        }
       }
      }
     }
    }

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    this will help in generating random numbers, don't ask me why, but it works
    The reason why you need this is because rand() will always return the same series of random numbers for a given seed. As such if you do not seed the random number generator (using srand() ) it will use the default value.

    edit: Forgot to add: what srand( (unsigned) time(NULL)); does it to get the current time in seconds and use that to seed the RNG. However this is not fool proof as if you run the program in the same second you will get the same results.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    re

    ok, that part is working now its the second half I do not get. I have to make a loop that will print if a certain number is in the array, now filled with the random numbers. And only print one answer, that it is not in there, or that it is in there at a certain location

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so loop through each element of the array, and check to see if it's there. If it is, stop there and do whatever it is you need to do. (Print the index, or value, or what not.) Then, if you're still having problems, consider starting a new thread on it. Also, post your attempt, and specificly what you're having trouble with.

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

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    18

    Re

    There is a new chunk of code just a couple of post above this one with the new function that I tried out. I just need to get it to break out of two loops instead of one.

  15. #15
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    You could break out of this loop by setting a flag then breaking as many loops as you like if this flag is set.

    You could do it with "goto"; to break out of just two nested loops this maybe considered poor form but the idea is worth considering as it is often the clearest cleanest method. Be aware though that most people shun 'goto' in C as it can lead to code that is very hard to follow.
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tabbed Windows with MDI?
    By willc0de4food in forum Windows Programming
    Replies: 25
    Last Post: 05-19-2005, 10:58 PM
  2. I Hate My School (rant!!!!)
    By Dalren in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-03-2003, 07:10 AM
  3. Pet Peeves
    By Srg Pepper in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 10-03-2002, 11:34 AM
  4. School Shooting in Germany
    By Golden Bunny in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 04-27-2002, 01:47 PM
  5. Question about going to a technical school
    By Goalie35 in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:34 AM