Returning Arrays from Methods - arrays

So I have to display the marks that are below average, and all the marks that are above average.
I don't know where to go with this next but here's what I have so far:
public static void main (String[]args) {
int[]marks={50,60,30,90,70,40,80,44};
int average=average(marks);
int[]resultBelowAverage=getBelowAverage(marks,average);
printIntArray(resultBelowAverage);
int[]resultAverageandAbove=getAverageandAbove(marks,average);
printIntArray(resultAverageandAbove);
}
This next method is used to calculate the average. I got the right answer for this one which is 58.
public static int average(int[] marks) {
int i=0;
int total=0;
for(i=0;i<marks.length;i++){
total=total+marks[i];
}
int average=total/i;
return average;
}
This next method gets how many values in the array are below average.
public static int countBelowAverage (int[] numbers,int av){
int size=0;
for(int i=0;i<numbers.length;i++) {
if (numbers[i]<av) {
size=size+1;
}
}
return size;
}
This next method gets how many values in the array are above average.
public static int countAboveAverage (int[] numbers,int av) {
int size=0;
for(int i=0;i>=numbers.length;i++) {
if (numbers[i]>=av) {
size=size+1;
}
}
return size;
}
This next method gets which values in the array are below average. I think this is where I went wrong.
public static int[] getBelowAverage(int[]numbers,int av) {
int size=countBelowAverage(numbers,av);
int[]belowAverage=new int[size];
return belowAverage;
}
This next method gets which values in the array are above average.
public static int[] getAverageandAbove(int[] numbers,int av) {
int size=countAboveAverage(numbers,av);
int[]AboveAverage=new int[size];
return AboveAverage;
}
This last method prints the arrays that are classified into below average and above average.
public static void printIntArray(int[]x) {
for(int i=0;i<x.length;i++){
System.out.println (x[i]);
}
}

In your method getAverageandAbove you make an array, but you do not put anything in it.
you should loop through your numbers array again, and fill your AboveAverage array with the values that are greater than the average.

Try something like this to get the below average array
public static int[] getBelowAverage(int[]numbers,int av){
int size=countBelowAverage(numbers,av);
int[]belowAverage=new int[size];
int j = 0;
for (int i = 0; i < numbers.length; i++){
if (numbers[i] < av){
belowAverage[j] = numbers[i];
j++;
}
}
return belowAverage;
}
Do similar for the above average method

In your methods you just creating arrays of proper size, an empty arrays and returning them. You should put some marks that are below / above average to these arrays before returning
Like here:
int[]belowAverage=new int[size];
//you have an empty array just after creation with nothing inside
//enter here marks from the marks array that are below average
return belowAverage;
Exactly the same situation here:
int[]AboveAverage=new int[size];
//you have an empty array just after creation with nothing inside
//enter here marks from the marks array that are above average
return AboveAverage;
You may use an algorithm from peeskillet's solution to do it.

Related

TWO SUM PROBLEM IN JAVA - I coded the below code but unable to find the problem in this

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Hi Team,
Above is my problem statement and below is the code i coded.
Online Java Compiler.
Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
public class Main {
public static void main(String[] args) {
int[] nums={2,7,8,0};
int target=9;
int s=0;
for(int i=0;i<nums.length;i++)
{
for(int j=i+1;j<nums.length;j++)
{
s=s+nums[i][j];
}
if(s==target)
{
System.out.print("["+i+","+j+"]");
}
}
}
}
I am unable to understand what's the problem with compilation ,could anyone help!->Main.java:20: error: array required, but int found s=s+nums[i][j]; it shows this errorenter code here
In the line s=s+nums[i][j]; you are accessing nums as 2D array.
But you defined the variable nums as a 1D array.
Reply if you need more help.
public class Solution {
public int[] twoSum(int nums[],int target) {
int arr[]=new int[2];
System.out.print("Array Elements : [");
for(int i=0;i<nums.length;i++){
System.out.print(nums[i]+" ");
}
System.out.println("]");
System.out.println("Target : "+target);
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j] == target){
arr[0]=i;
arr[1]=j;
System.out.println("["+i+","+j+"]");
}else if(i==0 && j==0){
arr[0]=i;
arr[1]=j;
}
break;
}
}
return arr;
}
}
}
public int[] twoSum(int[] nums, int target) {
int[] indexes = new int[2];
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(target ==nums[i]+nums[j]){
indexes[indexes.length-2] =i;
indexes[1] = j;
}
}
}
return indexes;
}

Given an array in which arr[i] = i-1 with the following method, what will be the output?

I'm given the following method written in pseudo-code
for i=1 to floor(n/2)
if arr[i] != 0 then
for(j=2 to floor(n/i)
arr[i*j] = 0
I need to find the output and to prove that it's indeed the output.
So far I tried to write the code in Java and to try different inputs and array sizes but to no avail.
Putting it here if it's of any help:
public class Checking
{
private static int method(int[] A,int n)
{
for (int i=1;i<=java.lang.Math.floor(n/2);i++)
{
if(A[i] != 0)
{
for(int j=2;j<=java.lang.Math.floor(n/i);j++)
{
A[i*j]=0;
System.out.println("The index ofA["+i*j+"] became "+A[i*j]);
}
}
//System.out.print(", "+A[i]);
}
for (int i=1;i<=java.lang.Math.floor(n/2);i++)
{
System.out.print(", "+A[i]);
}
return 0;
}
public static void main(String[] args)
{
int[] A = {-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};
System.out.println(method(A,20));
}
}
Thank you.
You can change the following:
In your main method remove
System.out.println(method(A,20));
as it will always print 0 as you return 0; from the method().
As you print everything in your method() change the following (as it does not print the full array but from index 1 (missing index 0) to floor n/2)
for (int i=1;i<=java.lang.Math.floor(n/2);i++)
{
System.out.print(", "+A[i]);
}
to
for (int i=0;i<A.length; i++)
{
System.out.print(", "+A[i]);
}
So you can print the whole array.
For simplicity you can use Array's print method to print the array without a for loop
System.out.println(Arrays.toString(A));

Have to create 2d array (4x4) array and has to fill the diagnal with 1s and rest of the spaces with 0s

public class sampleq8
{
public static void main(String args[])
{
int size =4;
int array[][]=new int[size][size];
for(int i=0; i<array.length;i++)
{
for(int j=0;j<array.length;j++)
{
array[0][0]=1;
}
//array[4][4]=1;
// array[
System.out.println(array[i][i]);
}
}
}
i got to print out the first column correctly i cant produce the rest of the rows or the columns i need some suggestions please thanks.
Java initializes all the values of your array to 0 automatically, you only need to assign values to the diagonal.
int size =4;
int array [][] = new int[size][size];
for (int i =0;i<size;i++){
array[i][i]=1;
}
System.out.println(Arrays.deepToString(array));
This does the job.
In your version you are only setting the value 1 to the element with coordinates 0,0 in your array
To print your array you need to import java.util.Arrays;
This one will print the matrix nicely
for (int i =0 ;i<4;i++){System.out.println(Arrays.toString(array[i]));}
Your assignment is always using the 0,0 element.
array[0][0]=1
You need to set the assignment like this:
array[i][j]=1
try something like this, dont know if it works, i cant run it right now
for(int i = 0; i<size; i++)
{
array[i][i] = 1;
}
for(int i=0; i<array.length;i++)
{
for(int j=0;j<array.length;j++)
{
if(array[i][j] != 1)
array[i][j]=0;
}
}

How do I calculate median from an array from a different class?

I have an array of values in a different class. I am trying to calculate max, min and median from this array. I have my methods created but all values still display "0" when I run the program. Can someone please point out what I am doing wrong?
import java.util.*;
public class SortedListStats
{
public int maxTemp;
public int minTemp;
public int medianTemp;
public SortedListStats()
{
this.maxTemp = maxTemp;
this.minTemp = minTemp;
this.medianTemp = medianTemp;
InputOutput io = new InputOutput(); //This is my other class that has the array.
}
public int maxTemp(int[] io) //method to determine maximum temperature
{
int max = io[0];
for(int i = 0; i < io.length; i++)
{
if(max < io[i])
max = io[i];
}//end for statement
return max;
}//end method maxTemp
public int minTemp(int[] io) //method to determine mininum temperature
{
int min = io[0];
for(int i = 0; i < io.length; i++)
{
if(min > io[i])
min = io[i];
}//end for statement
return min;
}//end class minTemp
public int medianTemp(int[] io)
{
Arrays.sort(io);
int sum = 0;
int count = 0;
int median;
for(int i = 0; i > io.length; i++)
{
sum = sum + io[i];
count++;
}
median = sum/count;
return median;
}//end method medianTemp
}//end class SortedListStats
What code are you using to call the methods on this class? The first three lines of the constructor don't do anything. If you're expecting them to call your calculation methods, then they need to be after "new InputOutput()" and they need to be function calls. Like:
this.maxTemp = maxTemp(io.getValues());
You should refrain from naming your functions the same as your member variables to avoid confusion. For instance, the "maxTemp()" function could be "calculateMaxTemp()" instead.

Passing a 2d array and using recursion error

I am doing an assignment and have trouble getting the code compiled. The code must have recursion. The idea is to pass a 2d array sequentially - row by row, column by column and to get a private helper method to match cases and return the result.
I have tried looking at different solutions and everything seems in order, however I get an error:
required: int
found: int[][]
I am passing an array, int row and int column to a method that should be accepting exactly those three.
Take a look:
public static int [][] calculateProximity ( boolean [][] mineField )
{
int [][] proximityField = new int [mineField.length][mineField[0].length];
for (int row = 0; row < mineField.length; row++) {
for (int column=0; column <mineField[row].length; column++) {
proximityField [row][column] = calculateProximity (mineField, row, column);
}
}
return proximityField;
}
private static int [][] calculateProximity (boolean [][] mineField,
int row, int column)
{
int [][] proximityField;
if (row >= mineField.length || column >= mineField[row].length){
return proximityField=0;
}
else if (mineField [row][column]= true){
proximityField[row][column]=1;
return proximityField;
}
else
{
proximityField[row][column]=0;
}
return proximityField;
}
By the way, calculateProximity main method is to return an int 2d array, but it is given a boolean 2d array.
return proximityField=0;, is incorrect. Here, you are returning the result of proximityfield=0. This is going to be a single integer, the value 0. When you assign a variable, the return value is the value you assigned to the variable. You should probably return null instead.
Never use loops while performing recursion. I hope this code gives you some idea:
#include<stdio.h>
#include<stdlib.h>
int i=0,j=0,k=0,flag=0;
int function_Matrix(int arr[][4],int i,int j)
{
if(j==4||i==3)
return 1;
if(flag==0)
{
printf("\t%d",arr[i][j]);
}
function_Matrix(arr,i,j+=1);
printf("\n");
function_Matrix(arr,i+=1,j=0);
flag=1;
}
int main()
{
int x;
int arr[][4]={{1,2,3,4},
{5,6,7,8},
{9,7,6,5}};
function_Matrix(arr,0,0);
}

Resources