Evaluating increases in a number range
Hello,
I'm working on a Java problem that has me completely stumped. I've been looking through all the references I can get my eyes on and just can't seem to find what it is that I'm looking for.
Basically, the problem I need to write a program for goes like this:
Write a program that will compute the largest increase that occurred in the price of a single stock within a 20 day time frame. The program should analyze any 20 integers that represent the stock's price during each of those days. For ex - 24, 59, 71, 31 ... 70 etc. Upong computing the increase the program should print a message that says something that looks something like this -
The largest increase of 25 dollars
from 50 dollars to 75 dollars
occurred between days 3 and 4.
Additionally this program must be designed so that the number of days can be changed by changing just a single constant (final variable) declaration. Also, if there is no increase at all, the program should notify the user.
With that said, I have come up with the following skeleton model that I'm trying to use. I haven't placed all the variables I need in it yet (this is one of my big problems which I'll explain below). Basically, it will consist of a final variable (constant N) and a for loop for entering the data for each day.
import java.util.*;
class Stock
{
public static void main (String [] args)
{
System.out.println("Enter the stock prices for the past 20 days to identify ");
System.out.println("when the largest increase (if any) occurred.");
final int N = 20; //N - number of days to be analyzed
Scanner keyboard = new Scanner (System.in);
for (int i = 1; i <= N; i++)
{
System.out.print("Value on day " + i + ": ");
}
System.out.println("Largest increase of ");
System.out.println("from to " );
System.out.println("occurred between day and day " );
}
}
Based off of the required output for this program, it seems to me that I will need to create variables for day 1 through day n as well as come up with a way to include a formula (simple subtraction ) in the for loop to compute the differences between each day. If I only had to use a hard set number of days, like 5, 10, 20 or whatever and didn't have to leave this program to be flexible in such a way that N could be the only variable that had to be changed to modify the program to analyze a different amount of days, I can think of different ways to get that output. But I have no idea how to even begin placing the above needed variables into this program that would adhere to this problems requirements. Is it even possible to have n about of variables in a program that exist solely depending on how many times a for loop is set to run? I imagine the comparison between increases would be done by simple < and > operators, but if thats the case, how would I even include them in this program if I'm not able to spell out the definate variables that have to be used? Could it be possible that I'm reading way into this problem? If anyone has any references in mind that they can point me too to give me more insight into this type of problem, any advice would be appreciated.
[3341 byte] By [
Tkoball] at [2007-11-20 11:42:22]

# 1 Re: Evaluating increases in a number range
You clearly need to spend some time learning the basic features of Java. The Java Tutorial (http://java.sun.com/docs/books/tutorial/index.html) is a good place to start.
If you have a fixed number of data items, and you know the size in advance, you can use an array to store them. If you don't know how to use arrays, read the tutorial.
To work out the information requested, you need to step through the array, comparing each item with the next. Now consider what information, besides the data values, you will need to hold while doing this. You'll need two variables for the values on the two days that have the biggest increase so far, and a variable for one of those day numbers (you can easily derive the other information to display from these variables).
For each step through the array, you make the comparison of two array values, and if you have a new biggest increase, you replace the old variable values with the current values and continue. At the end, you print out the required information using those values. Remember that arrays start at 0, not 1, so you might want to add one to the day numbers for display.
Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
A. Aho & J. Ullman
dlorde at 2007-11-10 2:14:02 >

# 2 Re: Evaluating increases in a number range
Dlorde,
Thank you for pointing me in the right direction. After delving into arrays I was able to work everything out and my program meets all of the requirements of the problem.
To my dismay, my teacher assistant has informed me that for whatever reason I should not be using an array for this problem because it is inefficient, storing more data than is necessary and that I should use only what has been covered in class so far. That isn't a whole lot because its only be going on for the past month now.
Im trying to figure out why she would say such a thing. After learning about arrays it seems to me to be an efficient way of handling problems like the one my program is designed to work with and seems to simplify things a great deal. Is there really a more efficient way to handle this that wouldn't include arrays? If not, then I'm just going to keep it as is and lose a few points if need be. I really can't see how I can streamline this program anymore. Any more pointers you may have would be much appreciated.
# 3 Re: Evaluating increases in a number range
Well arrays are the simplest and most efficient way of storing fixed-length data in Java (if not always the most convenient), so I can only assume your 'teacher assistant' means you shouldn't store the data at all.
If so, you could just keep track of the largest difference as the data is entered by the user - i.e. treat the user as your data 'array' and each user input as a step over that 'array'.
I suppose it's a simpler concept than doing the task in two stages, but it's clearly less robust and less flexible - by using an array, you've done everything that you'd need to do if you were processing the user input directly, and a little bit more.
I guess that either the system is so rigid and unimaginative that you're not permitted to touch any language features you haven't been told about yet, or your teacher assistant wants you to code the more primitive version so you can see the disadvantages of it, before moving on to a better design... I'd like to think the latter, but then I'm a hopeless cynic :rolleyes:
At least it will be simple for you to remove the unwanted extra code now you've got the array version working, and at this stage, all code modifications are a useful exercise, even if they seem like a backward step :rolleyes:
...methods are more important than facts. The educational value of a problem given to a student depends mostly on how often the thought processes that are invoked to solve it will be helpful in later situations. It has little to do with how useful the answer to the problem may be. On the other hand, a good problem must also motivate the students; they should be interested in seeing the answer. Since students differ so greatly, I cannot expect everyone to like the problems that please me...
D. Knuth
dlorde at 2007-11-10 2:15:58 >
