Thread: Arrays and Loops

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Arrays and Loops

    Hi im very new to C programming and I was writing a program to generate two random numbers who's answer the user has to guess, and the program will tell if the answer is right or wrong. While I have managed to do that, I have no clue how to produce a list of 10 random multiplication questions the user has to guess. Here is the code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void) {
    int n1, n2;
    long prd, guess;
    
    srand((unsigned int)time(NULL));
    
    n1 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
    n2 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
    prd = n1 * n2;
    
    printf("What is the product of %d and %d\?\n"
    "[Enter -1 to exit]\n", n1, n2);
    
    if(scanf("%ld", &guess) != 1) {
    puts("Incorrect input.");
    return EXIT_FAILURE;
    }
    else if(guess == -1) {
    puts("bye.");
    }
    else if(guess == prd) {
    puts("Correct!");
    }
    else {
    puts("Wrong guess.");
    }
    return 0;
    }
    I know to generate a list I have use loops and arrays but I'm completely clueless about it.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read some tutorials. Give it a shot and come back with your attempt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays and loops
    By pmooney12 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 10:38 PM
  2. Need help with for loops and arrays
    By Skeeter in forum C Programming
    Replies: 6
    Last Post: 10-23-2009, 03:33 PM
  3. Help with Arrays and For loops
    By Phil- in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2009, 10:34 AM
  4. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 04:09 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM