View Full Version : need an elegant solution to a problem
10-10-02, 11:35 PM
I am a bit stumped with this problem in pascal because the teacher won't let us use arrays(GRRR!!!). Its pascal, but I really just need the algorithm(ie english should be enough).
I need to compare 10 inputed strings and determine the longest and shortest. This is very simple with arrays and itteration, but I don't know how to do this without arrays without bruteforcing it and having a ton of if/thens. Any suggestions would be appreciated.
Hmm, You mean you cant use an array to store the strings also?
Ok, if no, can you use arrays for one other thing or arent you allowed to use arrays at all, cuz if thats the case, I bet it would be a pain in the a**. Anyways, can the assignment use any intermediate approach, or is it a basic program. cuz I guess you can use records if you arent allowed to use arrays.
10-11-02, 12:07 AM
This may be a silly reply but from what you said, you take in 10 strings and need to output the longest and shortest not caring about the other 8 strings that are in the middle. If this is the case, what about this:
Youre going to read in each value sequentially eh? Have a while loop with counter 10 so that you can read in 10 values. Have two variables, longest and shortest for example. When you read in the value you compare it to the two. There has to be some LENGTH function in pascal( i dont know pascal though so /shrug/) or depending on how the string comparison works that would return an integer if a<b or b<a. It seems like you should just be able to compare sizes from there in the loop.
10-11-02, 01:02 AM
That would work if there was a way to change the count variable to be name of the variable for the input. Thats what my problem is, in an array i just slap the counter i into the brackets for that array value, but anything else will be a problem because pascal variables MUST start with a character.
10-11-02, 08:30 AM
I got it, just had a loop and in it added a variable to concantenate strings together and then used that value. Still not as elegant or simple as using arrays, but worked.
you can always be really cruel and circumvent her no array policy by makign a linked list:D
I'm a bit late in giving the answer, but the strings are inputted. Just put the input in the loop. That way there is a new string in the same variable slot every time you go through the loop.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.