You are here:

C/using rand

Advertisement


Question
i am suppose to write a c progam that should multiply 2 one digit number using rand it should ask the user multiplication 10 times
then display each time if the answer the user enter is correct or incorrect using rand so if its incorrect u can display out of 4 response eg bad,very bad not that bad ,real bad and it should also display an average of correct answer i have written this so far but i dont know what to do next.

#include<stdio.h>
#include<stdlib.h>

void main ()
{
  int ans[];
int i,correct;


for(i=1;i<11;i++){

  printf("How much is %d times %d ?",1+rand()%9,1+rand()%9);
  scanf("%d",&ans[]);

  correct=(1+rand()%9*1+rand()%9);
  avg=


  if(ans==correct){
  printf("good\n");
  }
else
printf("bad\n");
}

}  

Answer
First of all, you have to store the random number in a variable. And remember that, each time you call the rand() function, a new number is returned. Also, you have to use srand() function to initialize the randomizing algorithm.

My sincere suggestion to you is, write an algorithm in plain english (splitting into steps) for what you intend to do, before starting to code. Then only you can code easily.

ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.