I'm a noob and I need help.

This is a discussion on I'm a noob and I need help. within the C Programming forums, part of the General Programming Boards category; Hi everyone. Before you read this thread, just warning you, you guys might get frustrated because I'm just that bad ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    I'm a noob and I need help.

    Hi everyone. Before you read this thread, just warning you, you guys might get frustrated because I'm just that bad at programming, so pardon me if I don't really understand whats going on.


    What I'm trying just to trying to do some basic stuff, like looping numbers and stuff, so far I got down to rand int's and what I'm trying to do is loop random numbers from 0-1000 (including 0 and 1000). And for every even number, i want it to say even and for every odd number I want it to say odd, and the only way the loop will stop is if the number is divisible by 10 evenly, so like 10, 20, 30, etc.


    so far this is what I got,
    ---------------------------------------------------------
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    int x, p;
    
    srand(time(NULL));
    x = rand() &#37; 1001;  
    
    // please tell me if this is the right function i should be using for my rand in order for it to be limited from 0 to 1000
    
    
    while (p != (x / 10) == 0)
    {
    if(x / 2 == 0)
    {printf("The number %d is even.\n", p);
    }
    else
    printf("The number %d is odd.\n", p);
    }
    ----------------------------------------------
    And then i have no idea what to do from here.... I don't even know if this is correct, I wrote a flowchart saying that I want and how i want to do it, but its pretty simple for you guys i bet. Sorry that I'm a noob, its just that I really wwant to get into programming because I wanted to program ever since I was 12. Thanks.

    -Yi

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Indent...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
      int x, p;
    
      srand(time(NULL));
      x = rand() &#37; 1001;  
    
      // please tell me if this is the right function i should be using for my rand in order for it to be  limited from 0 to 1000
    
    
      while (p != (x / 10) == 0) /* Wow... Letting the compiler decide how to handle this one? Brave guy */
        if(x / 2)
          printf("The number %d is even.\n", p);
        else
          printf("The number %d is odd.\n", p);
    }
    p != (x / 10) == 0

    That is a problem. It won't do what you want it to do since you are combining two conditions.

    It should process (x/10) == 0, first. Then check to see if that is equal to 10. Thus it will only handle something that is a power of 10. Which oddly sounds like the problem you are having.
    Last edited by master5001; 10-13-2008 at 05:24 PM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Thanks for helping, I'll give it a try. I'll report as soon as possible and let you guys know the results.

    Again, thanks for responding really fast.

    -Yi

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Never before have I been commended for not doing my homework. Thanks, nifear4.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by C_ntua View Post
    Amusing. I will have to bookmark this one for later.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Just in response to the link, i am a newb, not a noob. lol

    OTL. thats very interesting, gotta show that to my friends. Thanks.

    -Yi

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    //The function names
    int main(void)
    {
    int x, y;
    srand(time(NULL));
    x = rand() &#37; 100;
    
    
    //Code #1
    if(x % 10 != 0)
    {
    if (x % 2 == 0){
    printf("YOU GOT THE NUMBER %d AND ITS A EVEN NUMBER!\n", x);
    fflush(stdout);
    }
    else
    printf("YOU GOT THE NUMBER %d AND ITS A ODD NUMBER!\n", x);
    fflush(stdout);
    }
    if (x % 10 == 0)
    {
    if(x == 0)
    printf("You got the number %d, so we can't start!", x);
    else
    printf("YOU GOT THE NUMBER %d AND ITS AN EVEN NUMBER, BUT YOU ARE THE WEAKEST LINK, GOOD-BYE!\n", x);
    return (EXIT_SUCCESS);
    }
    
    
    y = x;
    x = rand() % 100;
    
    
    //Code #2
    while(x % 10 != 0)
    {
    if (x % 2 == 0 && x > y)
    {
    printf("The NUMBER is %d (even) which is > %d also the previous integer.\n", x, y);
    fflush(stdout);
    }
    if (x % 2 != 0 && x > y)
    {
    printf("The NUMBER is %d (odd) which is > %d also the previous integer.\n", x, y);
    fflush(stdout);
    }
    if (x % 2 == 0 && x < y)
    {
    printf("The NUMBER is %d (even) which is < %d also the previous integer.\n", x, y);
    fflush(stdout);
    }
    if (x % 2 != 0 && x < y)
    {
    printf("The NUMBER is %d (odd) which is < %d also the previous integer.\n", x, y);
    fflush(stdout);
    }
    y = x;
    x = rand() % 100;
    }
    if (x == 0)
    printf("YOU GOT THE NUMBER %d, so we can't start :'(\n", x);
    else
    printf("YOU GOT THE NUMBER %d AND ITS AN EVEN NUMBER, BUT YOU ARE THE WEAKEST LINK, GOOD-BYE!\n", x);
    return (EXIT_SUCCESS);
    }

    This is the code I made last night with the code, thank you guys for helping me solve this problem.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Much cleaner. Well except for the lack of indentation. But your actual code is more on the ball.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Indent, please!
    You cannot possible expect us to read that unreadable code mess.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    So where's the indentation?
    It's unreadable.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Salem is up to over 19,300 posts. Do you realize roughtly 19,000 of those posts were demands to fix indentation or use code tags?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Hey, quick and easy posts
    It's the easiest way to spam, while not spamming
    But seriously, it's unfortunately a needed reminder.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I notice the board will sometimes demand I put code tags around things in my posts that aren't even really code. How come it won't demand indentation if it detects code tags but no use of either the tab character or multiple consecutive spaces? Just a thought Kermi.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    It does demand code tags when it detects both { and } in the reply and not within code tags.
    I'm not sure how people are getting around this, unless they're just posting a reply and directly editing it afterwards.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21