In: Computer Science
What would have to be changed in the code if the while statement were changed to:
while (menu == 5);
Code is as follows
#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 ("d: shrink(x) = x/2\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 (" b(%.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
void shrink(float x){
float v = x/2;
printf("shrink(%.2f) = %.2f/2 = %.2f\n", x, x, v);
}//end of function shrink
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);
if(selection == 'd')
shrink(x);
return 0;
} // end function menu
int main()
{
while (menu() == 0);
printf ("... bye ...\n");
return 0;
} // end main
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.