readNumberAsArray assignment [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Write a readNumberAsArray method that takes an integer as a parameter and creates a new int array with that number as the length. Subsequently, a corresponding number of int values ​​should be read in with the aid of the IOTools and the array filled with them returned, whereby only single-digit numbers (0-9) should be taken into account as input. If the parameter is negative, the method should return null. For negative or two-digit value entries, the entered value should be replaced by 0. Use a for loop to read in the values. A text output when using the IOTools is not necessary.
My program is not working.
import Prog1Tools.IOTools;
package com.company;
public class Main {
public static void readNumberAsArray(int a) {
int [] a = new int[];
int a = IO.Tools.readInteger () ;
for int (a = 0 ; a<10 ; --a) {
System.out.println('0');
for (int a=0; a>10; a++) {
System.out.println(a);
for (int a=10; a=>10; a++) {
System.out.println('0');
}
}
}
// write your code here
}
}

I think you have to just implement what you're asked. Step by step. There's no special logic.
public int[] readNumberAsArray(int n) {
// negative or two-digit values should be replaced with 0
if (n <= 0 || n > 9)
return new int[0];
// creates a new int array with that number as the length
int[] arr = new int[n];
// corresponding number of int value should be read in with the aid of the IOTools
for (int i = 0; i < arr.length; i++)
arr[i] = IO.Tools.readInteger();
return arr;
}

Related

How do I debug arrays? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I have this assignment that I am having trouble on. I'm supposed to fix syntax error in the code to produce the desired number. I fixed the amount of arrays from 4 to 3 and added "[]" to the end of array in the for loop. I don't know what else there is to fix. Can anyone help?
//
// Fix the compiler errors. The program should display the value 6.
//
package debug5;
public class debug5 {
public static void rain(String[] args) {}
int val = 0; // initialize val to 0.
int array[] = new int[3]; // create an array of 3 integers.
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
// add up the values in the array.
for (int zx = 0;zx < array.length;zx++)
}
val += array;
{
system.out.println(val);
}
}
My version :
//
// Fix the compiler errors. The program should display the value 6.
//
package debug5;
public class debug5 {
public static void rain(String[] args) {}
int val = 0; // initialize val to 0.
int array[] = new int[3]; // create an array of 3 integers.
array[0] = 1;
array[1] = 2;
array[2] = 3;
// add up the values in the array.
for (int zx = 0;zx < array.length;zx++)
}
val += array[];
{
system.out.println(val);
}
}
In addition to Rafael's answer, be careful with braces and where you'd like to system out.
public static void rain(String[] args) {
int val = 0; // initialize val to 0.
int[] array = new int[3]; // create an array of 3 integers.
array[0] = 1;
array[1] = 2;
array[2] = 3;
//add up the values in the array.
for(int zx = 0;zx < array.length ;zx++){
val += array[zx];
}
System.out.println(val);
}
Above code will increment val with each value inside the array and sum inside val variable. Print is done afterwards.
There were some issues with your array initialization and use of arrays and indexes. I suggest you read a bit more on this because this is important on programming
Finally, a better name for the index variable would be i or index instead of zx.
Try this:
val += array[zx];
You have to use the index of the array.

Program to find the maximum product of two numbers in a given integer array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Kindly help me with the underlying program as I am stuck. I'm a newbie programmer.
import java.util.*;
public class Source
{
static int maxProduct(int arr[]) {
int n = arr.length ;
if (n < 2)
{
System.out.println("NA");
return Integer.MIN_VALUE;
}
int a = arr[0];
int b = arr[1];
for(int i = 0; i<n; i++) {
for (int j = i+1; j<n; j++) {
if (arr[i]*arr[j] > arr[0]*arr[1]) {
a = arr[i];
b = arr[j];
}
}
}
return maxProduct;
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[] arr = new int[size];
for(int i = 0; i < size; i++) {
arr[i] = s.nextInt();
}
int answer = maxProduct(arr);
System.out.print(answer);
}
}
You should change
if (arr[i]*arr[j] > arr[0]*arr[1])
to
if (arr[i]*arr[j] > a * b)
Since arr[0]*arr[1] is just the original max product, so you shouldn't be comparing against it.
Also note that your solution is not as efficient as it can be, since you are using a nested loop, which requires O(n^2) running time.
You can achieve linear (O(n)) running time if you use the fact that the max product is either the product of the two highest positive values or the product of the two lowest negative values. This means that if you find these 4 numbers, which can be done with a single loop, you'll find the max product.

Minimum value of an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I would like to write a program that reads an array of integers and finds the minimum value.
The first line contains the size of an array.
Output
An integer number representing the minimum in the input array.
Input:
5
5 1 4 2 3
My code:
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int[] c = new int[a ];
for (int i = 0; i < c.length; i++) {
c[i] = sc.nextInt();
}
Arrays.sort(c);
int b= c[0] > 0 ? c[0]: c[c.length -1];
for (int i =1; i < c.length - 1; i++){
if(c[i] < 0){
if(c[i] > b){
b = c[i];
}
}
else{
if (c[i] < b){
b= c[i];
}
}
}
System.out.println(b);
}
}
My output:
3
-1 -2 -3
What is wrong with my code?
You are making it more complicated than necessary. No need to sort the array. Just set a variable to be equal to the first value in the array. Then scan through the array setting the variable to the actual array element if it is less than the present variable value.

To use method from the main method java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This question is pretty specific for my problem, that is why I am creating a new question. The second method in this program is supposed to make a row of the number 1 2 3 4 5 6 7 8 9 10. The only problem I am having is that I don't know how to print this out in the main method.
public class Uppgift1_6a
{
public static void main(String[] args)
{
for(int k = 0; k < 10; k++)
{
int tal = Numberline(k);
System.out.print(tal);
}
}
public static int Numberline(int tal1)
{
int tal = 1;
for(int i = 1; i < 11; i++)
{
tal = tal1 + i;
}
return tal;
}
}
Right now it prints out all the number from 11 to 19. And if I change it, it only prints out either 10 or 11.
Look closely at the code:
public static int Numberline(int tal1)
{
int tal = 1;
for (int i = 1; i < 11; i++)
{
tal = tal1 + i;
}
return tal;
}
The for loop literally does absolutely nothing - you're only returning the final result. The final result is always exactly equal to tal1 + 10; again, what the for loop did up this point makes no difference. (I'd encourage you to step through the code with a debugger to convince yourself of that fact).
If you want it to print out the values as you're going through the for loop, you need to do something like:
for (int i = 1; i < 11; i++)
{
// You may need to modify this line too, depending on what values you want printed
tal = tal1 + i;
// Print the value here
System.out.print(tal);
}
because the way you've written it it'll only print out the final value of tal (the one you returned).

pseudocode to Put Even & Odd Elements of an Array in 2 Separate Arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Write pseudocode to Put Even & Odd Elements of an Array in 2 Separate Arrays
import java.util.Scanner;
public class InsertElementInArray
{
public static void main(String[] args)
{
int n, pos, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n+1];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the position where you want to insert element:");
pos = s.nextInt();
System.out.print("Enter the element you want to insert:");
x = s.nextInt();
for(int i = (n-1); i >= (pos-1); i--)
{
a[i+1] = a[i];
}
a[pos-1] = x;
System.out.print("After inserting:");
for(int i = 0; i < n; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n]);
}
}
Code and pseudocode both are heading in same way, but the main difference is the second one is much easier to write and understand for humans.
Let's take an example: We have a function that takes an string array as an argument and performs some complicated operations like searching for some specified char chains or looking for a pattern with regex.
It can looks very simply in pseudocode:
function doLotsOfStuff(String array):
variable patternApperance
for each string in array:
if (string has "PATTERN"):
increment patternApperance
return patternApperance
The thing about pseudocode is that it doesn't have any specific way or convention of writing. It's something where you don't have to care if it is going to compile, parse etc. but only care about that if others understand your pseudocode. In short words pseudocode isnt made for computers, it's for humans to better understand what a piece of code is going to do.

Categories

Resources