Simple output problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RadiationX

    #1

    Simple output problem

    I have to solve the following problem:Write a program that accepts two
    integers, and determines if the second is a factor (is evenly divisible
    into) the first. Here is the code i have so far.


    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int a; //dividend
    int b; //divisor
    int c;
    c==0;

    printf("<------The Factoring Machine------->\n");
    printf("\nPleas e enter an integer followed by\n");
    printf("a space and then the possible factor integer.\n");
    printf("For exampe: [a b] is the correct format.\n");
    scanf("%d%d", &a, &b);
    c == a/b;


    if(a%b==0)
    {
    printf("\n %d is a factor of %d\n", b, a);
    printf("\nThe other factor is %d\n", c);

    }

    else
    {
    printf("\n%d is not a factor of %d\n",b,a);
    }
    system("PAUSE") ;
    return 0;
    }

    why is the %d in the second printf not outputting the value of variable
    c?
    This should be the other factor.

  • xyombie

    #2
    Re: Simple output problem

    The problem is in the line that reads "c == a/b;". Using two equals is
    a comparison (boolean condition). It's asking "is c equal to a/b?",
    returning either a zero or one for true or false, but doesn't save this
    value anywhere.

    What you probably meant to do is "c = (int) a/b;" which will take the
    value of a/b and place it into the variable c. Also, because a/b is
    returning a double, it's probably a good idea to tell the compiler to
    cast it back to an integer because c is an integer. Without the (int),
    you might get a compiler warning depending on the compiler.

    Good luck.


    RadiationX wrote:[color=blue]
    > I have to solve the following problem:Write a program that accepts two
    > integers, and determines if the second is a factor (is evenly divisible
    > into) the first. Here is the code i have so far.
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int a; //dividend
    > int b; //divisor
    > int c;
    > c==0;
    >
    > printf("<------The Factoring Machine------->\n");
    > printf("\nPleas e enter an integer followed by\n");
    > printf("a space and then the possible factor integer.\n");
    > printf("For exampe: [a b] is the correct format.\n");
    > scanf("%d%d", &a, &b);
    > c == a/b;
    >
    >
    > if(a%b==0)
    > {
    > printf("\n %d is a factor of %d\n", b, a);
    > printf("\nThe other factor is %d\n", c);
    >
    > }
    >
    > else
    > {
    > printf("\n%d is not a factor of %d\n",b,a);
    > }
    > system("PAUSE") ;
    > return 0;
    > }
    >
    > why is the %d in the second printf not outputting the value of variable
    > c?
    > This should be the other factor.[/color]

    Comment

    • xyombie

      #3
      Re: Simple output problem

      I also just reallized, you have the same problem in the line "c==0; ".
      There is no need to initialize c, but if you wanted to, you would use
      "c=0;".

      As a reminder, double equal "==" is for comparison, while single "=" is
      for setting a variable equal to another variable or equation. When you
      mix this up, it can be a difficult bug to find because the compiler
      isn't going to complain because it is valid C syntax.

      Comment

      • RadiationX

        #4
        Re: Simple output problem

        that fixed it. thanks..

        Comment

        • abdur_rab7@yahoo.co.in

          #5
          Re: Simple output problem


          RadiationX wrote:[color=blue]
          > int a; //dividend
          > int b; //divisor
          > int c;
          > c==0;
          >[/color]

          To avoid future errors, correct the initialization statement c==0; to
          c=0;

          ---
          Best Regards,
          Abdur

          Comment

          • Vladimir S. Oka

            #6
            Re: Simple output problem

            xyombie wrote:[color=blue]
            > RadiationX wrote:[color=green]
            >> I have to solve the following problem:Write a program that accepts
            >> two integers, and determines if the second is a factor (is evenly
            >> divisible into) the first. Here is the code i have so far.
            >>
            >>
            >> #include <stdio.h>
            >> #include <stdlib.h>
            >>
            >> int main()
            >> {
            >> int a; //dividend
            >> int b; //divisor
            >> int c;
            >> c==0;
            >>
            >> printf("<------The Factoring Machine------->\n");
            >> printf("\nPleas e enter an integer followed by\n");
            >> printf("a space and then the possible factor integer.\n");
            >> printf("For exampe: [a b] is the correct format.\n");
            >> scanf("%d%d", &a, &b);
            >> c == a/b;
            >>
            >>
            >> if(a%b==0)
            >> {
            >> printf("\n %d is a factor of %d\n", b, a);
            >> printf("\nThe other factor is %d\n", c);
            >>
            >> }
            >>
            >> else
            >> {
            >> printf("\n%d is not a factor of %d\n",b,a);
            >> }
            >> system("PAUSE") ;
            >> return 0;
            >> }
            >>
            >> why is the %d in the second printf not outputting the value of
            >> variable c?
            >> This should be the other factor.[/color][/color]

            Please don't top post. I corrected it this time...
            [color=blue]
            >
            > The problem is in the line that reads "c == a/b;". Using two equals
            > is a comparison (boolean condition). It's asking "is c equal to
            > a/b?", returning either a zero or one for true or false, but doesn't
            > save this value anywhere.[/color]

            You probably meant the correct thing, but your expression of it may be
            confusing (see the order of true and false vis order of zero and one in
            your sentence). A logical operation in C yields 1 if condition is true,
            and 0 if it is false -- not the other way around, as your post implies.
            [color=blue]
            >
            > What you probably meant to do is "c = (int) a/b;" which will take the
            > value of a/b and place it into the variable c. Also, because a/b is
            > returning a double, it's probably a good idea to tell the compiler to[/color]

            `a/b` is _not_ returning a double. It returns an int, after
            performing /integer/ division (any fractional part is discarded).
            [color=blue]
            > cast it back to an integer because c is an integer. Without the
            > (int), you might get a compiler warning depending on the compiler.[/color]

            There's no need for a cast here. All variables are of the type int, as
            is the result of the division. What _is_ advisable though is to check
            for division by zero (obviously before it is attempted), and do
            something sensible if it's detected, e.g.:

            if (0 == b)
            {
            fprintf(stderr, "Division by zero attempted!\n");
            return EXIT_FAILURE;
            }

            Or ask user to try and input a legal value for b.

            Cheers

            Vladimir

            --
            What is mind? No matter.
            What is matter? Never mind.
            -- Thomas Hewitt Key, 1799-1875

            Comment

            • Mike Wahler

              #7
              Re: Simple output problem


              "RadiationX " <heavyreader@gm ail.com> wrote in message
              news:1138333690 .096638.186550@ g43g2000cwa.goo glegroups.com.. .[color=blue]
              >I have to solve the following problem:Write a program that accepts two
              > integers, and determines if the second is a factor (is evenly divisible
              > into) the first. Here is the code i have so far.
              >
              >
              > #include <stdio.h>
              > #include <stdlib.h>
              >
              > int main()
              > {
              > int a; //dividend
              > int b; //divisor
              > int c;
              > c==0;[/color]

              c = 0;

              or simply replace above two lines with:

              int c = 0;

              Look up the difference between = and == (they're
              two distinct operators with different meanings.

              [color=blue]
              > printf("<------The Factoring Machine------->\n");
              > printf("\nPleas e enter an integer followed by\n");
              > printf("a space and then the possible factor integer.\n");
              > printf("For exampe: [a b] is the correct format.\n");
              > scanf("%d%d", &a, &b);[/color]

              Be very careful with scanf(). See the FAQ for details.
              [color=blue]
              > c == a/b;[/color]

              Again, wrong operator.

              c = a / b;
              [color=blue]
              > if(a%b==0)
              > {
              > printf("\n %d is a factor of %d\n", b, a);
              > printf("\nThe other factor is %d\n", c);
              >
              > }
              >
              > else
              > {
              > printf("\n%d is not a factor of %d\n",b,a);
              > }
              > system("PAUSE") ;
              > return 0;
              > }
              >
              > why is the %d in the second printf not outputting the value of variable
              > c?[/color]

              Actually it was. As your code is written, the only possible values
              for 'c' were zero and one.
              [color=blue]
              > This should be the other factor.[/color]

              -Mike


              Comment

              • Arndt Jonasson

                #8
                Re: Simple output problem


                "xyombie" <brianblank@gma il.com> writes:[color=blue]
                > I also just reallized, you have the same problem in the line "c==0; ".
                > There is no need to initialize c, but if you wanted to, you would use
                > "c=0;".
                >
                > As a reminder, double equal "==" is for comparison, while single "=" is
                > for setting a variable equal to another variable or equation. When you
                > mix this up, it can be a difficult bug to find because the compiler
                > isn't going to complain because it is valid C syntax.[/color]

                If you tell it to, it will:

                % cat divv.c
                int main()
                {
                int a,b,c;
                a = b = c = 1;
                c == a/b;
                return 0;
                }
                % gcc -W divv.c
                divv.c: In function `main':
                divv.c:5: warning: statement with no effect

                Comment

                • Flash Gordon

                  #9
                  Re: Simple output problem

                  Arndt Jonasson wrote:[color=blue]
                  > "xyombie" <brianblank@gma il.com> writes:[color=green]
                  >> I also just reallized, you have the same problem in the line "c==0; ".
                  >> There is no need to initialize c, but if you wanted to, you would use
                  >> "c=0;".
                  >>
                  >> As a reminder, double equal "==" is for comparison, while single "=" is
                  >> for setting a variable equal to another variable or equation. When you
                  >> mix this up, it can be a difficult bug to find because the compiler
                  >> isn't going to complain because it is valid C syntax.[/color][/color]

                  Make that isn't required to. Some will.
                  [color=blue]
                  > If you tell it to, it will:[/color]

                  <snip>

                  No, it's not that it will, only that some do. There is no requirement to
                  produce a diagnostic since as xyombie says it is perfectly valid, and
                  I'm sure there are compilers that won't warn you.
                  --
                  Flash Gordon
                  Living in interesting times.
                  Although my email address says spam, it is real and I read it.

                  Comment

                  • Nelu

                    #10
                    Re: Simple output problem

                    xyombie wrote:[color=blue]
                    > The problem is in the line that reads "c == a/b;". Using two equals is
                    > a comparison (boolean condition). It's asking "is c equal to a/b?",
                    > returning either a zero or one for true or false, but doesn't save this
                    > value anywhere.
                    >
                    > What you probably meant to do is "c = (int) a/b;" which will take the
                    > value of a/b and place it into the variable c. Also, because a/b is
                    > returning a double, it's probably a good idea to tell the compiler to
                    > cast it back to an integer because c is an integer. Without the (int),
                    > you might get a compiler warning depending on the compiler.[/color]
                    Please, don't top post.
                    --
                    Ioan - Ciprian Tandau
                    tandau _at_ freeshell _dot_ org (hope it's not too late)
                    (... and that it still works...)

                    Comment

                    • Nelu

                      #11
                      Re: Simple output problem

                      xyombie wrote:[color=blue]
                      > I also just reallized, you have the same problem in the line "c==0; ".
                      > There is no need to initialize c, but if you wanted to, you would use
                      > "c=0;".
                      >
                      > As a reminder, double equal "==" is for comparison, while single "=" is
                      > for setting a variable equal to another variable or equation. When you
                      > mix this up, it can be a difficult bug to find because the compiler
                      > isn't going to complain because it is valid C syntax.
                      >[/color]
                      (Don't top post from the previous message)
                      Please quote some context, even if it's your post.

                      --
                      Ioan - Ciprian Tandau
                      tandau _at_ freeshell _dot_ org (hope it's not too late)
                      (... and that it still works...)

                      Comment

                      • Nelu

                        #12
                        Re: Simple output problem

                        RadiationX wrote:[color=blue]
                        > that fixed it. thanks..
                        >[/color]
                        That fixed what? Thanks for what?
                        I'm glad it got fixed and that you are thanking someone.
                        Please let us know what you're talking about.
                        QUOTE SOME CONTEXT.

                        --
                        Ioan - Ciprian Tandau
                        tandau _at_ freeshell _dot_ org (hope it's not too late)
                        (... and that it still works...)

                        Comment

                        • Arndt Jonasson

                          #13
                          Re: Simple output problem


                          Flash Gordon <spam@flash-gordon.me.uk> writes:[color=blue]
                          > Arndt Jonasson wrote:[color=green]
                          > > If you tell it to, it will:[/color]
                          >
                          > <snip>
                          >
                          > No, it's not that it will, only that some do. There is no requirement
                          > to produce a diagnostic since as xyombie says it is perfectly valid,
                          > and I'm sure there are compilers that won't warn you.[/color]

                          To be completely clear, I ought to have written, "if there is a way to
                          tell the compiler, then if you tell it to, it will." - I suppose I
                          made the impression of saying that every compiler works like 'gcc'.

                          Comment

                          • Dave Thompson

                            #14
                            Re: Simple output problem

                            On Fri, 27 Jan 2006 06:09:14 +0000 (UTC), "Vladimir S. Oka"
                            <novine@btopenw orld.com> wrote:
                            [color=blue]
                            > xyombie wrote:[/color]
                            <snip>[color=blue][color=green]
                            > > What you probably meant to do is "c = (int) a/b;" which will take the
                            > > value of a/b and place it into the variable c. Also, because a/b is
                            > > returning a double, it's probably a good idea to tell the compiler to[/color]
                            >
                            > `a/b` is _not_ returning a double. It returns an int, after
                            > performing /integer/ division (any fractional part is discarded).
                            >[/color]
                            Right. And even if a and b were floating-point, (int) a / b would
                            convert a to int (if in range, else Undefined Behavior), then convert
                            back to floating-point because of the usual arithmetic conversions for
                            computational operators, do the division in floating-point giving a
                            floating-point result, then implicitly convert to int (again if in
                            range and assuming c is int).

                            To explicitly force an integer result from flt-pt; c = (int) (a/b);

                            To explicitly do integer division: c = (int)a / (int)b;

                            (Or similarly for long, long long, unsigned, etc.)

                            - David.Thompson1 at worldnet.att.ne t

                            Comment

                            Working...