Thread: C noob with some issues

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    5

    Question C noob with some issues

    So I'm on university doing 3th grade of computer sciende (a 4 year career) but I have no idea on how to code so it was actually quite hard for to me to pass this semester (who would have thougth!?).
    Becouse of this I decided to start learning C again, I did learn to code in pseudocode and C in first year but forgot how to.
    Today I was trying to make an algorithm that would return all the A, B, and C variables (all between 0 and 9) such that (10A+B)/(10B+C)=A/C becouse the solutions are nice and I had already solved the equation by hand and already knew all the solutions. But I just can't make it work, I'm using an online C compiler and it doesn't show any error so I guess it's stuck in a loop.
    This is my sloppy code;

    Code:
    #include <stdio.h>
    
    
    int
    main ()
    {
        int a, b, c, d, e;
        while (a + b + c != 27)
        a = 0;
        b = 0;
        c = 0;
        a = a++;
            if (a = 9)
            a = 0;
            b = b + 1;
                if (b = 9)
                c = c++;
                d = (10 * a + b) / (10 * b + c);
                e = a/c;
                    if(d=e)
                    printf ("%d",a);
                    printf ("%d",b);
                    printf ("%d",c);
        return 0;
    }


    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    5
    I think it never gets out of the loop becouse C keeps getting bigger, I have this now:

    Code:
    #include <stdio.h>
    
    
    int
    main ()
    {
        int a, b, c, d, e;
        while (a + b + c != 27)
        a = 0;
        b = 0;
        c = 0;
        a = a++;
            if (a = 9)
            a = 0;
            b = b++;
                if (b = 9)
                b=0;
                while(c<=9)
                     c = c++;
                     d = (10 * a + b) / (10 * b + c);
                     e = a/c;
                         if(d=e)
                         printf ("%d",a);
                         printf ("%d",b);
                         printf ("%d",c);
                    if(c=9)
                        a=30;
        return 0;
    }
    Last edited by Ptolemaic; 09-08-2020 at 02:39 PM. Reason: Damn am I bad at this

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,138
    I think your problem is a lack of change in the variables, all your while loop is doing is setting a to 0, also variables must be initialized BEFORE trying to use them, your loop starts with a, b and c equaling some random number left over from previous usage, in this case it's probably 0 although there's no garuntee on that, so 9999 times out of 10000 a + b + c will never equal 27 which means your loop never ends which means the app never progresses to b = 0;

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    763
    Are you aware that C isn't Python, don't ya?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    455
    C uses curly brackets to indicate blocks. YOu need to read some C code to get the idea of the syntax.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,138
    Guys, I was trying to be subtle about it, you both just handed the answer on a silver plate to xem

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Quote Originally Posted by awsdert View Post
    Guys, I was trying to be subtle about it, you both just handed the answer on a silver plate to xem
    You make a good point, but our homework policy mainly forbids people asking for others to do their homework for them, and consequently we discourage contributors from doing people's homework for them too. Hence, there's a fair bit of leeway in helping as long as we aren't handing out code that can be used as-is or other answers that don't require the help seeker to adapt/apply the help given for themselves. Both flp1969's reply and Malcolm McLean's reply thus fall within this acceptable range of answers: they're just somewhat more obvious hints than yours (and maybe not for flp1969's case: it assumes Ptolemaic knows Python, but the poor indentation means that a Python version of Ptolemaic's code wouldn't even be syntactically correct).
    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

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,138
    Quote Originally Posted by laserlight View Post
    You make a good point, but our homework policy mainly forbids people asking for others to do their homework for them, and consequently we discourage contributors from doing people's homework for them too. Hence, there's a fair bit of leeway in helping as long as we aren't handing out code that can be used as-is or other answers that don't require the help seeker to adapt/apply the help given for themselves. Both flp1969's reply and Malcolm McLean's reply thus fall within this acceptable range of answers: they're just somewhat more obvious hints than yours (and maybe not for flp1969's case: it assumes Ptolemaic knows Python, but the poor indentation means that a Python version of Ptolemaic's code wouldn't even be syntactically correct).
    *face palms* didn't even occur to me that it was xer homework, glad I took the route of a subtle answer

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,076
    Quote Originally Posted by awsdert View Post
    *face palms* didn't even occur to me that it was xer homework, glad I took the route of a subtle answer
    It might not be formal homework, but besides the fact that we cannot be sure, there's also the thing where applying stuff rather than just using it as-is usually leads to better learning outcomes, whether the problem was set by some instructor or boss or the help seekers for themselves. So you're doing the right thing, but it isn't (necessarily) the case that flp1969 and Malcolm McLean are doing the wrong thing.
    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
    Registered User
    Join Date
    Sep 2020
    Posts
    5
    Guys this is not my homework lmao, we don't even have homework at my univeristy.
    Nobody is gonna tell me how to fix it then?

  11. #11
    Registered User
    Join Date
    Sep 2020
    Posts
    5
    I have this now but it doesn't return anything:
    Code:
    int main()
    {
        int a, b, c, d, e;
        a=0;
        b=0;
        c=0;
        d=0;
        e=0;
        while(c!=9 && b!=9 && a!=9)
            a=a++;
            if(a=9)
                a=0;
                b=b+1;
                if(b=9)
                    b=0;
                    c=c++;
            d=(10*a+b)/(10*b+c);
            e=a/c;
            if(d=e)
                printf("%d",a);
                printf("%d",b);
                printf("%d",c);
        return 0;
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,285
    1. You need lots of braces. C isn't Python. Just because you indented it nicely doesn't make C follow a different path through the code.

    2. Use == in place of =, if you want to compare two values.

    3. Don't mix = and ++ together with the same variable.

    So at a minimum, you need something like
    Code:
        while(c!=9 && b!=9 && a!=9) {
            a++;
            if(a==9) {
                a = 0;
            }
        }
    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.

  13. #13
    Registered User
    Join Date
    Sep 2020
    Posts
    5
    Many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help because i am a noob!
    By FruixX in forum C++ Programming
    Replies: 11
    Last Post: 04-26-2013, 01:04 PM
  2. Help a Noob 2 :))
    By jadumonium in forum C Programming
    Replies: 4
    Last Post: 08-20-2011, 04:31 AM
  3. Help a noob. :(
    By jadumonium in forum C Programming
    Replies: 9
    Last Post: 08-19-2011, 07:53 PM
  4. The noob(me) needs help again! plz...
    By shadowsora15 in forum C++ Programming
    Replies: 11
    Last Post: 06-01-2007, 04:56 PM
  5. Need help...I'm a noob
    By viciousv322 in forum C++ Programming
    Replies: 8
    Last Post: 05-24-2007, 11:52 AM

Tags for this Thread