static int count = 0;
static long[] cache = new long[3000];
static {
cache[0] = 1;
}
public static void main(String args[]) {
for (int i = 0; i < 100; i++){
factorial_with_cache(2999);
}
System.out.println(count);
}
public static long factorial_with_cache (int n){
if (cache[n] != 0){
return cache[n];
}
cache[n] = n * factorial_with_cache(n - 1);
count++;
return cache[n];
}
I built a function that calculates factorials using a cache (ignoring overflow).
But its runtime isn't any better compares to non-caching function and I found that caching isn't working correctly.
Because I expected a variable 'count' to be 2999 after loop but I fount it is 293465 which is a lot more than that. (without loop, it prints 2999)
What is to wrong with this function?
It is because the range of long datatype:
long 8 bytes (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
your factorial gives the positive values till you are searching for factorial of 25.
And latter 25 , the value of factorial which are calculated is coming negative (It means you are overflowing long) and your count will work as expected till 65 factorial is calculated (till negative value) and then the value for factorial 66 it reaches to 0..
just try below by printing factorial:
for (int i = 0; i < 100; i++) {
long fact=factorial_with_cache(66);
System.out.println(fact);
}
System.out.println(count);
And the count will be printed as
(forLoopCount*(number-65))+65 , In your case (100*(2999-65))+65 is 293465
becuase the value for factorial it traces back in cache which is not zero is 65th element (at 64 index).
So, let me break it down to you!.
The factorial value of 2999 is 13831198678126180285189556976955373903170397316439366392298225525658528550411773166953335884059334473358394878461205706165250290371348635931517800720356113203873679301399725840687554463393227601721573232498656280819242922032367667908703698446747042372944385912938442812976579204340368770976791467161792908482026536763476497016597962706920837208377639001547222987372530067858391829660217859289638428364594156664891566396944031170356121274762354507422848113071258383496196034678939603033097637499047300169646331468238450491051510529147169986724755958362486789324705690789582425714662973561230539501202732819906031261484507986168459203695503720787715306962036185816958171315101812071237299383615730698142124734425009998777928314671591890471560274735833304904959453223240212984890196653392856493421129589037623689334983609133572374939825462133789731181451373437595533811597516653047843373551034468875167743673579450500731701049557870779579798365517154053116338596649981357437001475641612713380485162624246015285341187194401439315317685276897783748963573748717156161769574677089322363037849660911158458585633121725465474927244487503626338581145799640164089032772344644035725447994450814858680158855133471471566493328118184865933128900946476171137729637721898436949504508364668715924682335777135513931476001478897301228489756952423900549337170685817482869322924594934553551003036605213030431370211238540513634600045019557208087444634144722209572173742863424856277834965975623123264292792298280478857955478985025157988294085407394460004866920398953072534411602822485072110137246001443714741975414563519594304969324628586261528579942898463434127945755294142551435479172585965738348357120600576683876141492098075828585209276254499589065093789637861306274833094913863894376778010733315584027638295254781954610771181735025318637354022424432317128707664886202509105961484149955116054723079614963020498616978124480963271628142440605710424177963844835527047941127068625126602868360296587251294298750977877916818183056200089076530408509618194827562218441437670451251604136520412562026002566902646513949635716489125951469176102624329367970981707309954931234483752478815193819919046940901995441721784302194857041195090070644484327035189466269961364993448728359341757679578928014056475745993488101416681676935545677586672113973814268569600810283522630702882325818379846373279545389063433426048793378977494110923865060198731023168079278694206856962403099309932476468192292446106625814895181431600107247018896474570181676290139870638357579666677214004824737774431290539343185204668200955800140961796618216776009476171707432009598580600540607745366106990480349004177689104965384412262911661517983201465683450782588737131971506423474040743808999564029154506993562074784937792064798377314289371162177187409391071658827235127282051775532062510098826200386957421213481307419200642164870305628383671047981858132767730852384720717147489707456839716365953949812050321036644546531520598864465617319535112877058774715688947419064131580843908826519916664427749093269298823584351338064696733536570891584395455175134404252585951310232510018865069402485833157299381484590749402629481111706181867189617902727630965072766713218125649095472925555254271796579113386470181227145145782044748230689023360927474548519150294185556719080917603439047213543803560461443641342427106320294937680221742056517608545727954154981431896286464615575574564733158523773515339421141910343038451177576269394257673865595696145764470705694837193543322565755533887878425653323007167374034984753189092864395172089482366791478206807334511797067912797698473565007162069130217730303607114778083832632094745399523891591282708699147685012083622118240243657099361565732179240926631053400186908352669170802498149387797404799372830916093992885059492849249732904712828347430127984696856947346045759045222567855013693926840231677378744507230913510338446973920611359605911891548916357205533551076812967546595355169668436624145148563105200443314135704098338484590507017314148504265121657071624074064576429719944333914132659197510167106279294498294744074169106272133146311875939034291810862931519619247593463572388793752634599383049481842295290543310338170244405184924421525345305394766236107745856279305711691002008912764098229027010250648673458812712892369111259770860752452145178192387601343493641928636582411137977634690746620001816089765370131213375846513299799140007308422976207521653093045456515661082438164698099164737736238987887604321282063792704869736900344337373311421647222149710103632831178455467277094227704168859462141968845732336969092143110178250432445572309805015956983080592707178326499816577452815043403343240119997858835910187898983513330297084001603373363523211048122726398082654521129700828750257789083133267241150542349616716479447911942146684493824985807109624549493196517947945459425200477792599605375618154135498044805373036317551108920542553407043626403888641459832178279343412021431660661847004770522200579839513142002891066188195561545147576726674097535863255730732789176509455052905173962566105999668617707172897012592225627884763749139942033144845223843137607945870705185228736818194603451718134984572823575454406177591828833937000873777076143286975370719857688674644025825580933636487558107379601763547689237092321337911968750350350207856080439217177676925119658378188382474722467172490240279700623329997759788303630895974913235271384285181321984740867973019755275212448225468786508612988993837327986628867894584347339330109395315189804183506355434284956511125894328200010602697940710012700738129019153727101417320638801990152011727707236050552882452185513510976137249542985694285597433351175282125687093530484011848796644042625302116542612658623784196206071084087859012798734245795108302784560165996695233840827767813175065781551114331550855078698914278183507913244634124753936290599761240761788764479746965254304384814035524536401827280713121964622571847614159091700055578119247697879649481521588125728024624793157002622383690149851552542093855379183576921256026866677281432077992602744437119929192378994337605857208557227762427170645778895263145703032582860407622708941374105757560394991735954484369165110569420051053146751215121841484529317344915071143082972214834934785117427325907327323956351628058965466260901594022042307157516206082619875478093063424834484331589789332405761721821863933157263022628562426191490028096574592744921026913639837655412782142613217955005774888616730714697231543457088527065900258227520678399439594304530337999272434545057921568904794664270097074820282773506327749449618377008513240527728310551223577424390998477250258491878249375773652218477283959862326057497240566955256032969442610279413479245991485114175229570966285171060322337499286484763536542456863553323594724246140240535192263020188389231521830040129388591953979743641454327796516619093291507799363499952822494950133169703132944736808231037872625326203934365369591454254996804885055848502537888575304997792038113414587636763461378629510437530377514949741965335728283723130064661811549805161260779434177122925330001814574110967154189394698666276840840528442866611330112198472977565560054421966452744887749026774673476597119141918499516457632064030550264890665707146610860517981945041882670296628962352294237074209578011924139474934410391009112814073534671869747359856497734981269800821854176767776274721767346002013762814794961396376464661990588556737167203780040389785178621661600980521079426482525438347561911423287705520933644054859388470137299692798736919535048363842176140811215743626823029475470841708861576378019731723918097050874000743252328975986374873654787685684044366405474086145883963064543443283741094316935375400191188518071061185279429772712931980320767930925743448141144113853867852863500774825091500876956610471375132123918137870632423052883572240769361145098526133695745342449000372928715411061813143685686161256824935345001518961673534243407418799210246313328341659385060893305664721381846776636604396835551452718885897176356302728734712814697152118430414021633905812143491151730458640971651094329383936246692244495665898440957150263861482901326801912557851925957864645963859049318674446902818797438056706130851744872706591971891128651973088423541354578092586738615503678456019321106904965647554452856791768609892202741402821821519518889296407542539996754653930879152617217513639585536181286553740648082906808320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 find it yourself
I know its a lot of scrolling :P
Okay, now during your iterations, factorial of 20 will be 2,432,902,008,176,640,000 and factorial of 21 will be 51,090,942,171,709,440,000 and maximum value for long in java is 9,223,372,036,854,775,807 so, here your cache[19] will be -4,249,290,049,419,214,848 and cache[65] will be 0. which makes all values till cache[2999] to 0. So, every time you call the method factorial_with_cache, it's not serving from cache, but calculating every time (because you're checking if(cache[n] != 0) then return from cache else calculate) causing the count value 293465 instead of 2999.
I've modified the code a little for your understanding.
public class Test2 {
static int count = 0;
static long[] cache = new long[3000];
static {
cache[0] = 1;
}
public static void main(String args[]) {
factorial_with_cache(2999);
}
public static long factorial_with_cache (int n){
if (cache[n] != 0){
return cache[n];
}
cache[n] = n * factorial_with_cache(n - 1);
System.out.println("Factorial(" + n + ") is " + cache[n]);
count++;
return cache[n];
}
}
This will print your calculated fact values.
public class Test2 {
static int count = 0;
static long[] cache = new long[3000];
static {
cache[0] = 1;
}
public static void main(String args[]) {
for (int i = 0; i < 100; i++){
factorial_with_cache(2999);
System.out.println("i[" + i + "] count[" + count +"]");
}
}
public static long factorial_with_cache (int n){
if (cache[n] != 0){
return cache[n];
}
cache[n] = n * factorial_with_cache(n - 1);
count++;
return cache[n];
}
}
This will print count value for each iteration.
Related
Ok so I been working on this assignment all day for the past 3 days but I haven't had any luck. I wasn't going to ask for help but I finally gave up. But there is also one more thing I need to implement to the code. This is what I gotta implement "Find the length of the longest continuous series of positive numbers in the array data. If the contents were: 4 5 0 2 . . . -1 88 78 66 -6. The length would be 3. For this problem, 0 is considered non-negative but not positive". Plus I have an issue where I can't print the largest int in the array of 20.
import java.util.Random;
import java.util.ArrayList;
public class arrayops {
public static int findLargest(ArrayList<Integer> nums) {
int greatestnum = nums.get(0);
for (Integer item : nums) {
if (item > greatestnum) {
greatestnum = item;
}
}
return greatestnum;
}
public static int randomData(ArrayList<Integer> nums) {
int[] array = new int [20];
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = -100 + random.nextInt(201);
}
return -100 + random.nextInt(201);
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(1);
nums.add(4);
nums.add(13);
nums.add(43);
nums.add(-25);
nums.add(17);
nums.add(22);
nums.add(-37);
nums.add(29);
System.out.println("The Greatest Number from the hardcoded numbers " + findLargest(nums));
System.out.println("The Greatest number from the random numbers " + randomData(nums));
}
}
The findLargest method:
public static int findLargest(ArrayList<Integer> nums) {
int greatestnum = 0;
int greatestLen = 0;
for (Integer item : nums) {
if (item > 0) {
greatestLen++ ;
if(greatestLen > greatestnum)
greatestnum = greatestLen;
}
else
greatestLen = 0;
}
return greatestnum;
}
Logic used:
Keep the length of the longest chain encountered, and the length of current chain, in two separate variables (greatestnum and greatestLen respectively)
Increment greatestLen every time a positive number is encountered. If the number if less than or equal to zero, reset this count.
If the length of current chain is greater than the previous longest chain, sent the longest chain size to current chain size.
The problem is you created a list with random numbers but never put that list into the findLargest method. You also never created a method to find the consecutive positive numbers. If you didn't know how to go about coding it, I recommend drawing out an algorithm on paper.
Largest value in ArrayList...
public static int findL(ArrayList<Integer> nums)
{
int top = nums.get(0);
for(int i = 0; i<nums.size(); i++)
{
if(nums.get(i)>top)
{
top = nums.get(i);
}
}
return top;
}
Largest number of consecutive positives...
public static int positiveString(ArrayList<Integer> nums)
{
int longest = 0;
int count = 0;
for(int i = 0; i<nums.size(); i++)
{
if(nums.get(i) > 0)
{
count++;
}
else
{
if(longest<count)
{
longest = count;
}
count = 0;
}
}
return longest;
}
If you want to arrange the numbers into order you can simply use java.util.TreeSet. Then use the method last() to get the largest number.
public static int findLargest(ArrayList<Integer> nums) {
return new TreeSet<Integer>(nums).last();
}
I am learning Java and discovering that a little bit of knowledge is confusing.
The goal is to write a method that is the equivalent of the n! function. I am using a for loop to multiply a variable declared outside the method. All I get back is 0.
What am I doing wrong?
//
// Complete the method to return the product of
// all the numbers 1 to the parameter n (inclusive)
// # param n
// # return n!
public class MathUtil
{
public int total;
public int product(int n)
{
for (int i = 1; i == n; i ++)
{
total = total * i;
}
return total;
}
}
There is actually a lot of problems in your code:
It does not make sense to make it an instance method.
You have not initialize your total to a reasonable value.
The condition in your for loop is wrong
Method is not given a meaningful name
Messy indentation
(The list keeps growing...)
So here is a slightly improved version
public class MathUtil
{
//
// Complete the method to return the product of
// all the numbers 1 to the parameter n (inclusive)
// # param n
// # return n!
public static int factorial(int n)
{
int total = 1;
for (int i = 1; i <= n; i ++)
{
total = total * i;
}
return total;
}
}
so that you can call it as MathUtil.product(123) instead of some weird new MathUtil().product(123)
Personally I would rather do something like
result = n;
while (--n > 0) {
result *= n;
}
return result;
You are missing the initialization. Now I added the default value to 1. And you also have to change the condition. The for loop has to go on as long as the var i is smaller or equal to n.
public class MathUtil
{
public int total = 1;
public int product(int n)
{
for (int i = 1; i <= n; i ++)
{
total = total * i;
}
return total;
}
}
you didn't initialized total, therefore it's 0. Whenever you multiply 0 with anything, you will get 0 as result.
public int total = 1;
public int product(int n) {
for (int i = 1; i <= n; i++) {
total = total * i;
}
return total;
}
you havent initialized total. It defaults to 0. Then when you multiple anything with total, you get 0 as result
Can anyone answer this question?
public class AddingArray {
public static void main(String[] args){
int arry1[] = {2,3,4,5,6,7,9};
int arry2[] = {4,3,7,9,3,5};
for(int i = 0; i <arry1.length; i++){
int result = arry1[i] + arry2[i];
System.out.println("Result "+result);
}
}
}
Whenever I try executing the above code I get the error Exception in
thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at
basics.AddingArray.main(AddingArray.java:9)
But,my output should be like this 6,6,11,14,9,12,9
As people have mentioned, one of yours arrays is literally shorter than the other. Take two blocks and overlay them over only one block. The second (in this case index 1 block) would fall into the abyss, because the block that was supposed to catch it never existed.
I would make sure both of them are of the same size. If you do want to leave em as they are, I would do this:
int result = 0;
try
{
for(int i = 0, length = array2.length; i < length; i++)
{
result = array1[i] + array2[i];
System.out.println("Result is: " + result);
}
catch(Exception e)
{
System.out.println("You tried to do something that resulted in an error");
System.out.println("Your previous result was: " + result);
}
}
SO, assuming that I still recall how to do basic arrays, what this code will do is that it will catch any errors thrown by your code.
Let's make this as simple and understandable as possible, with no fancy annotations:
You have two int arrays, of not equal lengths, and you wish to add the index-paired numbers to an array of the sums. However, if one of the arrays does not have any more numbers, the value of the longest array will be the result.
public class AddingArray {
public static void main(String[] args){
int arry1[]={2,3,4,5,6,7,9};
int arry2[]={4,3,7,9,3,5};
You need to determine the length of the longest array. You can do this with a Math.max()-method, where you give the length of each array as parameters:
int biggestArrayLength = Math.max(arry1.length, arry2.length);
Then, instead of for(int i=0;i<arry1.length;i++){, you write:
for(int i=0;i<biggestArrayLength;i++){
Now it doesn't matter which of the two arrays is the biggest one.
Inside the loop, I would define two ints, representing a value from each of the two arrays:
int value1 = arry1[i];
int value2 = arry2[i];
however, this will give an error when the smallest array does not have any more elements. We need to check if the array actually has an element with index i. index numbers in arrays start with 0. so if the length is 7, the 7 elements will have index numbers from 0-6. In other words, only index numbers that are lower (and not equal) to length, is valid numbers:
int value1 = 0;
int value2 = 0;
if(arry1.length > i){
value1 = arry1[i];
}
if(arry2.length > i){
value2 = arry2[i];
}
int result = value1 + value2;
System.out.println("Result "+result);
}
}
}
Now, if you need to put these in a third array, say named sumArray, this would be the complete code:
public class AddingArray {
public static void main(String[] args){
int arry1[]={2,3,4,5,6,7,9};
int arry2[]={4,3,7,9,3,5};
int biggestArrayLength = Math.max(arry1.length, arry2.length);
int[] sumArray = new int[biggestArrayLength];
for(int i=0;i<biggestArrayLength;i++){
int value1 = 0;
int value2 = 0;
if(arry1.length > i){
value1 = arry1[i];
}
if(arry2.length > i){
value2 = arry2[i];
}
int result = value1 + value2;
sumArray[i] = result;
System.out.println("Result "+result);
}
}
}
It is because your loop will go from 0 to 6 (which is the array1.length - 1) and your array2 only has 6 elements (so from 0 to 5).
So when you are accessing arry2[6]; It will give you the java.lang.ArrayIndexOutOfBoundsException.
You could change your for loop to go to the length of the smallest array:
for(int i = 0; i < arry2.length; i++){ /*Do what you want */ }
Or add an element in array2, but that is yours to decide since I do not know your requirements.
Because arry1 is longer than arry2 when you make the last iteration through the loop arry2[i] returns null because there is no element to return.
either do:
if(arry2[i] != null) {
//run your adding code
}
or change your arrays to be the same size
Edit: The reason it is not working properly is because you are using the length of the largest array as the conditional within the for loop. This condition allows you to attempt to access the 2nd array at a location that does not exist, which is why you are getting an ArrayIndexOutOfBoundsException.
Can we stop the downvoting?
End edit----
If you want to add up all of the elements in the array use this code.
public class AddingArray {
public static void main(String[] args){
int arry1[]={2,3,4,5,6,7,9};
int arry2[]={4,3,7,9,3,5};
int result = 0;
for(int i=0;i<arry1.length;i++){
result+=arry1[i];
}
for(int j=0; j < array2.length; j++){
result += array2[j];
}
System.out.println("Result: "+ result);
}
}
if you are trying to sum individual elements as you loop you can do the following. This will properly handle 2 arrays of different length regardless of which one is longer.
public class AddingArray {
public static void main(String[] args){
int arry1[]={2,3,4,5,6,7,9};
int arry2[]={4,3,7,9,3,5};
int result = 0;
for(int i=0;i<arry1.length;i++){
result=arry1[i];
if(i < array2.length){
result += array2[i];
}
System.out.println("Result: "+ result);
}
for(int j = i; j < array2.length; j++){
System.out.println("Result: "+ array2[j]);
}
}
}
public class FibonacciGenerator
{
//instance variables
private int recent ; //one values ago
private int previous ; //two values ago
private int n ; // the number of values returned so far
/**
Constructs the generator by setting the instance variables to 1
*/
public FibonacciGenerator()
{
recent = 1 ;
previous = 1 ;
n = 0 ;
}
/**
Produces the next Fibonacci number
#return the next in the Fibonacci sequence
*/
public int next()
{
n ++ ;
if (n == 1) return 1 ;
if (n == 2) return 1 ;
int result = recent + previous ;
//----------------Start below here. To do: approximate lines of code = 3
// 1. Update previous and recent, and 2. return result.
previous++;
recent++;
return result;
//----------------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
}
import java.util.* ;
public class FibonacciGeneratorTester
{
public static void main(String[] args)
{
System.out.println("The 1st Fibonacci number is: "
+ getFibonacci(1)) ;
System.out.println("The 10th Fibonacci number is: "
+ getFibonacci(10)) ;
}
/**
A static method to return the n'th Fibonacci number
#param n the index of the Fibonacci number
#return the n'th Fibonacci number
*/
public static int getFibonacci(int n)
{
FibonacciGenerator generator = new FibonacciGenerator() ;
int result = 0 ;
//----------------Start below here. To do: approximate lines of code = 4
// 1. Write a for-loop that calls the generator n times 2 . return the last result of the call.
for (int i = 1; i <= n; i++){
generator.next();
return result;
}
}
}
missing return statement second last curly brace highlighted. Is my for loop correct?.......................................................................................................................................................
Your for loop begins:
for (int i = 1; i <= n; i++){
If n was less than 1, it would immediately exit, and there is no return statement between the end of the loop and the end of the method. That is the missing return statement.
Look at this:
public static int getFibonacci(int n)
{
FibonacciGenerator generator = new FibonacciGenerator() ;
int result = 0 ;
//----------------Start below here. To do: approximate lines of code = 4
// 1. Write a for-loop that calls the generator n times 2 . return the last result of the call.
for (int i = 1; i <= n; i++){
generator.next();
return result;
}
}
It must return an int. Look closer at your loop. What is n = 0?
That's right it won't return anything. That is not allowed here.
public static int getFibonacci(int n)
{
FibonacciGenerator generator = new FibonacciGenerator() ;
int result = 0 ;
//----------------Start below here. To do: approximate lines of code = 4
// 1. Write a for-loop that calls the generator n times 2 . return the last result of the call.
for (int i = 1; i <= n; i++){
generator.next();
}
return result;
//put a return statement here, instead of in your loop.
}
TRY THIS CODE:
import java.util.*;
public class Fibonnacci{
public static void main(String args[]){
int num;
Scanner in=new Scanner(System.in);
System.out.println("Enter an integer");
num = in.nextInt();
System.out.println("Fibonacci Series");
int sum=0,a=0,b=1;
for(int i=0;i<num;i++){
System.out.print(" "+sum);
a = b;
b = sum;
sum = a + b;
}
}
}
I was trying to write a simple max and min method, as I wrote it I just cant help feeling it shouldn’t be this complicated….maybe Im wrong?
My maximum code works like this, excuse my poor pseudo code:
Fill an array with 10 random numbers.
Create a max variable initialised to 0, because 0 is the lowest max.
Compare each element against the max
If the element is greater then max, replace the value of max with the element in question
I don’t like the fact I have to initialise max to 0, I feel there might be a better way then this?
My min code works similar except I:
Compare my min is lower then the array element.
If the element is lower replace min.
What I really don’t like about this is I have to initialise my min to the maximum random number, in this case 50.
My questions are:
Is there a better way to do this?
Is there a more efficient way to write this code?
import java.util.Random;
public class Main {
public static void main(String[] args) {
//Declare min and max
int max=0;
int min;
//Array of 10 spaces
int[] ar=new int[10];
//fill an array with random numbers between 0 and 50
for(int i=0;i<10;i++)
{
ar[i]=new Random().nextInt(50);
}
//Test max algorithm
//loop trough elements in array
for(int i=0;i<10;i++)
{
//max is set to 0, there should always be a maximum of 0
//If there isnt 0 will be the maximum
//If element is greater then max
//replace max with that element
if(ar[i]>max)
{
max=ar[i];
}
}
System.out.println("The max is "+ max);
//Test min
//Initialising min to maximum Random number possible?
min=50;
for(int i=0;i<10;i++)
{
if(ar[i]<min){
min=ar[i];
}
}
System.out.println("The min is "+min);
}
}
You can always grab the first element of the array (i.e. numbers[0]) as the initial value and start the loop from the second element.
int[] numbers = new int[10];
int max, min;
...
min = max = numbers[0];
for(int i = 1; i < numbers.length; ++i) {
min = Math.min(min, numbers[i]);
max = Math.max(max, numbers[i]);
}
Ok, while others were already posting answers, I have taken the time to edit your code into something I think would be more usable.
Make static methods. Those can be reused.
Use an ellipsis (...) because you then can either call the methods on array arguments like in your code, but also with a variable number of arguments as min(5,3,8,4,1).
Initialize with the smallest/biggest possible number the data type provides
To check that your code works, you have to print out the items in the array first, since when you don't know what's in it, there's no way to tell the result is correct.
Base your code on the existing methods in the standard library because these are known to be thoroughly tested and work efficiently (I know, min/max looks like a too trivial example).
I wouldn't bother too much about performance unless you really can show there is a performance problem in your code. Priority should be more like 1st correctness, 2nd readability/maintainability, 3rd performance.
Most of this has been already mentioned by others, but anyway, here's the code:
import java.util.Random;
public class MinMax {
public static int min(int... args) {
int m = Integer.MAX_VALUE;
for (int a : args) {
m = Math.min(m, a);
}
return m;
}
public static int max(int... args) {
int m = Integer.MIN_VALUE;
for (int a : args) {
m = Math.max(m, a);
}
return m;
}
public static void main(String[] args) {
// fill an array with random numbers between 0 and 50
int[] ar = new int[10];
for (int i = 0; i < 10; i++)
{
ar[i] = new Random().nextInt(50);
System.out.println(ar[i]);
}
int maxValue = max(ar);
int minValue = min(ar);
System.out.println("The max is " + maxValue);
System.out.println("The min is " + minValue);
}
}
Few tips:
Initialize min with first element and start from the second:
int min = ar[0];
for(int i=1;i<10;i++)
...or start from:
int min = Integer.MAX_VALUE;
this approach is better if you expect your array can be empty.
Use Math.min to avoid explicit condition (some may say it's slower though):
for(int i=0;i<10;i++)
{
min = Math.min(min, ar[i]);
}
Initialize max to 0 & min to 50 won't work when the numbers change. A more appropriate way is:
1. initialize them to the first element of the array.
2. Use length instead of a constant.
max = ar[0];
for(i=0;i<ar.length; i++)
{
if(ar[i]>max)
{
max=ar[i];
}
}
Same for min:
min = ar[0];
for(i=0;i<ar.length; i++)
{
if(ar[i]<min)
{
min=ar[i];
}
}
public static void main(String[] args) {
int[] myArray = {9, 7,9, -40, -10, 40};
//int[] myArray = {};
//int[] myArray = {4};
System.out.println("Difference between max and min = "
+ findDifference(myArray));
}
// Find difference between Max and Min values for a given array
public static int findDifference(int[] arr) {
if (arr.length == 0) {
// Log
System.out.println("Input Array is empty");
return Integer.MIN_VALUE;
}
int min = arr[0];
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min)
min = arr[i];
else if (arr[i] > max)
max = arr[i];
// Just to check if logic works fine
System.out.println("Min=" + min + " Max=" + max);
}
return max - min;
}
import java.io.*;
public class MultiDimensionalArrayIO {
public static void main(String[] args)throws IOException {
BufferedReader c= new BufferedReader (new InputStreamReader (System.in) );
System.out.print ( "Enter Number Column : " );
int column = Integer.parseInt(c.readLine());
System.out.print ( "Enter Number Row : " );
int row = Integer.parseInt(c.readLine());
int array [][] = new int [column][row];
int max = array [0][0];
int min = array [0][0];
int sum= 0;
for ( int i=0 ; i < array.length; i++){
for (int j=0 ; j<array[i].length; j++){
System.out.print("Enter Array Values ["+i+"]["+j+"]: " );
array[i][j]= Integer.parseInt (c.readLine());
min = Math.min(min , array[i][j]);
max = Math.max(max , array[i][j]);
sum += array[i][j];
}
}
System.out.println("The Min Number :"+ min);
System.out.println("The Max Number :"+ max+ " total is "+ sum);
}
}
Depending on whether you'd want the max and min-functions in the same method you also have to consider the return type.
So far most suggestions have kept the two separate, meaning it's fine to return an int. However, if you put the max and min-functions into a findLargestDifference-method you'd have to return a long seeing as the largest difference between any given numbers in the int array can be the size of 2 ints. You'd also getting rid of having to loop over the int array twice.
Furthermore I recommend writing unit tests for corner and edge cases instead of printing in a main-method. It helps test your logic early on when implementing it and thus often makes the code cleaner.
See example code below.
public class LargestDifference {
public static long find(int[] numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Input cannot be null or empty.");
}else {
long currentMax = numbers[0];
long currentMin = numbers[0];
for (int i=0; i < numbers.length; i++) {
if (currentMin > numbers[i]) {
currentMin = numbers[i];
}else if (currentMax < numbers[i]) {
currentMax = numbers[i];
}
}
return currentMax - currentMin;
}
}