Question

In: Computer Science

What would have to be changed in the code if the while statement were changed to:...

What would have to be changed in the code if the while statement were changed to:

while (menu == 5);

Code is as follows

 
  1. #include <stdio.h>

  2. void printHelp ()

  3. {

  4. printf ("\n");

  5. printf ("a: a(x) = x*x\n");

  6. printf ("b: b(x) = x*x*x\n");

  7. printf ("c: c(x) = x^2 + 2*x + 7\n");

  8. printf ("d: shrink(x) = x/2\n");

  9. printf ("q: quit\n");

  10. }

  11. void a(float x)

  12. {

  13. float v = x*x;

  14. printf (" a(%.2f) = %.2f^2 = %.2f\n", x, x, v);

  15. } // end function a

  16. void b(float x)

  17. {

  18. float v = x*x*x;

  19. printf (" b(%.2f) = %.2f^3 = %.2f\n", x, x, v);

  20. } // end function b

  21. void c(float x)

  22. {

  23. float v = x*x + 2*x + 7;

  24. printf (" c(%.2f) = %.2f^2 + 2*%.2f + 7 = %.2f\n",

  25. x, x, x, v);

  26. } // end function c

  27. void shrink(float x){

  28. float v = x/2;

  29. printf("shrink(%.2f) = %.2f/2 = %.2f\n", x, x, v);

  30. }//end of function shrink

  31. int menu ()

  32. {

  33. char selection;

  34. float x;

  35. printHelp ();

  36. scanf ("%s", &selection);

  37. if (selection == 'q')

  38. return 1;

  39. scanf ("%f", &x);

  40. if (selection == 'a')

  41. a(x);

  42. if (selection == 'b')

  43. b(x);

  44. if (selection == 'c')

  45. c(x);

  46. if(selection == 'd')

  47. shrink(x);

  48. return 0;

  49. } // end function menu

  50. int main()

  51. {

  52. while (menu() == 0);

  53. printf ("... bye ...\n");

  54. return 0;

  55. } // end main

Solutions

Expert Solution

Hi,

According to question, we need to change while condition to while (menu == 5);

In the code, 'menu' is a function. I am not sure if the required while condition should be  while (menu() == 5); or

while (menu == 5);   

The absence of parenthesis ' () ' makes menu as a function to a variable.

So I am putting answers for while (menu() == 5); and while (menu == 5); both.
Please make this sure if required condition consists menu() or menu only.

Please refer below changes need to be done to use given while condition.

Scenario 1 while (menu() == 5);
Please refer below changes needs to be done.

Method menu() will get changed as below with return value as 5 ( highlighted part ) .

int menu()

{

   char selection = 'n';

   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);

   if (selection == 'd')

       shrink(x);

   return 5;

} // end function menu

int main()

{
  
   while (menu() == 5);

   printf("... bye ...\n");

   return 0;

} // end main

Remaining methods will remain unchanged.

Scenario 2   while (menu == 5);

In this case, we will need an integer type variable with name 'menu'. But to use this we need to change our function name, as we cannot use same variable name and function at global scope simultaneously. In following solution, I have changed function name of menu to 'menu_function'. You can use function name of your choice here.
Take a look on the following changes.

I have highlighted major changes in code.

int menu_function()

{

   char selection = 'n';

   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);

   if (selection == 'd')

       shrink(x);

   return 5;

} // end function menu

int main()

{
   int menu = 5;
   while (menu == 5)
   {
       menu = menu_function();
   }

   printf("... bye ...\n");

   return 0;

} // end main

End of solution

Remaining functions printhelp(), a(), b(), c(), shrink() would remain unchanged.

That's all for now.
Please mention comments or queries if any in comments section. I will be available to resolve any questions through comment section.

Thank You.


Related Solutions

What would have changed in America if the federal government chose to annex Cuba as it...
What would have changed in America if the federal government chose to annex Cuba as it did Puerto Rico?
Computers have changed a lot since they were first introduced. They have become smaller and more...
Computers have changed a lot since they were first introduced. They have become smaller and more portable. They now even come in the form of smartphones, which can do much of what only desktops could do just a few years ago. We now have watches and other devices with more computing power than laptops. Where do you see computing going in the future? What types of devices do you think we will be using as computers, small and large, become...
You were introduced to control structures - such as IF-ELSE statement, WHILE and FOR loops. Describe...
You were introduced to control structures - such as IF-ELSE statement, WHILE and FOR loops. Describe what they do and why are control structures important to programming?
the following statement : International business and globalisation have changed the financial and economic climate of...
the following statement : International business and globalisation have changed the financial and economic climate of the world into a centralised marketplace that allows access to all global stakeholders. It provided a pool of products and services, previously not available to all global customers or consumers, with the touch of a button. It allowed governments to engage on a regular basis in terms of trade and trade-related issues and to form bilateral trade agreements in the interest of all stakeholders....
Once a scope statement is agreed to, how is it changed and what is always required...
Once a scope statement is agreed to, how is it changed and what is always required when a scope statement changed?
“How do you think the stock market would be affected if the laws were changed so...
“How do you think the stock market would be affected if the laws were changed so that trading on insider information was no longer illegal? What would be the impact on the goal of the financial manager if such a change were to occur?”
Force Table: If the center ring were to have considerable mass, what effect would it have...
Force Table: If the center ring were to have considerable mass, what effect would it have on the experiment?
Discuss what changes would have to be made to a financial forecast if a hospital were...
Discuss what changes would have to be made to a financial forecast if a hospital were investor-owned versus being a non-profit.
In the following code, what values could be read into a number to terminate the while...
In the following code, what values could be read into a number to terminate the while loop? PRINT "Enter a number: READ user input number ← user input WHILE (number < 1 or number > 10) PRINT "Enter another number: " READ user input number ← user input END WHILE A) Numbers in the range 0 - 9 B) Numbers in the range 1 - 10 C) Numbers greater than 10 D) Numbers less than 1
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT