Below is my code and the instructions for the code. I can't figure out what's wrong. Please help.
Tasks
This lab has two parts:
Part 1 – Searching
Continuing with arrays, we will now expect you to find information in an array and derive useful properties from the information stored in an array.
|
0 |
1 |
2 |
3 |
4 |
|
5 |
-1 |
-5 |
-5 |
5 |
|
3 |
-2 |
56 |
25 |
-15 |
|
0 |
5 |
5 |
0 |
324 |
|
46 |
25 |
0 |
0 |
0 |
CODE BELOW:
import java.util.Scanner;
public class AssignmentOne {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] data = new int[5][5];
//setting values into the array;
System.out.println("Enter values for the array: ");
for (int row = 0; row < data.length; row++) {
for (int column = 0; column < data[0].length; column++) {
data[row][column] = scan.nextInt();
}
}
//printing array to screen
for (int row = 0; row < data.length; row++) {
for (int column = 0; column < data[row].length; column++) {
System.out.print(data[row][column] + " ");
}
System.out.println();
}
//calling the array;
int longestStreak = LongestPositiveSeries(data);
System.out.println("Longest positive streak: " + longestStreak);
//closing scanner;
scan.close();
}
public static int LongestPositiveSeries(int[][]myArray) {
int[][] array = new int[5][5];
int positiveStreak = 0;
int longestPosStreak = 0;
for (int row = 0; row < array.length; row++) {
for (int column = 0; column < array[0].length; column++) {
if (array[row][column] > 0) {
positiveStreak++;
//longestPosStreak++;
}
else {
if (longestPosStreak < positiveStreak) {
longestPosStreak = positiveStreak;
positiveStreak = 0;
}
positiveStreak = 0;
}
}
}
return longestPosStreak;
}
}
I saw your code and found that you were creating a new array (
whose name was array) inside the
LongestPositiveSeries function. So this was wrong, you
don't need to create a new array inside the
LongestPositiveSeries function.
Instead, you need to use the myArray (which has come as a
parameter to the LongestPositiveSeries function) inside
the for loop. You were creating and then using
array inside the for loop, but in the corrected
code I have used myArray inside for loop and
commented on the statement where you were creating the
array.
myArray contains the data entered by the user, you passed data array to the LongestPositiveSeries function and myArray is the receiving parameter, so it is obvious that you need to use myArray inside for loop and not array.
Corrected code:
import java.util.Scanner;
public class AssignmentOne {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] data = new int[5][5];
// setting values into the array;
System.out.println("Enter values for the array: ");
for (int row = 0; row < data.length; row++) {
for (int column = 0; column < data[0].length; column++) {
data[row][column] = scan.nextInt();
}
}
// printing array to screen
for (int row = 0; row < data.length; row++) {
for (int column = 0; column < data[row].length; column++) {
System.out.print(data[row][column] + " ");
}
System.out.println();
}
// calling the array;
int longestStreak = LongestPositiveSeries(data);
System.out.println("Longest positive streak: " + longestStreak);
// closing scanner;
scan.close();
}
public static int LongestPositiveSeries(int[][] myArray) {
// no need to use the below
statement
// int[][] array = new
int[5][5];
int positiveStreak = 0;
int longestPosStreak = 0;
// use myArray inside the for
loop
for (int row = 0; row <
myArray.length; row++) {
for (int column = 0; column < myArray[0].length; column++) {
if (myArray[row][column] > 0) {
positiveStreak++;
// longestPosStreak++;
}
else {
if (longestPosStreak < positiveStreak) {
longestPosStreak = positiveStreak;
positiveStreak = 0;
}
positiveStreak = 0;
}
}
}
return longestPosStreak;
}
}
Note: Please refer to the screenshot of the corrected code to understand the indentation of the code.
Screenshot of the corrected code:



Output and Input:

Below is my code and the instructions for the code. I can't figure out what's wrong....
This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...
I can't figure out how to change my code to write info in from a text file for the input. I'll post the context of the problem I am working on and the code I have finished so far. The program will create a Catapult object that will create a searchable matrix of trajectories for the given speeds and angles. The object should store these values in a 2D array. Given a target range of minimum and maximum distances, representing...
//please help
I can’t figure out how to print and can’t get the random numbers to
print. Please help, I have the prompt attached. Thanks
import java.util.*;
public
class
Test
{
/**
Main method */
public
static
void main(String[]
args)
{
double[][]
matrix
=
getMatrix();
//
Display the sum of each column
for
(int
col
= 0;
col
<
matrix[0].length;
col++)
{
System.out.println(
"Sum
" + col
+
" is
" +
sumColumn(matrix,
col));
}
}
/**
getMatrix initializes an...
The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...
C# Code please with picture of code.Thanks! 1. Create a Main method, and create a 2D integer array called data, with a size of five (5) by five (5). Ask the user to input the values for this array (Scanner’s nextInt) 2.Create a method called LongestPositiveSeries, that takes in a 2D array and finds the length of the longest continuous series of positive numbers in it. For example, if we had an array like this: 0 1 2 3 4...
Can someone explain this code with comments I am supposed to dispay an array an add each columns and add each row An application uses a two-dimensional array declared as follows: int[][] days = new int[29][5]; a. Write code that sums each row in the array and displays the results. b. Write code that sums each column in the array and displays the results. class TwoDimensionalArrayDemo { public static void main( String[] arg ) { int[][] days = new int[29][5];...
/** this is my code, and I want to invoke the method,
doubleArraySize() in main method.
and I need to display some result in cmd or terminal.
also, I have classes such as Average and RandomN needed for
piping(pipe).
please help me and thank you.
**/
import java.util.Scanner;
public class StdDev {
static double[] arr;
static int N;
public static void main(String[] args) {
if(args.length==0){
arr= new double[N];
Scanner input = new Scanner(System.in);
int i=0;
while(input.hasNextDouble()){
arr[i] =
i++;
}...
This is the contents of Lab11.java
import java.util.Scanner;
import java.io.*;
public class Lab11
{
public static void main(String args[]) throws IOException {
Scanner inFile = new Scanner(new File(args[0]));
Scanner keyboard = new Scanner(System.in);
TwoDArray array = new TwoDArray(inFile);
inFile.close();
int numRows = array.getNumRows();
int numCols = array.getNumCols();
int choice;
do {
System.out.println();
System.out.println("\t1. Find the number of rows in the 2D
array");
System.out.println("\t2. Find the number of columns in the 2D
array");
System.out.println("\t3. Find the sum of elements...
Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...