declaring variable values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jacoder
    New Member
    • Oct 2006
    • 13

    declaring variable values

    i amm on my 4th C class ever;)
    i have taken to the language ok but im stuck trying to declare the values of my 3 variables hers my effort HOPE SUMONE CAN CORRECT THIS MESSY CODE & LEND A HELPING HAND TIA

    #include <stdio.h>
    #ifdef _WIN32
    #include <conio.h>
    #else
    #include <curses.h>
    #endif

    int main()
    {

    float coffee;
    float sugar;
    float milk;
    float average;

    coffee = 1.50;
    sugar = 0.99;
    milk = 0.89;

    average = coffee,sugar,mi lk;

    printf("\n the cost of a coffe,sugar,mil k,%d is \n",average);


    getch();
    return 0;

    }


    trying
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by jacoder
    i amm on my 4th C class ever;)
    i have taken to the language ok but im stuck trying to declare the values of my 3 variables hers my effort HOPE SUMONE CAN CORRECT THIS MESSY CODE & LEND A HELPING HAND TIA

    #include <stdio.h>
    #ifdef _WIN32
    #include <conio.h>
    #else
    #include <curses.h>
    #endif

    int main()
    {

    float coffee;
    float sugar;
    float milk;
    float average;

    coffee = 1.50;
    sugar = 0.99;
    milk = 0.89;

    average = coffee,sugar,mi lk;

    printf("\n the cost of a coffe,sugar,mil k,%d is \n",average);


    getch();
    return 0;

    }


    trying
    The declaration of your variables is fine.

    The only thing the compiler is complainig about is that you say you want to print an integer (%d), but you pass a float (average). Replace the %d by a %f.

    Your program will then be syntactically correct. However, since the variable is called average, you may want to determine the average of the prices. On the other hand your printf text indicates you want to print out the sum.

    For the sum you would do
    Code:
    average = coffe + milk + sugar;
    and for the average
    Code:
    average = (coffe + milk + sugar) / 3;
    Your current statement assigns the value of coffe to average.

    And, finally, you may want to move the %f behind the "is" in your print ...

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by arne
      Your current statement assigns the value of coffe to average.
      All correct except that actually

      Your current statement assigns the value of milk to average, not the value of coffee.

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by Banfa
        All correct except that actually

        Your current statement assigns the value of milk to average, not the value of coffee.
        No, it doesn't, at least not on my compiler (gcc 4.02). I would have thought so too, as comma expressions are evaluated from left to right and the 'old' results are discarded.

        Can you check that please with your C implementation?

        Comment

        • jacoder
          New Member
          • Oct 2006
          • 13

          #5
          Originally posted by arne
          No, it doesn't, at least not on my compiler (gcc 4.02). I would have thought so too, as comma expressions are evaluated from left to right and the 'old' results are discarded.

          Can you check that please with your C implementation?
          aha thx alot i took the %d away and added %f since its float datatype i shud have known better but its great to get an extra opinion im using DEV-C++ 4.9.9.2 to compile my code here is what i have now ;)

          /*============== =============*/
          /* NAME: Jacoder */
          /*TUTOR: A.Wizard */
          /*CLASS: Declare3Vars.c */
          /*============== =========*/


          #include <stdio.h>
          #ifdef _WIN32
          #include <conio.h>
          #else
          #include <curses.h>
          #endif

          int main()
          {

          float coffee; // here we have float type vars
          float sugar;
          float milk;
          float average;

          coffee = 1.50;
          sugar = 0.99; // here we give vars values
          milk = 0.89;
          average = coffee,sugar,mi lk; // here we get average paid for a coffee



          printf("\n the cost of a coffe,sugar,mil k, is %f \n",average); // here we get our final result


          getch();
          return 0;

          }

          Comment

          • arne
            Recognized Expert Contributor
            • Oct 2006
            • 315

            #6
            Originally posted by jacoder
            aha thx alot i took the %d away and added %f since its float datatype i shud have known better but its great to get an extra opinion im using DEV-C++ 4.9.9.2 to compile my code here is what i have now ;)

            /*============== =============*/
            /* NAME: Jacoder */
            /*TUTOR: A.Wizard */
            /*CLASS: Declare3Vars.c */
            /*============== =========*/


            #include <stdio.h>
            #ifdef _WIN32
            #include <conio.h>
            #else
            #include <curses.h>
            #endif

            int main()
            {

            float coffee; // here we have float type vars
            float sugar;
            float milk;
            float average;

            coffee = 1.50;
            sugar = 0.99; // here we give vars values
            milk = 0.89;
            average = coffee,sugar,mi lk; // here we get average paid for a coffee



            printf("\n the cost of a coffe,sugar,mil k, is %f \n",average); // here we get our final result


            getch();
            return 0;

            }
            Let it run, please ... what's the output?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by arne
              No, it doesn't, at least not on my compiler (gcc 4.02). I would have thought so too, as comma expressions are evaluated from left to right and the 'old' results are discarded.

              Can you check that please with your C implementation?
              Yes it does, you are falling fowel of the fact that the assignment operator = has a higher precedence than the , operator, try this code

              Code:
              #include<stdio.h>
              
              int main (void)
              {
              	int c,b,x,y;
              
              	c = 5;
              	b = 10;
              
              	y = b, c;
              	x = (b, c);
              
              	printf("%d %d\n", x, y);
              
              	return(0);
              }
              y has the value 10 because = is higher precedence than , so

              y = b, c;

              is evaluated as

              (y = b), c;

              But the comma operator actually evaluates to the type and value of the right right hand expression.

              Try this

              Code:
              #include<stdio.h>
              
              int main (void)
              {
              	int c,b,x;
              
              	c = 0;
              	b = 10;
              
              	if (x = b, c)
              	{
              		printf("Expression true: x = %d\n", x);
              	}
              	else
              	{
              		printf("Expression false: x = %d\n", x);
              	}
              
              
                 return(0);
              }
              it outputs

              Expression false: x = 10

              because = has high precedence than , so x=b is evaluated first but the value of the entire expression is the value of c or 0.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                P.S. But this does mean that your original statement that average is set to the value coffee is correct. just not for the correct reasons :D

                Comment

                • arne
                  Recognized Expert Contributor
                  • Oct 2006
                  • 315

                  #9
                  OK, I see ... thanks for the detailed explanation. So, the C-World is saved and comma expressions are still evaluated from left to right :)

                  But your comment
                  Originally posted by Banfa
                  Your current statement assigns the value of milk to average, not the value of coffee.
                  is not correct: though I hadn't understood why (thanks to you I do now) the value of coffe is assigned to average (and the expression evaluates to milk)!

                  Comment

                  • jacoder
                    New Member
                    • Oct 2006
                    • 13

                    #10
                    Originally posted by arne
                    OK, I see ... thanks for the detailed explanation. So, the C-World is saved and comma expressions are still evaluated from left to right :)

                    But your comment

                    is not correct: though I hadn't understood why (thanks to you I do now) the value of coffe is assigned to average (and the expression evaluates to milk)!
                    thx alot guys >.< so = i now know has presidence over , as does () have presidence over + or - i tried above codes :p i added getch for puase (); also to see console window ;)
                    im going to be tought sum string ("strstr") but have never done any string before do any of you more experienced programers out there have any good examples of howto make string in C thx again ,I like to say thx to you all for the feedback reespect&peaceo ut :D

                    Comment

                    Working...