// Author: F. WU // 2019-01-13 /*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ #include #include #include #include #define MAX_ABS 10 int main() { int numQ = 0; int numRt = 0; int inp = 0; printf("Algebra practice for solving simple equations...\n"); printf("Enter the number of questions: "); scanf("%i",&numQ); printf("Generating %i questions. Starting now.\n",numQ); printf("Solve for x.\nType in your answer, and press enter to submit.\n"); srand(time(NULL)); for(int i = 1; i <= numQ; i++) { // The equation is in the form of: // ax+b = cx+d // (a-c)x = d-b int a = 0; int b = 0; int c = 0; int d = 0; int ans = 0; while(a==c){ // to prevent trivial solutions a = rand() % MAX_ABS+1; // No negative numbers for now. c = rand() % MAX_ABS+1; } d = rand() % MAX_ABS+1; ans = rand() % MAX_ABS+1; b = (c-a)*ans+d; // Print the negative numbers appropriately. if(b<0){ printf("%ix - %i = %ix + %i\n",a,-b,c,d); } else{ printf("%ix + %i = %ix + %i\n",a,b,c,d); } printf("x = "); scanf("%i",&inp); if((inp-ans)==0) { printf("Correct!\n"); numRt++; } else printf("Incorrect. The right answer is: x = %i\n",ans); } printf("The end.\n"); printf("Your score is %i / %i, or %.1f%%.\n",numRt,numQ,(double)numRt/(double)numQ*100); return 0; }