c programming 1

floating point exception the reason is divided by zero.

after a afternoon's hard working, i get it ,so be aware of the variables that has different affect in different areas.

#include<stdio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
int genQuestion(int);
int answerQuestion(int);
void response(int);
int main(){
         int i;
         int num;
         int dif;
         int ans;
        int count;
         srand(time(NULL));
         while(num<1||num>20)
        {
        printf("How many questions do you want : from (1-20)\n");
        scanf("%d", &num);
        }
        while(dif<1||dif>4)
        {
        printf("How many difficulty do you want: from (1-4)\n");
        scanf("%d", &dif);
        }
        for (i=0;i<num;i++)
        {
        int flag=0;
        printf("%s%d\n","Question",i+1);
        ans=genQuestion(dif);
        flag=answerQuestion(ans);
        response(flag);
        if(flag==1)
        count++;
        }
        printf("%s%d/%d\n","Your score is",count,num);
        return 0;
}

int genQuestion(int dif)
{
        int t=0;
        int opr1=0;
        int opr2=0;
        char a;
        int ans=0;
        t=rand()%4;
        if(t==0)a='+';
        if(t==1)a='-';
        if(t==2)a='*';
        if(t==3)a='/';
         switch (dif){
         case 1: opr1=rand()%10;
          opr2=rand()%10;
          break;
          case 2: opr1=rand()%50;
          opr2=rand()%50;
           break;
           case 3:opr1=rand()%100;
            opr2=rand()%100+1;
            break;
            case 4:opr1=rand()%200-100;
            opr2=rand()%200-100;
            break;
            }
  switch(t){
  case 0:ans=opr1+opr2;
  break;
  case 1:ans=opr1-opr2;
  break;
  case 2:ans=opr1*opr2;
  break;
  case 3:ans=opr1/opr2;
  break;
  }
printf("%d%c%d%c%c\n",opr1,a,opr2,'=','?');
return ans;
}

int answerQuestion(int ans){
        int flag=0;
        int out=0;
        printf("%s\n","Enter answer");
        scanf("%d",&out);
        if(ans==out)
        flag=1;
        else
        printf("%s%d\n","The correct answer is",ans);
        return flag;
}

void response(int flag){
        char *repg[]={"nice","great job","well done"};
        char *repb[]={"try again","oh,no","you can do it"};
        if (flag==1)
        printf("%s\n",repg[rand()%3]);
        else
        printf("%s\n",repb[rand()%3]);
}

srand()can't be included in the for loop


Executable files: Green
* Normal file : Normal
* Directory: Blue
* Symbolic link : Cyan
* Pipe: Yellow
* Socket: Magenta
* Block device driver: Bold yellow foreground, with black background
* Character device driver: Bold yellow foreground, with black background
* Orphaned syminks : Blinking Bold white with red background
* Missing links ( … and the files they point to) : Blinking Bold white
with red background

* Archives or compressed : Red (.tar, .gz, .zip, .rpm)
* Image files : Magenta (.jpg, gif, bmp, png, tif)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章