Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I will enter the code I have thus far below. I generated a random number to the

ID: 440235 • Letter: I

Question

I will enter the code I have thus far below. I generated a random number to the user, now I need to give the user the option of using either (+,-,*, or /) to get the sets of numbers to equal 24. For example, if the numbers displayed are (8,5,8,1) the code should look like.... enter (+,-,*, or /) to get 24. Then 8-5 = 3, 3 * 8 = 24, 24/1 = 24. The user used (+,-,* or /) to get the number 24. Here is the first half of the code I have to display the random set of numbers: #include #include #include void main() { int index; srand(time(NULL)); index=rand()%6; if (index==0) printf("[8,5,8,1] "); if (index==1) printf("[5,1,6,1] "); if (index==2) printf("[8,7,8,2] "); if (index==3) printf("[3,9,4,1] "); if (index==4) printf("[9,2,5,2] "); if (index==5) printf("[8,6,4,2] "); } Please help

Explanation / Answer

//are not familiar with either compilers. last try //this runs and compile perfectly fine in both Code::Blocks and Microsoft Visual Studio 2010 #include #include #include char getValidChoice(int,int); int doOp(int,int,char); void main() { int index; int num1,num2,num3,num4; int number=0; char a,b,c; srand(time(NULL)); //seed time index=rand()%6; printf("Try using either (+,-,*, or /) to get the set of numbers to equal 24. "); if (index==0) { printf("[8,5,8,1] "); num1=8; num2=5;num3=8,num4=1; } else if (index==1){ printf("[5,1,6,1] "); num1=5; num2=1;num3=6,num4=1; } else if (index==2){ printf("[8,7,8,2] "); num1=8; num2=7;num3=8,num4=2; } else if (index==3){ printf("[3,9,4,1] "); num1=3; num2=9;num3=4,num4=1; } else if (index==4){ printf("[9,2,5,2] "); num1=9; num2=2;num3=5,num4=2; } else{ printf("[8,6,4,2] "); num1=8; num2=6;num3=4,num4=2; } a=getValidChoice(num1,num2); b=getValidChoice(num2,num3); c=getValidChoice(num3,num4); number=num1; number=doOp(number,num2,a); number=doOp(number,num3,b); number=doOp(number,num4,c); //show results printf(" %d %c %d %c %d %c %d = %d",num1,a,num2,b,num3,c,num4,number); if(number==24){ printf(" Congrats! You succeeded in getting the series to equal 24."); } else printf(" Your result was not equal to 24. Better luck next time."); getchar(); return; } char getValidChoice(int num1,int num2){ char input; do{ printf(" %d ? %d",num1,num2); printf(" Enter ? operation choice(+,-,*,/): "); scanf(" %c",&input); if(input!='+'&&input!='-'&&input!='*'&&input!='/'){ printf("Invalid operation entered."); } }while(input!='+'&&input!='-'&&input!='*'&&input!='/'); return input; } int doOp(int num1,int num2,char op){//returns operation resultant or 0 if error if(op=='+') return num1+num2; if(op=='-') return num1-num2; if(op=='*') return num1*num2; if(op=='/'){ if(num2==0){ //dividing by 0 return 0; } else return num1/num2; //note this is integer division... } return 0;//if you get here, op was invalid }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote