Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Use a for loop for Efficiency
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
dmitrip




PostPosted: Wed Feb 21, 2007 12:30 pm   Post subject: Use a for loop for Efficiency

how can i use for loops in this program to make it much more efficient and shorter?

the point of the program is to read 5 numbers and output the biggest one.

thanks




[code]
var num1, num2, num3, num4, num5 : int

put "Please input 5 different numbers, separating them with spaces"
get num1, num2, num3, num4, num5



if (num1 > num2 and num1 > num3 and num1 > num4 and num1 > num5) then

put num1

elsif (num2 > num1 and num2 > num3 and num2 > num4 and num2 > num5) then

put num2

elsif (num3 > num1 and num3 > num2 and num3 > num4 and num3 > num5) then

put num3


elsif (num4 > num1 and num4 > num2 and num4 > num3 and num4 > num5) then

put num4

elsif (num5 > num1 and num5 > num2 and num5 > num3 and num5 > num4) then

put num5

end if


[code]
 
Sponsor
Sponsor
Sponsor
sponsor
rollerdude




PostPosted: Wed Feb 21, 2007 1:03 pm   Post subject: Re: Use a for loop for Efficiency

ic.... once, in our class, we were told to make a program that would an array of numbers in order from smallest to largest... the code is a bit longer than this tho... sry... i have no solution
 
bugzpodder




PostPosted: Wed Feb 21, 2007 1:31 pm   Post subject: RE:Use a for loop for Efficiency

use an array. use a for loop to iterate through the array.
 
Cervantes




PostPosted: Wed Feb 21, 2007 2:36 pm   Post subject: RE:Use a for loop for Efficiency

Bugzpodder is right, but some expansion is warranted.

Use an array to store the 5 numbers. Then create another variable, called 'current_high' or something like that, and set it equal to the first element of your array. Then iterate, using a for loop, from 2 to 5. Inside the for loop, if the element in the array at the current index is greater than 'current_high', set 'current_high' to be that element.

There's the complete solution. Converting that into code should not be hard at all.
 
BenLi




PostPosted: Wed Feb 21, 2007 7:19 pm   Post subject: RE:Use a for loop for Efficiency

again, the solutions you guys are suggesting is beyond his level.

Okay, so the basic idea is to go through and find the highest right. Well what makes it the highest? It is the highest when it is higher than all the other numbers. try to code this, and put up your attempts/
 
Clayton




PostPosted: Wed Feb 21, 2007 7:36 pm   Post subject: Re: Use a for loop for Efficiency

Hmmm.... Cervantes' solution is definately one of the quickest ways to do it, and only require little reading. Not very hard at all.
 
ericfourfour




PostPosted: Fri Feb 23, 2007 6:39 pm   Post subject: RE:Use a for loop for Efficiency

BenLi, you suggested exactly the same thing as cervantes. cervantes, however, wrote out the algorithm.

A better way to do this, after you have the array, is to sort the numbers. It is more complicated. However, you know which element in the array is the highest and you don't have to worry about an extra variable.

Or, if you want to take up less space and not worry about efficiency or arrays, you can do this:

Turing:
put max (num1, max (num2, (max (num3, max (num4, num5)))))
 
Cervantes




PostPosted: Sat Feb 24, 2007 12:40 am   Post subject: Re: RE:Use a for loop for Efficiency

ericfourfour @ Fri Feb 23, 2007 6:39 pm wrote:

A better way to do this, after you have the array, is to sort the numbers. It is more complicated. However, you know which element in the array is the highest and you don't have to worry about an extra variable.

I doubt I need to tell you this, but I'll say it anyways.
No, this statement is not correct. If all you want to do is find the biggest element in an array, then use the algorithm I outlined. All that it requires is one temporary variable and one pass through the whole array. It's O(n). Sorting, however, is at best O(n log(n)), and often as bad as O(n^2).
Sure, it does more for you, but in this specific problem no one cares if we can sort our array or not. All we want is the biggest element.
 
Sponsor
Sponsor
Sponsor
sponsor
BenLi




PostPosted: Sat Feb 24, 2007 1:21 am   Post subject: RE:Use a for loop for Efficiency

all I meant is that the likelihood of the original poster knowing arrays is low, since he is struggling with this question. Suggesting using techniques and tools that he does not possess is a waste of breath.
 
Clayton




PostPosted: Sat Feb 24, 2007 8:40 am   Post subject: Re: Use a for loop for Efficiency

BenLi, saying it's a waste of breath is not right. We are still helping him, and if he doesn't understand arrays, there's plenty of resources to help him understand. Just re-enforcing bad coding style is helping no one in the long run.
 
Abstraction




PostPosted: Sat Feb 24, 2007 12:01 pm   Post subject: Re: Use a for loop for Efficiency

Well the issue here is that the solution doesn't really seem to match the problem. Since it specifically asks to use a nested loop, I don't think it would be a really good idea to use an array. I think a simpler solution would be to do something like this:

var num : int
var biggest : int := 0

for i : 1 .. 5
put "Input number ", i
get num
if num > biggest
then biggest := num
end if
end for

put biggest

The question doesn't specifically ask to output each of the individual numbers, just the largest one, so I think this would be the most elegant way to come to the answer. However, unless you use the first number as the reference, it assumes that the numbers are > 0
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: