You are working for the Caterpillar company, developing code for the new robotic
ID: 3542040 • Letter: Y
Question
You are working for the Caterpillar company, developing code for the new robotic steamroller machine, FLATMAKER Deluxe. Your colleague has written the following program piece. You are the systems engineer and it is your job to test this. But she did not comment it well. You have to figure what the following code does before putting it in the machine. Describe its functionality, add comments as appropriate, and show what output it would produce. Also this code has a significant design flaw that could cause the robotic steamroller
You are working for the Caterpillar company, developing code for the new robotic steamroller machine, FLATMAKER Deluxe. Your colleague has written the following program piece. You are the systems engineer and it is your job to test this. But she did not comment it well. You have to figure what the following code does before putting it in the machine. Describe its functionality, add comments as appropriate, and show what output it would produce. Also this code has a significant design flaw that could cause the robotic steamroller's computer to freeze and have the machine get out of control. It is not a particularly fast robot but then again when 12 tons of hot steel (the drums are heated internally) are rolling uncontrolled at any speed, it is in your best interest to respect it. Find this flaw, and fix it.(i.e. write the code again with the fix).Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
int mystery(int a, int b);
int main(void)
{
//this program uses recursion to find the product of 2 integers
int x;
int y;
printf("Enter two integers: ");
scanf("%d%d",&x,&y);
printf("the result is %d ",mystery(x,y));
getch();
return 0;
}
//repeatedly add the first number, the 2nd number of times
int mystery(int a,int b)
{if (b==1) //when b goes down to 1
return a; //return a
else
return a+mystery(a,b-1); //add a
}
the design flaw is if the 2nd number is less than 1, you will get an infinite loop, since b will always be less than 1
correction
#include <stdio.h>
#include <conio.h>
int mystery(int a, int b);
int main(void)
{
//this program uses recursion to find the product of 2 integers
int x;
int y;
printf("Enter two integers: ");
scanf("%d%d",&x,&y);
if(y<0)
printf("the result is %d ",-mystery(x,-y));
else
printf("the result is %d ",mystery(x,y));
getch();
return 0;
}
//repeatedly add the first number, the 2nd number of times
int mystery(int a,int b)
{if (b==1) //when b goes down to 1
return a; //return a
else
return a+mystery(a,b-1); //add a
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.