Why do Min & Max show up as 0 in this array?
I tried to figure it out for a while now but I couldn't wrap my head around it.
public class Main
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int []x=new int[n-1];
random(x);
sizeArray(x);
System.out.println("The min is "+ min(x));
System.out.println("The max is "+ max(x));
System.out.println("The Total is "+ getTotal(x));
}
public static int random(int[]x){
Random rand=new Random();
for (int i = 0; i < x.length; i++) {
x[i]=rand.nextInt(101);
}
return 0;
}
public static int sizeArray(int []x){
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
return 0;
}
Here is the 'min'.
public static int min(int[]x){
int min=0;
for (int i = 0; i < x.length; i++) {
if(x[i]<min){
min=x[i];
}
}
return min;
}
And the 'max' I am not sure what the issue is.
public static int max(int[]x){
int max=0;
for (int i = 0; i < x.length; i++) {
if(x[i]>max)
x[i]=max;
i++;
}
return max;
}
public static int getTotal(int[]x){
int total=0;
for (int i = 0; i < x.length; i++) {
total=total+x[i];
}
return total;
}
}```
||SOLVED||
Changed min and max and also added maximum(x); minimum(x); in main while removing ^^ System.out.println("The min is "+ min(x)); ^^
^^ System.out.println("The max is "+ max(x));.^^
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int []x=new int[n];
random(x);
sizeArray(x);
maximum(x);
minimum(x);
System.out.println("The Total is "+ getTotal(x));
}
public static int random(int[]x){
Random rand=new Random();
for (int i = 0; i < x.length; i++) {
x[i]=rand.nextInt(101);
}
return 0;
}
public static int sizeArray(int []x){
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
return 0;
}
|Changed max|
public static void maximum(int x[]) {
int max = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] > max) {
max = x[i];
}
}
System.out.println("maximum is:" + max);
}
|And changed min|
public static void minimum(int x[]) {
int min = x[0];
for (int i = 1; i < x.length; i++) {
if (x[i] < min) {
min = x[i];
}
}
System.out.println("minimum is:" + min);
}
public static int getTotal(int[]x){
int total=0;
for (int i = 0; i < x.length; i++) {
total=total+x[i];
}
return total;
}
}
Here you are probably setting every element in the array to 0. And in any case, max is always going to return 0
public static int max(int[]x){
int max=0;
for (int i = 0; i < x.length; i++) {
if(x[i]>max)
x[i]=max;
i++;
}
return max;
}
And here, you are starting with min = 0 (which isn't necessarily a value in the array) and since all the values are greater than 0, the return value is 0.
public static int min(int[]x){
int min=0;
for (int i = 0; i < x.length; i++) {
if(x[i]<min){
min=x[i];
}
}
return min;
}
And honestly, it would have been very easy or you to figure this out if you'd used the debugger to step through your functions. Learning how to use a debugger is an invaluable skill!
i want to get the index in the student to return
but it cannt compare . i donnt know what is the mintake here
would soome one figure it out
public int search(String[][] student, String searchKey) {
int search = 0;
int column = student.length;
int row = student[1].length;
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
System.out.printf("%s", student[i][j]);
if ((student[i][j]).equals(searchKey))
search = i;
}
}
return -1;
}
class FindKey
{
public int search(String[][] student, String searchKey) {
int search = 0;
int column = student.length;
int row = student[1].length;
for (int i = 0; i < column; i++) {
for (int j = 0; j < row; j++) {
if ((student[i][j]).equals(searchKey)){
System.out.println(j);
return j;
}
}
}
return -1;
}
public static void main(String[] args) {
FindKey search = new FindKey();
String[][] book ={{"1", "2"} , {"2", "4"}} ;
search.search(book, "2");
}
}
try this
I am interested in implementing the Rabin-Karp algorithm to search for sub strings. Here is the algorithm i used: algorithm.
The code I made for the same is as below.
But output is blank. It says build successfully but gives no output...
package rabinkarp;
public class RabinKarp {
final static int q = 101;
final static int d = 256;
public static void RabinKarpMatcher(String T,String p,int q,int d)
{
int n = T.length();
int M = p.length();
int H = 1,i,j;
for (i = 0; i < M-1; i++)
H = (H*d)%q;
int P = 0;
int t = 0;
for(i = 0;i < M; i++)
{
P = (d*P+p.charAt(i))%q;
t = (d*t+T.charAt(i))%q;
}
for(i = 0; i < n-M; i++)
{
if(P==t)
{
for(j = 0; j < M; j++)
{
if(T.charAt(i+j)!=p.charAt(j))
{
break;
}
}
if(j==M)
{
System.out.println("Pattern found at " + i+1);
}
}
if(i < n-M)
{
t = (d*(t-T.charAt(i)*H)+T.charAt(i+M))%q;
if(i <0)
{
t = t+q;
}
}
}
}
public static void main(String[] args) {
String text = new String("ababaaabhahhhha");
String pat = new String("ha");
RabinKarpMatcher(text,pat,q,d);
}
}
When I run, I get this:
run assignment1question1
2147483642
It should return '2', because I'm trying to find the number of unique elements in the intersection of the two arrays. Please help.
Thanks.
public class assignment1question1 {
public static void main(String[] args) {
int[] a = {1,3,2,3,5};
int[] b = {1,3,4,1,7,3};
int n = 5;
int m = 6;
System.out.print(listIntersection(a,b,n,m));
}
public static int listIntersection (int[] a, int[] b, int n, int m) {
int i,j,k;
int intersect = 0;
for(i=0; i<n; i++) {
int duplicate = 0;
for(j=0; j<=i; j++) {
if(a[i] == a[j]) {
duplicate = duplicate + 1;
}
}
if(duplicate == 1) {
for(k=0; k<m; m++) {
if(a[i] == b[k]) {
intersect = intersect + 1;
}
}
}
}
return intersect;
}
}
Here is my updated code:
public class assignment1question1 {
public static void main(String[] args) {
int[] a = {1,3,2,3,5};
int[] b = {1,3,4,1,7,3};
int n = a.length;
int m = b.length;
System.out.print(listIntersection(a,b,n,m));
}
public static int listIntersection (int[] a, int[] b, int n, int m) {
int i,j,k;
int intersect = 0;
for(i=0; i<n; i++) {
int duplicate = 0;
for(j=1; j<=i; j++) {
if(a[i] == a[j]) {
duplicate = duplicate + 1;
}
}
if(duplicate == 0) {
for(k=0; k<m; k++) {
if(a[i] == b[k]) {
intersect = intersect + 1;
break;
}
}
}
}
return intersect;
}
}
If you want to make your code working you will need to fix k for-cycle, where you iterate b array.
if(duplicate==1) {
for(k=0; k<m; k++) { // increase k
if(a[i] == b[k]) {
intersect = intersect + 1;
break; // insert break to avoid duplicates in b array
}
}
}
And here little bit cleaner and shorter code. (I assume, you are not allowed to use Java collection classes.)
public class assignment1question1 {
public static void main(String[] args) {
int[] a = {1,3,4,3,5};
int[] b = {1,3,4,1,7,3};
System.out.print(listIntersection(a,b));
}
public static int listIntersection(int[] a, int[] b) {
int intersect = 0;
for(int i = 0; i < a.length; i++) {
boolean duplicate = false;
for (int j = 0; j < i; j++) {
if(a[i] == a[j]) {
duplicate = true;
break;
}
}
if (!duplicate) {
for(int k = 0; k < b.length; k++) {
if (a[i] == b[k]) {
intersect++;
break;
}
}
}
}
return intersect;
}
}
You was stuck in the infinite loop becoz you increment the m value as m++ :
if(duplicate == 1) {
for(k=0; k<m; k++) {
if(a[i] == b[k]) {
intersect = intersect + 1;
}
}
}
import java.util.HashSet;
import java.util.Set;
public class Del {
public static void main(String[] args) {
int[] a = { 1, 3, 2, 3, 5 };
int[] b = { 1, 3, 4, 1, 7, 3 };
// duplicates removed by SET
Set intersect = new HashSet();
for (int aVal : a) {
for (int bVal : b) {
if (aVal == bVal) {
intersect.add(aVal);
System.out.println("->" + aVal);
}
}
}
System.out.println("Size =" + intersect.size());
}
}
import java.util.*;
public class HelloWorld {
public static void main(String []args) {
int[] a = {1,3,2,3,5};
int[] b = {1,3,4,1,7,3};
Set<Integer> aTmp = asSet(a);
aTmp.retainAll(asSet(b));
System.out.println(aTmp.size());
}
public static Set<Integer> asSet(int... args) {
Set<Integer> tmp = new HashSet<Integer>();
for (int i : args) {
tmp.add(i);
}
return tmp;
}
}
Ok, the title might be deceiving. All i want to do is take my Bingo program and when the second bingo card is printed, i want to replace all the "0"'s with "X"'s. I was thinking i would have to go and change the array to an string, but i'm not surer where to start.
Here is the Bingo program:
import java.util.*;
import java.io.*;
import java.util.Arrays;
public class Bingo
{
public static final int ROWS = 5;
public static final int COLS = 5;
public static final int VERTICAL = 1;
public static final int DIAGONAL = 2;
public static final int HORIZONTAL = 3;
public static int winFound;
public static int currPick = 0;
public static int randomPick = 0;
public static int WinFound;
public static void main(String[] args)
{
int Totcards;
int[][] card = new int[ROWS][COLS];
int[] picks = new int[25];
fillCard (card);
printCard(card);
playGame(card);
printCard(card);
finalCard(card);
}
private static void fillCard (int[][] card)
{
// FileReader fileIn = new FileReader("Bingo.in");
// Bufferreader in = new Bufferreader(fileIn);
try {
Scanner scan = new Scanner(new File("bingo.in"));
for (int i=0; i<card.length; i++){
for (int j=0; j<card[0].length; j++){
card[i][j] = scan.nextInt();
}
}
} catch(FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
}
}
private static void printCard (int[][] card)
{
System.out.println("\n\tYOUR BINGO CARD : ");
System.out.println("\n\tB I N G O");
System.out.println("\t----------------------");
for (int i=0; i<card.length; i++){
for (int j=0; j<card[0].length; j++){
System.out.print("\t" + card[i][j]);
}
System.out.print("\n");
}
}
private static void playGame (int[][] card)
{
int numPicks = 0;
System.out.println("\n\tBINGO NUMBERS PICKED AT RANDOM FROM BIN: ");
while (true)
{
markCard (card); // Generate a random num & zero-it out
winFound = checkForWin(card); // Look for zero sums
numPicks++;
if (winFound != 0)
{
if (winFound == 1)
{
System.out.print("\n\n\tYOU WIN WITH A VERTICAL WIN AFTER " + numPicks + " PICKS\n");
}
else if (winFound == 2){
System.out.print("\n\n\tYOU WIN WITH A DIAGONAL WIN AFTER " + numPicks + " PICKS\n");
}
else if (winFound == 3){
System.out.print("\n\n\tYOU WIN WITH A HORIZONTAL WIN AFTER " + numPicks + " PICKS\n");
}
announceWin (numPicks);
return;
}
}
}
private static void markCard (int[][] card)
{
int randomPick = (int) (Math.random() * 74) + 1;
for (int j = 0; j < ROWS; j++){
for (int k = 0; k < COLS; k++){
if (card[j][k]==randomPick)
card[j][k] = 0;}
}
System.out.print("\t " + randomPick + " ");
System.out.print("");
}
private static int checkForWin(int[][] card)
{
int sum=0;
for (int i = 0; i < ROWS; i++)
{
sum = 0;
for (int j = 0; j < COLS; j++)
sum += card[i][j];
if (sum == 0)
return HORIZONTAL;
}
for (int j = 0; j < COLS; j++)
{
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][j];
if (sum == 0)
return VERTICAL;
}
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][ROWS-i-1];
if (sum == 0)
return DIAGONAL;
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][i];
if (sum == 0)
return DIAGONAL;
return WinFound;
}
private static void makeCard(int[][] card, int[] picks)
{
int count = 100;
int currPick = 0;
for (int i=0; i<count; i++){
currPick = (int)(Math.random() * 74) + 1;
System.out.print(" " + currPick + "\n");
picks[i] = currPick;
}
}
private static void announceWin(int numPicks)
{
}
private static boolean duplicate (int currPick, int[] picks, int numPicks)
{
for (int i = 0; i < numPicks; i++){
if (picks[i] == currPick){
return true;}
}
return false;
}
private static void finalCard (int[][] card)
{
Arrays.sort(card);
final String stringRep = Arrays.toString(card);
final String[] out =
stringRep.substring(1, stringRep.length() - 1).split("\\s*,\\s*");
System.out.println(Arrays.toString(out));
}
}
Try this:
System.out.print("\t" + (card[i][j] == 0 ? "X" : card[i][j]))
Why don't you just use a String[][] for the fields from the beginning? You can still compare String values with ints (with Integer.valueOf for instance) and this way you don't have to switch types runtime...