Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › help me with C programming
New Posts  All Forums:Forum Nav:

help me with C programming

post #1 of 5
Thread Starter 
Code:
/*
 * Author: Jason Hurd
 * Date: 10/3/13
 * 
 * This program will ask a series of questions then create
 * a set of simple math problems which involve addition, subtraction,
 * multiplication, and division.
 */

#include <stdio.h>

void addition (int, int);
void subtraction (int, int);
void multiplication (int, int);
void division (int, int);

int rand1, rand2;

int main (){
   
   char add[5], subt[5], mult[5], div[5];
   int min, max, questions;
   int temp;

   srand(time(NULL));

   printf("Do you want to have addition problems? ");
   scanf("%s", add);
   printf("Do you want to have subtraction problems? ");
   scanf("%s", subt);
   printf("Do you want to have multiplication problems? ");
   scanf("%s", mult);
   printf("Do you want to have diviision problems? ");
   scanf("%s", div);
   printf("What is the smallest number you want in the questions? ");
   scanf("%d", &min);
   printf("What is the biggest number you want in the questions? ");
   scanf("%d", &max);
   printf("How many questions do you want? ");
   scanf("%d", &questions);

   addition (min, max);
   subtraction (min, max);
   multiplication (min, max);
   division (min, max);

   return 0;
   }


void addition (int min, int max){
   //addition
      rand1 = rand() % (max-min) + min;
      rand2 = rand() % (max-min) + min;
      printf(" %3d  + %3d =\n", rand1, rand2);
      }

void subtraction (int min, int max){
   //subtraction
      rand1 = rand() % (max-min) + min;
      rand2 = rand() % (max-min) + min;
   //the bigger number needs to be put first, to avoid negative numbers
      if(rand1 >= rand2){
         printf(" %3d  - %3d =\n",  rand1, rand2);
         ;
      }if(rand2 > rand2){
         printf(" %3d  - %3d =\n",  rand2, rand1);
         ;}}

void multiplication(int min, int max){
   //multiplication
      rand1 = rand() % (max-min) + min;
      rand2 = rand() % (max-min) + min;
      printf(" %3d  * %3d =\n", rand1, rand2);
      ;}

void division(int min, int max){
   //division
   int i = 0;
   while (i == 0){
      rand1 = rand() % (max-min) + min;
      rand2 = rand() % (max-min) + min;
   //make sure the remainder is 0, then devide the bigger number by the smaller one. this avoids decimals
      if(0 == rand1 % rand2 || 0 == rand2 % rand1){
         if(rand1 > rand2){
            printf(" %3d  / %3d =\n",  rand1, rand2);
            break;
         }else{
            printf(" %3d  / %3d =\n", rand2, rand1);
            break;
         }}}}

I need to make it so that it can take an input of which of the 4 types of problems to accept, then print a random one each time for as many times as it is told to. right now, I have the random number generation for the problems set up, and in the code i posted, i put the function calls in the body.

really, i'm just having a hard time grasping what i need to do for the different ways the user can enter which type of problem. I've got some ideas, like maybe a while loop with an if statement for each type of problem, but that wouldn't give me a random one each time. i need there to be an equal chance of the next problem being multiplication as it is anything else.
post #2 of 5
It sounds like to me you need a switch statement.

Then for the variable that the switch statement uses, you want another random number generator that generates a number between 0 and 3.

pseudo code could be...
Code:
switch( random number between 0 and 3):
    case(0):
        add();
   case(1):
        subtract();
   .......
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
post #3 of 5
Thread Starter 
I solved it with a loop that generated a random number, then if that number also had a yes, it would accept and print that question.
Code:
i = questions;
   while(i > 0){
      temp = 1 + rand() % 4;

      if (temp == 1 && add[0] == 'y'){
         addition (min, max);
         i--;
      }if (temp == 2 && subt[0] == 'y'){
         subtraction (min, max);
         i--;
      }if (temp == 3 && mult[0] == 'y'){
         multiplication (min, max);
         i--;
      }if (temp == 4 && div[0] == 'y'){
         division (min, max);
         i--;
      }
   }

edit: the issue i was facing, was if they put a "n" in any of the boxes, i do not want to give problems for that sign. my solution ended up working very well. i'm sure it makes unnecessary loops, but it is consistent and has worked every time with different numbers as input
post #4 of 5
Thread Starter 
btw, i gave you a plus rep

thank you for your help. my code is finished now, free to close this thread
post #5 of 5
Glad you found a solution that worked.

Depending on your compiler, a switch statement might improve the performance of your code. Switch statements are commonly compiled as either a series of nested if-else statements or with some type of look-up table. If your code does get compiled as a look-up table, it should perform faster, because only one conditional would be checked through each iteration of the loop. In your implementation, five conditional statements will execute with each iteration of the loop. You could also improve the performance of your code without a switch statement by using else if statements in place of each of the if statements after the first.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › help me with C programming