//Fixed version of the original robotic steamroller program. //New version no longer will crash! #include <stdio.h> #include <conio.h> #include <math.h> int mystery(int a, float B)/>; int main(void) { int x; float y = 0; //This while statement protects the program. If 'y' (which is 'b') is ever 0, it gives an error message and restarts the program //so you have to enter two more digits, if 'y' continues to be 0 it will keep repeating the program until 'y' is not 0. //If, however, 'y' is not equal to zero, then the program will take two numbers, give the output and exit like it should. while(y == 0){ printf("Enter two integers: "); scanf("%d%f", &x, &y); if(y == 0){ printf("ERROR! Second digit cannot be 0!\n");} else{ printf("The result is %d\n", mystery(x, y));} } getch(); return 0; } //FLAW in mystery() function. When b = 0, a + mystery(a, b-1) equals 0 and therefore the mystery() function returns 0, //which turns mystery(x,y) into a 'return 0' statement telling the program to cease prematurely, which causes a crash. int mystery(int a, float B)/> { if(b == 1){ return a; } //Also, the previous function could not handle any negative numbers, therefore I changed the second input of mystery() to a float //and used fabs() to get the absolute value of the second input if it was negative, then mystery() would return a positive integer //back into the main() function. else if(b < 0){ return a + mystery(a, fabs(B)/> - 1);} else{ return a + mystery( a, b - 1); } }
We were supposed to find the problem with the original code, then fix it. Original program is this:
//This program takes two inputs 'x' and 'y' and //returns to the user an output of x + (x * (y-1)). #include <stdio.h> //Library used for basic input/output commands #include <conio.h> int mystery(int a, int B)/>; //Declaration of second function that is defined at the bottom (used in main()). int main(void) //void is unnecessary. It only tells the compiler that main takes no arguments, which could be done by typing int main(). { int x; int y; printf("Enter two integers: "); scanf("%d%d", &x, &y); //Allows you to enter two integers and stores them for later use printf("The result is %d\n", mystery(x, y)); //Print statement to give you the result of the mystery() function that was created getch(); return 0; } //This is the definition for the function declared in the header and used in main(). int mystery(int a, int B)/> //The function myster() takes two integer arguments to be functional when used in the program. { if(b == 1){ return a; //Simply returns the value of the first number you entered if '1' is the second number you enter. } else{ return a + mystery(a, b - 1); //If 'b' is not equal to one, then the program will return the value of the first number you entered (x) PLUS 'x' TIMES 'y' minus one. } //Entering the digits 20 and 10 will give you (20 + (20 * (10 - 1))) }
I found the problem to be, if 0 was entered as the second digit or a negative number was entered, the program would crash. So the first code I pasted is my solution. It allows you to enter 0 and will tell you no and loop back to redo it, it also takes absolute value of negative number so you can enter negatives too.
However, when too large of number is entered, it crashes. Does this just happen due to memory limitations and there is not much I can do about it? Or can this be fixed as well? If I were to enter like "445 and 7948" it would crash... (program takes (445+(445*(7948-1))).