Question

In: Computer Science

Write a program in java to determine whether a given function F from the set {0,1,2,3,...,m}...

Write a program in java to determine whether a given function F from the set {0,1,2,3,...,m} to (0,1,2,3,...,n} is one-to-one and/or onto. The inputs to your program will be:

- an integer m

- an integer n, and

- a two dimensional boolean array F of size (m+1) x (n+1), where F[ i , j ] = 1 if F(i) = j.

Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main
{
public static boolean checkOneToOne(int m, int n, int F[][]) {
// check if every column has less than or equal to one 1.
for (int j = 0; j <= n; j++) {
int count_one = 0;
for (int i = 0; i <= m; i++) {
count_one += F[i][j];
}
if (count_one > 1)
return false;
}
return true;
}
  
public static boolean checkOnto(int m, int n, int F[][]) {
// check if every column has atleast one 1.
for (int j = 0; j <= n; j++) {
int count_one = 0;
for (int i = 0; i <= m; i++) {
count_one += F[i][j];
}
if (count_one == 0)
return false;
}
return true;
}
  
   public static void main(String[] args) throws IOException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter m: ");
       int m = Integer.parseInt(br.readLine());
       System.out.print("Enter n: ");
       int n = Integer.parseInt(br.readLine());
       int F[][] = new int[m + 1][n + 1];
       for (int i = 0; i <= m; i++) {
       for (int j = 0; j <= n; j++) {
       System.out.print("Enter F[" + i + ", " + j +"]: ");
       F[i][j] = Integer.parseInt(br.readLine());
       }
       }
       if (checkOneToOne(m, n, F))
       System.out.println("Function is one-to-one.");
       else
       System.out.println("Function is not one-to-one.");
       if (checkOnto(m, n, F))
       System.out.println("Function is onto.");
       else
       System.out.println("Function is not onto.");
       br.close();
   }
}


Related Solutions

3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
In Java, a set of integers is given. write a function to find 2 integers in...
In Java, a set of integers is given. write a function to find 2 integers in this set that sums upto a target value. i.e [1,5,2,0,11,3] target = 7 result [5,2] i.e [1,5,4,0,14,-7] target = 9 result [5,4] NOTE: THE SAME INTEGER CANNOT BE USED TWICE !!!
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
1.            Determine whether the function f from { a, b, c, d } to {a,...
1.            Determine whether the function f from { a, b, c, d } to {a, b, c, d, e} is injective (one-to-one), surjective (onto) and/or bijective (one-to- one correspondence) : f(a) = a,            f(b) = c,            f(c) = b, f(d) = e a. Is this function injective?              . surjective?              . bijective?              . If your answer is no for any of the above, explain:             b. Is there an inverse for this function?              . c. Is the composition f...
For the following exercises, determine whether or not the given function f is continuous everywhere...f(x) = log2 (x)
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = log2 (x)
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
For the following exercises, determine whether or not the given function f is continuous everywhere...f(x) = tan(x) + 2
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = tan(x) + 2
For the following exercises, determine whether or not the given function f is continuous everywhere....f(x) = 2x + 5/x
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = 2x + 5/x
For the following exercises, determine whether or not the given function f is continuous everywhere....f(x) = sec(x) − 3.
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = sec(x) − 3.
you are to write a program in Java, that reads in a set of descriptions of...
you are to write a program in Java, that reads in a set of descriptions of various geometric shapes, calculates the areas and circumferences of the shapes, and then prints out the list of shapes and their areas in sorted order from smallest to largest area. There are four possible shapes: Circle, Square, Rectangle, and Triangle. The last is always an equilateral triangle. The program should read from standard input and write to standard output. The program should read until...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT