How can i call runningSum method in main with new array of intigers so it return result?
class Main {
public static void main(String[] args) {
}
public int[] runningSum(int[] nums) {
int[] result = new int[nums.length];
result[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
result[i] = result[i - 1] + nums[i];
}
return result;
}
}
I have no idea how to call it.
Add static modifier to your runningSum method. example
public static void main(String[] args) {
int[] myNum = {10, 20, 30, 40};
int[] result = runningSum(myNum);
for (int item : result){
System.out.println(item);
}
}
public static int[] runningSum(int[] nums) {
int[] result = new int[nums.length];
result[0] = nums[0];
for (int i = 1; i < nums.length; i++) {
result[i] = result[i - 1] + nums[i];
}
return result;
}
Related
Make an array of integer (score) with 10 members. Randomize the
content with value between 0-100. For each of the member of array,
visualize the value using “-” for each ten. For example: score[0] = 55 will be visualized as “-----" (Using Java).
public class w9lab1 {
public static void main(String args[]) {
double[] temperature = new double[7];
for (int i = 0; i < 7; i++) {
temperature[i] = Math.random()*100;
}
for (int i = 0; i < 7; i++) {
System.out.println(temperature[i]);
}
double totalTemperature = 0;
for (int i = 0; i < 7 ; i++) {
totalTemperature += temperature[i];
}
double maxTemperature = temperature[0];
for (int i = 1; i < 7; i++){
if (temperature[i] > maxTemperature){
maxTemperature = temperature[i];
}
}
System.out.println("Temperatur maximum adalah " + maxTemperature);
}
}
import java.util.Random;
import java.util.Arrays;
public class w9lab1 {
public static void main(String[] args) {
Random random = new Random();
int[] score = new int[10];
for (int i = 0; i < score.length; i++) {
score[i] = random.nextInt(101);
for (int n = 1; n <= score[i] / 10; n++)
System.out.print('-');
System.out.println();
}
System.out.println(Arrays.toString(score));
}
}
public class Testfor
{
public static int ArrayChallenge(int[] arr)
{
int[] temp = new int[arr.length - 1];
int diff = 0;
for (int i = 0; i < arr.length - 1; i++)
{
diff = Math.abs(arr[i] - arr[i + 1]);
temp[i] = diff;
}
arr = temp;
for (int i:arr)
{
System.out.println(i);
}
if (arr.length > 1)
{
ArrayChallenge(arr);
}
else
{
diff = arr[0];
System.out.println("diff" + diff);
}
return diff;
}
public static void main(String[] args)
{
// keep this function call here
//Scanner s = new Scanner(System.in);
System.out.println("answer:" + ArrayChallenge(new int[] { 5, 7, 16, 1, 2 }));
}
}
Here the input is an array of integers finally the array should be reduced to the size of one element by finding the absolute difference between two elements.
I am using recursion to solve this but I am getting 1 as the answer can someone please someone help to solve it?
input:[5,7,16,1,2]
[2,9,15,1]
[7,6,14]
[1,8]
[7]--->the array should reduce in the following manner to get an answer and finally should return the last element
output:7
Just preserve the return value
public class Testfor
{
public static int ArrayChallenge(int[] arr)
{
int[] temp = new int[arr.length - 1];
int diff = 0;
for (int i = 0; i < arr.length - 1; i++)
{
diff = Math.abs(arr[i] - arr[i + 1]);
temp[i] = diff;
}
arr = temp;
for (int i:arr)
{
System.out.println(i);
}
if (arr.length > 1)
{
diff = ArrayChallenge(arr);
}
else
{
diff = arr[0];
System.out.println("diff" + diff);
}
return diff;
}
public static void main(String[] args)
{
// keep this function call here
//Scanner s = new Scanner(System.in);
System.out.println("answer:" + ArrayChallenge(new int[] { 5, 7, 16, 1, 2 }));
}
}
package Package;
import java.util.Arrays;
class Cars {
public int[] runningSum(int[] nums) {
int sum = 0;
int[] res = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
sum = sum+nums[0];
res[i]=sum;
}
return res;
}
public static void main(String[] args) {
int[] a = {1,2,3,4};
Cars arr = new Cars();
System.out.println(arr.runningSum(a));
}
}
//output i'm getting is [I#e580929]
//output i want is [1,3,6,10]
Am not sure what you are trying to do by sum = sum+nums[0]; after the loop. But the solution below works!
public int[] runningSum(int[] nums) {
int sum = 0;
int[] res = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
res[i] = sum;
}
return res;
}
}
Hi Everyone, i am new to the programming world, can you please help me in finding second duplicate element in an array. i have tried but its not working.
public class FindSecondDuplicate {
public static void main(String[] args) {
int[] intArray = { 6,4,2,3,4,6,8};
int count=0;
Set<Integer> set=new LinkedHashSet<>();
for(int no:intArray)
{
if(set.add(no)==false)
{
count=count+1;
if(count==2)
{
System.out.println(no);
break;
}
}
else
{
set.add(no);
}
}
}
}
I think what you are trying to do can be accomplished using
public static void main(String[] args) {
int[] intArray = {6, 4, 2, 3, 4, 6, 8};
int count = 0;
Map<Integer, Integer> repeatCounter = new HashMap<>();
for (int i : intArray) {
if (repeatCounter.containsKey(i)) {
int repeatedNTimes = repeatCounter.get(i);
repeatCounter.put(i, repeatedNTimes + 1);
}else{
repeatCounter.put(i, 1);
}
}
for (int i : intArray) {
if (repeatCounter.get(i) == 2) {
count++;
if (count == 2) {
System.out.println(i);
break;
}
}
}
}
Find 2nd/3rd or any duplicate with Set Interface
public static void main(String[] args){
int[] array = {3, 12, 9, 3, 8, 3, 12, 4, 7, 8};
int find_duplicate = 3;
// Add all duplicates to set
Set<Integer> storeDuplicates = new LinkedHashSet<>();
for (int i = 0; i < array.length-1; i++){
for (int j = i+1; j < array.length; j++){
if (array[i] == array[j]){
storeDuplicates.add(array[i]);
}
}
}
// Traverse set for find the 2nd/3rd/any other duplicate
int count = 0;
for (int i : storeDuplicates){
count++;
if (count == find_duplicate) System.out.println(find_duplicate+" duplicate is : "+i);
}
}
I need help writing this code to get the permutation of numbers.
I need to store all the permutations in a 2D array.
After output of the permutation, I then need to process 30 percent of the permutations in one method an the the rest in another method.
My code:
public class Permutation {
* #param args the command line arguments
*/
void printArray(int []a) {
for (int i = 0; i< a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println("");
}
void permute(int []a,int k ) {
if(k==a.length)
printArray(a);
else
for (int i = k; i< a.length; i++) {
int temp=a[k];
a[k]=a[i];
a[i]=temp;
permute(a,k+1);
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}
public static void main(String[] args) {
Permutation p=new Permutation();
int a[]={1,2,3,4,5,6};
p.permute(a, 2);
}
}
This is my solution instead of a 2d array use an ArrayList
import java.util.ArrayList;
import java.util.Arrays;
/**
*
* #author David
*/
public class Permutations {
public static int[] a;
public final int SIZE = 6;
public final int NUMPERM;
public final ArrayList<int[]> newlist;
public Permutations()
{
a = new int[SIZE];
for(int x = 0; x < SIZE; x++)
a[x] = x+1;
NUMPERM = Factorial(a.length);
newlist = new ArrayList<>(NUMPERM);
}
public void permute()
{
permutation(a,0,a.length);
}
private void permutation(int array[],int start, int end)
{
newlist.add(saveArray(array));
if (start<end)
{
int i,j;
for(i=end-2; i>=start; i--)
{
for(j=i+1; j<end; j++)
{
Swap(array,i,j);
permutation(array,i+1,end);
}
Rotate_Left(array,i,end);
}
}
}
private int[] saveArray(int[] array)
{
int[] newarray = new int[array.length];
System.arraycopy(array, 0, newarray, 0, array.length);
return newarray;
}
public void Print()
{ //just to prove the list works
System.out.println("the current size of newlist is : " + newlist.size());
int[] array = new int[a.length];
for(int x = 0; x < newlist.size(); x++)
{
array = newlist.get(x);
System.out.println(Arrays.toString(array));
}
}
private void Swap(int array[],int i,int j)
{
int t;
t = array[i];
array[i] = array[j];
array[j] = t;
}
private void Rotate_Left(int array[],int start,int end)
{
int tmp = array[start];
for (int i=start; i < end-1; i++)
{
array[i] = array[i+1];
}
array[end-1] = tmp;
}
private int Factorial(int a)
{
int fact = 1;
for(int x = a; x > 0; x++)
fact *= a;
return fact;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Permutations newperm = new Permutations();
newperm.permute();
newperm.Print();
}
}
then all you have to do is send the list to the other functions and only use what you need from it.