I am doing a 5 X 5 bingo card using C language . Obviuosly, there are 5 rows and
ID: 3592703 • Letter: I
Question
I am doing a 5 X 5 bingo card using C language. Obviuosly, there are 5 rows and 5 columns. What I need is to have random, non-repeating numbers per column.
Column 0 should have random, non repeating numbers from 1-15.
Column 1 should have random, non repeating numbers from 16-30.
Column 2 should have random, non repeating numbers from 31-45 (Note that this column must include the word "FREE" in the centeror third position)
Column 3 should have random, non repeating numbers from 46-60
& Column 4 should have random, non repeating numbers from 61-75.
I know how to do the grid, however, my main issue is creating random, non-repeating numbers per column. Any help will be greatly appreciated!
Explanation / Answer
Since you only needed help in generating random numbers, I'll give you a program only for it. What I've done in the program is that I've added (column number * 15) to the value so that 0th column produces numbers between 1-15 and 1st column produces numbers between 16-30 and so on. Also, you will see, I've used '%num' while generating random numbers in order to avoid repetition of the numbers.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int bingo[5][5];
int all[25]={0};
int row;
int col;
int num=15;
for(row=0;row<5;row++)
{
for(col=0;col<5;col++)
{
for(;;)
{
bingo[row][col]=rand()%num+(col*15) +1; //this line produces random numbers.
if(all[bingo[row][col]]==0)
{
all[bingo[row][col]]++;
break;
}
}
}
}
for(row=0;row<5;row++)
{
for(col=0;col<5;col++)
{
if(row==2 && col==2)
{
printf("FREE ");
}
else
printf("%d ", bingo[row][col]);
}
printf(" ");
}
return 0;
}
Note : please update the program as per your need, in order to add grid.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.