Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Created December 13, 2018 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/906c45553d989d4af4216a6067fc5310 to your computer and use it in GitHub Desktop.
Save bytecodeman/906c45553d989d4af4216a6067fc5310 to your computer and use it in GitHub Desktop.
CSC-111 Solution to the Even Ones Homework
/*
* Name: Prof. A.C. Silvestri
* Date: 12/3/2018
* Course Number: CSC-111
* Course Name: Introduction to Java Programming
* Problem Number: Two Dimensional Array Assignment
* Email: silvestri@stcc.edu
* Description: Fix the program from the titled video "LiangCh08 - Even Number Of Ones," to run in a loop until it finds the correct solution
*/
import java.util.Scanner;
public class EvenOnes {
private static void report(int[][] matrix, int count) {
if (!hasEvenOnes(matrix))
System.out.printf("Could not find a solution in %d attempts\n", count);
else {
System.out.println("Matrix:");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++)
System.out.print(matrix[i][j]);
System.out.println();
}
System.out.println("Number of iterations to generate: " + count);
}
}
// ************************************************************************
private static boolean hasEvenOnes(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
int oneCount = 0;
for (int j = 0; j < matrix[i].length; j++)
if (matrix[i][j] == 1)
oneCount++;
if (oneCount % 2 != 0)
return false;
}
for (int j = 0; j < matrix[0].length; j++) {
int oneCount = 0;
for (int i = 0; i < matrix.length; i++)
if (matrix[i][j] == 1)
oneCount++;
if (oneCount % 2 != 0)
return false;
}
return true;
}
// ************************************************************************
private static void generateMatrix(int row, int col) {
final int MAXTRIES = 10000;
int matrix[][] = new int[row][col];
int iterationCount = 0;
do {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++)
matrix[i][j] = (int) (Math.random() * 2);
}
iterationCount++;
} while (!hasEvenOnes(matrix) && iterationCount < MAXTRIES);
report(matrix, iterationCount);
}
// ************************************************************************
private static void process(Scanner sc, String[] args) {
System.out.println();
System.out.print("How many rows would you like in the matrix? ");
int row = sc.nextInt();
System.out.print("And how many columns? ");
int col = sc.nextInt();
generateMatrix(row, col);
sc.nextLine();
}
// ************************************************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
// ************************************************************************
public static void main(String[] args) {
final String TITLE = "Even Number Of Ones Matrix Generator V1.0";
final String CONTINUE_PROMPT = "Generate another matrix? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment