I am having some issues getting my min and max values to print. Also as new
as I am to programming I don't understand why all my values are decimals. I will post the code and my results.
import java.util.Random;
import java.util.Scanner;
import java.util.*;
public class RandomArray1
extends ArrayList<Double>
{
private static final long serialVersionUID = 1L;
public RandomArray1()
{
super();
}
public static RandomArray1 getInstance(int size)
{
Random randomNumberGenerator = new Random();
RandomArray1 randomArray = new RandomArray1();
for (int i = 0; i < size; i++)
{
randomArray.add(randomNumberGenerator.nextDouble());
}
return randomArray;
}
public Double getAverage()
{
if (this.size() == 0)
{
return 0d;
}
Double sum = 0d;
for (Double element : this)
{
sum = sum + element;
}
return sum / this.size();
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many numbers you would like to Generate: ");
RandomArray1 randomArray = RandomArray1.getInstance(scan.nextInt());
System.out.println(randomArray);
System.out.println("Average" +randomArray.getAverage());
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
while (true) {
if ( !scan.hasNextDouble())
break;
Double num = scan.nextDouble();
min = Math.min(min, num);
max = Math.max(max, num);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
}
}
Here are my results...
Enter how many numbers you would like to Generate: 5
[0.8630040934474159, 0.12949667753808425, 0.5751777190226718, 0.18492672539115063, 0.7508377917335503]
Average : 0.5006886014265746
always decimal point and it doesn't even print "Max is:" or "Min is:" which makes me think something is wrong with this section. But I don't know what it is...any thoughts. Just ideas...
while (true) {
if ( !scan.hasNextDouble())
break;
Double num = scan.nextDouble();
min = Math.min(min, num);
max = Math.max(max, num);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
I think you're filling you're array with nextDouble() which as documented gives you double precision floating point numbers between 0 and 1.
All good, changed the Arraylist to an Integer and everything else. This solved my issues as discussed. here is what the code looks like now...works perfect!
import java.util.*;
public class RandomArray1
extends ArrayList<Integer>
{
private static final long serialVersionUID = 1L;
public RandomArray1()
{
super();
}
public static RandomArray1 getInstance(int size)
{
Random randomNumberGenerator = new Random();
RandomArray1 randomArray = new RandomArray1();
for (int i = 0; i < size; i++)
{
randomArray.add((int) randomNumberGenerator.nextInt());
}
return randomArray;
}
public Double getAverage()
{
if (this.size() == 0)
{
return 0d;
}
Double sum = 0d;
for (Integer element : this)
{
sum = sum + element;
}
return sum / this.size();
}
public static void main(String[] args)
{
#SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many numbers you would like to Generate: ");
RandomArray1 randomArray = RandomArray1.getInstance(scan.nextInt());
System.out.println(randomArray);
System.out.println("Average : " +randomArray.getAverage());
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;
for (int num : randomArray)
{
min = Math.min(min, num);
max = Math.max(max, num);
}
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
}
thank you so much!
Your program works fine:
Enter how many numbers you would like to Generate: 3
[0.26063976207509887, 0.48867331377683443, 0.3864751544223266]
Average0.37859607675808665
4
Max is: 4.0
Min is: 4.0
5
Max is: 5.0
Min is: 4.0
3
Max is: 5.0
Min is: 3.0
end
But since it expects an input, but doesn't say so, it could appear to be "dead". Just include a prompt before waiting for the next input:
System.out.print("Please enter next number: ");
if ( !scan.hasNextDouble())
break;
In case you wanted to calculate the min/max of the existing array instead of a new user input, use a for loop to iterate over the existing array:
for (double num : randomArray) {
min = Math.min(min, num);
max = Math.max(max, num);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
Related
my code to find standard deviation is wrong. my code is supposed to find standard deviation from user input. i typed in the numbers 1 2 3 and the standard deviation of this set of numbers is 1 but it printed 10 where did i go wrong. also i know i have a bunch of unused variables dont mind them.
import java.util.Scanner;
public class readFromKeyboard {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inStr = input.next();
int n;
int i;
int count=0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
double average=0;
int sum;
double deviation = 0;
int total;
int temp = 0;
while (!inStr.equals("EOL")) {
count++;
n = Integer.parseInt(inStr);
min = Math.min(min, n);
max = Math.max(max, n);
System.out.printf("%d ", n);
inStr = input.next();
average += n;
temp += Math.pow(n - average, 2);
}
deviation = temp
average = average/count;
System.out.println("\n The average of these numbers is " + average);
System.out.printf("The list has %d numbers\n", count);
System.out.printf("The minimum of the list is %d\n", min);
System.out.printf("The maximum of the list is %d\n", max);
System.out.printf("The standard deviation of the list is %d\n", temp);
input.close();
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class ReadFromKeyboard {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inStr = input.next();
int n = 0;
int i;
int count = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
double average = 0;
int sum;
double deviation = 0;
int total;
double temp = 0;// correction here because it must store double or float value
ArrayList<Integer> n1 = new ArrayList<Integer>();// i used this to store all entered values
while (!inStr.equals("EOL")) {
count++;
n = Integer.parseInt(inStr);
min = Math.min(min, n);
max = Math.max(max, n);
System.out.printf("%d ", n);
n1.add(n);
inStr = input.next();
average += n;
}
average = average / count; // this will give you final average
for (int j = 0; j < count; j++) {
temp += Math.pow(n1.get(j) - average, 2);
}
//System.out.println("\n" + temp + " " + count);
deviation = Math.sqrt(temp / count); // this is your standard deviation
//System.out.println(deviation);
System.out.println("\n The average of these numbers is " + average);
System.out.printf("The list has %d numbers\n", count);
System.out.printf("The minimum of the list is %d\n", min);
System.out.printf("The maximum of the list is %d\n", max);
System.out.println("The standard deviation of the list is " + deviation);
input.close();
}
}
Standard deviation for one variable is defined here. You have to take the square root of the mean of the sum of the squared differences between observations and the mean of the set.
//in the loop
temp += Math.pow(n - average, 2)
//outside the loop
deviation = Math.pow(temp/count,0.5) //or alternatively Math.sqrt()
This should give you what you need.
So I have a program I wrote that finds the max and min value of a five number set. It works for the most part but when I enter a set of numbers like {5,6,7,8,9} then it outputs 9 as the max, but outputs 0 for the min. Any thoughts or suggestions.
import java.util.Scanner;
public class MinMax {
public static void main (String [] args) {
#SuppressWarnings("resource")
Scanner in = new Scanner (System.in);
final int NUM_ELEMENTS = 5;
double[] userVals = new double[NUM_ELEMENTS];
int i = 0;
double max = 0.0;
double min = 0.0;
System.out.println("Enter five numbers.");
System.out.println();
while (i < NUM_ELEMENTS) {
System.out.println("Enter next number: ");
userVals[i] = in.nextDouble();
i++;
System.out.println();
}
for (i = 0; i < userVals.length; i++) {
if (userVals[i] > max) {
max = userVals[i];
}
else if (userVals[i] < min) {
min = userVals[i];
}
}
System.out.println("Max number: " + max);
System.out.println("Min number: " + min);
}
}
Default your min to a number out of range (like Double.MAX_VALUE), and max to Double.MIN_VALUE. You might also simplify your code by removing the second loop; you can perform the logic in one loop and you might use Math.max(double, double) and Math.min(double, double). Something like,
Scanner in = new Scanner(System.in);
final int NUM_ELEMENTS = 5;
double[] userVals = new double[NUM_ELEMENTS];
System.out.println("Enter five numbers.");
System.out.println();
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < NUM_ELEMENTS; i++) {
System.out.println("Enter next number: ");
userVals[i] = in.nextDouble();
min = Math.min(min, userVals[i]);
max = Math.max(max, userVals[i]);
}
System.out.println("Max number: " + max);
System.out.println("Min number: " + min);
Intialize your min variable to non-zero max value. Means max value that you can have in your input from console.
I need to calculate the sum of 2^0+2^1+2^2+...+2^n, where n is a number entered by the user. The main problem is that I don't know how to use the while loop to sum the different result of 2^n up.
Here is what I've tried:
import java.util.Scanner;
public class SumOfThePowers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a power: ");
int power = Integer.parseInt(reader.nextLine());
int number = 2;
int i = 0;
double sum = 0;
while(power <= i) {
Math.pow(number, i);
sum = sum + Math.pow(number, i);
i = i + 1;
}
int result = (int)Math.pow(number, i);
System.out.println("The sum is: " + result);
}
}
Only you have to do is:
import java.util.Scanner;
public class SumOfThePowers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a power: ");
int power = Integer.parseInt(reader.nextLine());
double sum = Math.pow(2,power+ 1 ) - 1;
System.out.println("The sum is: " + sum);
}
}
In this link explains the math expresion
All fine, just a few changes.
Change the parts code to
System.out.println("Type a power: ");
int power = Integer.parseInt(reader.nextLine());
int number = 2;
int i = 0;
double sum = 0;
/*Remove this --------> while(power <= i) {*/
while (i <= power) {//it should be this
/*Remove this -------> Math.pow(number, i);*/
sum = sum + Math.pow(number, i);
i = i + 1;
}
System.out.println("The sum is: " + sum);
Your conditional is backwards, it should read:
while (i <= power)
You compute the sum of powers, then completely ignore it, just printing out the result of 2^i. you should be printing out sum, something like:
while (i <= power) {
sum += Math.pow(number, i);
i++;
}
System.out.println("The sum is: " + sum);
For style points this won't handle a negative power, so you'll need to test for that.
Dont understand why do you want to loop in this case. You can do it like :
System.out.println("The sum is: "+(Math.pow(2, power+1)-1 ));
But if you really want to use loop try this :
Scanner reader = new Scanner(System.in);
System.out.println("Type a power: ");
int power = Integer.parseInt(reader.nextLine());
int number = 2;
int i = 0;
double sum = 0;
while(i<=power) {
sum = sum + Math.pow(number, i);
i = i + 1;
}
int result = (int)Math.pow(number, i);
System.out.println("The sum is: " + sum);
Here is a solution with comments to explain the logic. While loops need some kind of variable to control the number of iterations. That variable of course needs to be updated somewhere inside the loop.
You can compute the sum of the powers with the Math.pow() function, obviously. No import statement is needed to use it. I hope this helps. Good luck.
/* Scanner and variable to get and hold user input */
Scanner scan = new Scanner( System.in );
int userInput = 0;
/* Variable to hold the sum, initialized to 0.0 */
double sum = 0.0;
/* Prompt the user, and obtain the reply */
System.out.print( "Enter the exponent: ");
userInput = scan.nextInt();
/* The while loop and it's initialized counter */
int counter = 0;
while( counter <= userInput ){
/* Add each power to sum using Math.pow() */
sum = sum + Math.pow( 2, counter );
/* Watch the output as the loop runs */
System.out.println( "Sum: " + sum );
counter++; // Increment counter, so the loop exits properly
} // End while loop
public class SumofSquare {
public static void main(String[] args) {
// TODO Auto-generated method stub
String c = "123";
char d[] = c.toCharArray();
int a[] = new int[d.length + 1];
for (int i = 0; i < d.length; i++)
a[i] = d[i] - 48;
int r = 0;
for (int i = 0; i < c.length(); i++)
r = r + (int) Math.pow(a[i], a[i + 1]);
System.out.println(r);
}
}
import java.util.Scanner;
public class SumOfThePowers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a number:");
int power=Integer.parseInt(reader.nextLine());
int number=2;
int i=0;
int result=0;
while (power>=i) {
result += (int)Math.pow(number, i);
i++;
}
System.out.println("The result is "+result);
}
}
import java.util.Scanner;
public class SumOfThePowers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Type a number:");
double power=Double.parseDouble(reader.nextLine());
int number=2;
int i=0;
double sum=0;
while (power>=i) {
sum=sum+Math.pow(number, i);
i++;
}
System.out.println("The sum is "+sum);
}
I want to create program read more than 10 numbers from the user and find the maximum number and minimum number then print all the numbers from the user.
This is my program, but I don't know how can I find the maximum number and minimum number:
import java.io.*;
public class ass3 {
public static void main (String [] args) throws IOException
{
int times , num1 ;
int max , min;
System.out.print("How many numbers you want to enter?\n*moer than five number");
times=IOClass.getInt();
if (times>5) {
for(int i = 0;i<times;i++){
System.out.println("please type the "+i+ "number");
num1=IOClass.getInt();
}
}
}
}
If you initialize the min and max like this:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
You can check whether the new number is smaller than min or bigger than max, and change them if needed:
int num = ...;
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
This is my solution, hope it helps:
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("How many numbers you want to enter?\nThe number must be grater than 5");
int times = in.nextInt();
if (times > 5)
{
int[] numbers = new int[times];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < times; i++)
{
System.out.println("Please type the " + i + " number:");
int number = in.nextInt();
numbers[i] = number;
if(number < min)
{
min = number;
}
if(number > max)
{
max = number;
}
}
System.out.println("Max: " + max);
System.out.println("Min: " + min);
}
in.close();
}
}
I'm trying to write a class which reads 5 integers from the user and returns the highest and lowest value back. This must be done using loops and without using arrays and Integer.MIN.Value/Integer.MAX.Value. I've already succeeded writing code that gets 5 integers from the user and returns the highest value but I just can't get both the highest and the lowest value returned in the same class.
Here is the code I mentioned above:
import java.util.Scanner;
public class Ovning_321 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
int max = 0;
for (int x = 0; x<5; x++){
System.out.print("Give me an integer: ");
number = input.nextInt();
if (number > max){
max = number;
}
}
System.out.println("Highest value: " + max);
}
}
here you go :)
import java.util.Scanner;
public class Ovning_321 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
int max = 0;
int min = 0;
for (int x = 0; x<5; x++){
System.out.print("Give me an integer: ");
number = input.nextInt();
if (x == 0 || number > max){
max = number;
}
if (x == 0 || number < min){
min = number;
}
}
System.out.println("Highest value: " + max);
System.out.println("Lowest value: " + min);
}
}
Why not just repeat your max logic for min?
public class Ovning_321 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Give me an integer: ");
number = input.nextInt();
int max = number;
int min = number;
for (int x = 0; x<4; x++){
System.out.print("Give me an integer: ");
number = input.nextInt();
if (number > max){
max = number;
}
if (number < min){
min = number;
}
}
System.out.println("Highest value: " + max);
System.out.println("Lowest value: " + min);
}
}
Note that max and min are initially set to the first number that the user enters, so there will be no false 0's and no need to MAX_INT or MIN_INT. This in turn makes the loop run once less so terminate at i == 4 instead of 5.