Question

#include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x)...

#include <stdio.h>

void printHelp () {
  printf ("\n");
  printf ("a: a(x) = x*x\n");
  printf ("b: b(x) = x*x*x\n");
  printf ("c: c(x) = x^2 + 2*x + 7\n");
  printf ("q: quit\n");
}

void a(float x) {
  float v = x*x;
  printf ("  a(%.2f) = %.2f^2 = %.2f\n", x, x, v);
} // end function a

void b(float x) {
  float v = x*x*x;
  printf ("  a(%.2f) = %.2f^3 = %.2f\n", x, x, v);
} // end function b

void c(float x) {
  float v = x*x + 2*x + 7;
  printf ("  c(%.2f) = %.2f^2 + 2*%.2f + 7 = %.2f\n",
            x, x, x, v);
} // end function c

int menu () {
  char selection;
  float x;
  printHelp ();
  scanf ("%s", &selection);
  if (selection == 'q') return 1;
  scanf ("%f", &x);
  if (selection == 'a') a(x);
  if (selection == 'b') b(x);
  if (selection == 'c') c(x);
  return 0;
} // end function menu

int main() {
  while (menu() == 0);
  printf ("... bye ...\n");
  return 0;
} // end main

Why is the number of x’s different in the printf statements in the functions?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
void a(float x) {
  float v = x*x;
  printf ("  a(%.2f) = %.2f^2 = %.2f\n", x, x, v); // it is equal to a(x) = x^2 = v(=x*x)
} // end function a

In the above printf statement, x value is used two times and v value is used one time. 
The expression inside printf statement prints value of function a(x).
The expression is : a(x)=x^2=v
Hence, two times x value and one time v value is passed.


void b(float x) {
  float v = x*x*x;
  printf ("  b(%.2f) = %.2f^3 = %.2f\n", x, x, v); // it is equal to b(x) = x^3 = v(=x*x*x)
} // end function b

In the above printf statement, x value is used two times and v value is used one time.
The expression inside printf statement prints value of function b(x).
The expression is : b(x)=x^3=v
Hence, two times x value and one time v value is passed.


void c(float x) {
  float v = x*x + 2*x + 7;
  printf ("  c(%.2f) = %.2f^2 + 2*%.2f + 7 = %.2f\n",
            x, x, x, v); // it is equal to c(x) = x^2+2*x+7 = v(=x*x + 2*x + 7)
} // end function c 

In the above printf statement, x value is used three times and v value is used one time.
The expression inside printf statement prints value of function c(x).

The expression is : c(x)=x^2+2*x+7

Hence, three times x value and one time v value is passed.

To better understand , count the number of occurrences of x and v in the expression.

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> void printHelp () { printf ("\n"); printf ("a: a(x) = x*x\n"); printf ("b: b(x)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT