I am generating n x n (0,1) matrix. However I have no idea how to balance out 0 and 1 in the matrix. Each entry inside the matrix is either 0 or 1 such that every row have to contain exactly (n/2) 1's and (n/2) 0's. what code would you add to mine? I am in desperate help please.
so if I put the size 4 for matrix, initial 4 by 4 should look like this:
0011
0011
0101
0110
or any random location as long as it has (50:50) of (0,1) in each row
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<stdbool.h>
#define R 80
#define C 80
void initialization(int n, int a[R][C])
{
int i,j;
//int row_flag = 0;
for (i = 0; i<n;i++) {
for(j=0; j<n;j++) {
a[i][j] = rand()%2;
printf("%d", a[i][j]);
}
printf("\n");
}
}
int main()
{
srand(time(NULL));
int n=0;
int a[R][C]={0, };
printf("Enter the size of matrix, a positive even integer: ");
scanf("%d",&n);
if (n%2 ==0) {
initialization(n,a);
}
else {
return 0;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire