// Author: F. WU // 2019-01-06 /*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 20 int main() { int numQ = 0; int numAdd = 0; int numRt = 0; printf("Arithmetic practice with negative numbers...\n"); printf("Enter the number of questions: "); scanf("%i",&numQ); printf("Generating %i questions. Starting now.\n",numQ); printf("Type in your answer, and press enter to submit.\n"); srand(time(NULL)); numAdd = (int) floor((double)numQ/2); // half questions on adding //printf("numAdd = %i\n",numAdd); for(int i = 1; i <= numAdd; i++) { int a = 0; int b = 0; int ans = 0; int inp = 0; a = rand() % (MAX_ABS*2+1)-MAX_ABS-5; b = rand() % (MAX_ABS*2+1)-MAX_ABS-5; ans = a+b; printf("(%i) + (%i) = ",a,b); scanf("%i",&inp); if((inp-ans)==0) { printf("Correct!\n"); numRt++; } else printf("Incorrect. The right answer is: %i\n",ans); } for(int i = 1; i <= (numQ-numAdd); i++) { int a = 0; int b = 0; int ans = 0; int inp = 0; a = rand() % (MAX_ABS*2+1)-MAX_ABS-5; b = rand() % (MAX_ABS*2+1)-MAX_ABS-5; ans = a-b; printf("(%i) - (%i) = ",a,b); scanf("%i",&inp); if((inp-ans)==0) { printf("Correct!\n"); numRt++; } else printf("Incorrect. The right answer is: %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; }