How to increment counters based on a printed array

Posted by Sam Liew on Stack Overflow See other posts from Stack Overflow or by Sam Liew
Published on 2013-10-21T15:46:12Z Indexed on 2013/10/21 15:53 UTC
Read the original article Hit count: 291

Filed under:
|
|
|
|

I managed to developed a simple board of 5x5 using random numbers and array. Big achievement for someone like me! :)

Now I have to increment the counters depending on the frequency of the numbers.

If the value within 0-49 is printed..then nCounter++ If the value within 50-75 is printed..then pCounter++ something like that.

The problem is that I don't know how to increase the counters based on the printed board.

Here is the code:

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

int main()

{
//Initialize Variables
int randomNumber;
int rows;
int columns;
int hdCounter =0;
int hCounter = 0;
int cCounter = 0;
int pCounter = 0;
int nCounter = 0;

//Declare board size
int board[5][5]; //size of board is 5 x 5

//Create the random number generator seed
srand(time(NULL));

//Assign the random numbers from 1 - 25 into variable randomNumber

//Create the rows for the board
for ( rows = 1; rows <= 5 ; rows++ )
{
    //Create the colimns for the board
    for ( columns = 1; columns <= 5 ;  columns++ )
    {
        //Assign variable randomNumber into variable board
         randomNumber = rand() %100 + 1;

         board[rows][columns] = randomNumber;

         //print the board
         printf("%d\t", board[rows][columns]);

        //calculate the frequency of numbers on the printed board
        if (randomNumber >= 85 && randomNumber <= 100 )
            hdCounter++;
        else if ( randomNumber >= 75 )
             hCounter++;
        else if ( randomNumber >= 65 )
             cCounter++;
        else if ( randomNumber >= 50 )
             pCounter++;
        else if ( randomNumber >= 1 )
             nCounter++;
        else
            continue;
    }
    //Newline after the end of 5th column.
    printf("\n\n");


}

    printf( "N \t P \t C \t H \t HD\n\n" );

    printf("%d \t %d \t %d \t %d \t %d \t",
           nCounter, pCounter, cCounter, hCounter, hdCounter);

 }//end main

I tried replacing randomNumber in the if-statement with board[rows][columns] but I seem to get the same undesired results.

© Stack Overflow or respective owner

Related posts about c

    Related posts about arrays