Categories

# Fill Matrix with Zeroes and Ones Row-wise

Write a program in Java to create a matrix a[][] of order (m × n).

Here, ‘m’ is the number of rows and ‘n’ is the number of columns.

Note that the values of both ‘m’ and ‘n’ must be greater than 2 and less than 10.

Now fill each row of the matrix uniformly by 1 or 0 alternately.

Sample Input:
M=3
N=3
Resultant matrix:
1 1 1
0 0 0
1 1 1

``````import java.io.*;
class Alternate{
public static void main(String args[])throws IOException{
System.out.print("m: ");
int m = Integer.parseInt(br.readLine());
System.out.print("Columns: ");
int n = Integer.parseInt(br.readLine());
if(m < 3 || n < 3 || m > 9 || n > 9){
System.out.println("Invalid range!");
return;
}
int a[][] = new int[m][n];
int value = 1;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
a[i][j] = value;
}
if(value == 1)
value = 0;
else
value = 1;
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}``````

## 2 replies on “Fill Matrix with Zeroes and Ones Row-wise”

sayan ranasays:

A class stringop is designed to handle string related operations. Some members of the class are given
below:
Date member
txt : to store the given string of maximum length 100.
Member function
stringop() : constructor
void readstring() : to accept the string
char caseconvert(int, int): to convert the letter to other case
void circular decode() : to decode the string by replacing each letter by converting it
to opposite case and then by the next character in a circular
way. Hence “AbZ” will decode “bCa”.
Specify the class giving details of all member functions . You need to write function main().