Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard network operating system printer problem ram registry router slow software sound svchost.exe trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
student having beginner reading Algorithm problems

Reply  
Thread Tools
Spmook's Avatar
Member with 99 posts.
 
Join Date: Feb 2005
Experience: Obsessive compulsive with my puter
26-Sep-2006, 11:36 PM #1
student having beginner reading Algorithm problems
HI guys and gals...
I recently enrolled in a Intro to Computer programming class and to be honest the prof is a turd. His lectures are non-informative and quite dull. I'm continusously having problems with basic things. for example I started out fine reading Algorithms then on my last homework I got a 24/50 on it. most of the points off were on being able to read Algorithms here were the problems that I had and the answers. even after getting the answers Im still puzzled at how he got them. I've googled algorithms and have not found any information on basic reading. Some help please and if anyone knows any good websites to learn algorithms and reading them it woul be handy also

1) N = 1234
D = 0
while N != 0
inc(D)
N = N div 10
end while

write "D = " D

2) N = 16
D = 1
while D < N
if N mod D == 0 then
write D ","
end if
end while

3) N = 24
D = N - 1
while N mod D != 0
dec(D)
end while
write D

4) D = 0
N = 100

writeln D
writeln N

while D < N div 2
inc(D)
if N mod D == 0 then
L = D
end if
end while


writeln L
write N


here's the answer key
1) D = 4
2) 1, 2, 4, 8
3) 12
4) 0
100
50
100

Last edited by Spmook; 27-Sep-2006 at 03:28 AM..
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
27-Sep-2006, 02:58 AM #2
These are very basic. It comes down to understanding numeric operations, conditionals and loops. The way you use the word algorithm indicates you are over thinking. (don't get too frustrated because that's a common issue with new coders).

There are two tricks that helped me "get it".

1) Use indentation to visually isolate the results of conditionals. Here are two examples (1 and 4):

Code:
example #1

N = 1234
D = 0
while N != 0
    inc(D)
    N = N div 10
end while

write "D = " D

Example #4

D = 0
N = 100

writeln D
writeln N

while D < N div 2
    inc(D)
    if N mod D == then
       L = D
    end if
end while

writeln L
write N

2) Write out the operations through the loop so you can follow it. Example #1 :

Code:
N = 1234
D = 0
while N != 0
    inc(D)
    N = N div 10
end while

write "D = " D

-----------------------------------------------
Beginning of Pass 1
N=1234 
D=0

N (1234) IS NOT equal to zero so the while conditional will be true, and the loop will continue
D (0) is incremented so D (0) will equal D (0) + 1 or 1 in this pass
N (1234) is divided by 10 so now equals 123.4 and the result set back to N (the test instructions probably say to assume that all variables are integers which cannot have a decimal place so the .4 is truncated and ignored)  N will thus equal 123

-----------------------------------------------
Beginning of Pass 2

N=123 
D=1

N (123) IS NOT equal to zero so the while conditional will be true, and the loop will continue
D (1) is incremented so D (1) will equal D (1) + 1 or 2 in this pass
N (123) is divided by 10 so now equals 12.3 and the result set back to N (the test instructions probably say to assume that all variables are integers which cannot have a decimal place so the .3 is truncated and ignored)  N will thus equal 12

-----------------------------------------------
Beginning of Pass 3

N=12 
D=2

N (12) IS NOT equal to zero so the while conditional will be true, and the loop will continue
D (2) is incremented so D (2) will equal D (2) + 1 or 3 in this pass
N (12) is divided by 10 so now equals 1.2 and the result set back to N (the test instructions probably say to assume that all variables are integers which cannot have a decimal place so the .2 is truncated and ignored)  N will thus equal 1

-----------------------------------------------
Beginning of Pass 4

N=1 
D=3

N (1) IS NOT equal to zero so the while conditional will be true, and the loop will continue
D (3) is incremented so D (3) will equal D (3) + 1 or 4 in this pass
N (1) is divided by 10 so now equals .1 and the result set back to N (the test instructions probably say to assume that all variables are integers which cannot have a decimal place so the .1 is truncated and ignored)  N will thus equal 0

-----------------------------------------------
Beginning of Pass 5

N=0 
D=4

N (0) IS equal to zero so the while conditional will be false , and the loop terminate
the write statement create an output of  "D = 4"
Hope that helps,

Dan
Spmook's Avatar
Member with 99 posts.
 
Join Date: Feb 2005
Experience: Obsessive compulsive with my puter
27-Sep-2006, 03:32 AM #3
Crap DAN!!!....ok so I didnt get the whole concept of Loops...I'm gonna try out the other 3 tonight and I really appreciate your help. I knew about the whole isolating indentation thing but the while and loops threw me for a loop.
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
27-Sep-2006, 03:56 AM #4
Happy to help.

Here are a couple of other "loops" that will yield the same answer (except for #3) to think through

Code:
Example 1
a = 1
for x = 1 to 10
    a = a * x
next x

write a


Example 2
a = 1
x = 0
do
   inc(x)
   a = a * x
until (x == 10)

write a

The following will not yield the same answer for a  

Example 3
a = 1
x = 0
do
   a = a * x
   inc(x)
until (x == 10)

write a

hint: a will equal 0 after all passes through the loop on this one
Spmook's Avatar
Member with 99 posts.
 
Join Date: Feb 2005
Experience: Obsessive compulsive with my puter
27-Sep-2006, 04:00 AM #5
Talking I think I've got it....
Thanks for the help dan....Hopefully understanding this stuf will be ab it easier for me now. My Prof is not meant to teach. hrees what I did for 2) I just kept finding out all the divisors of 16 wthout a remainder and wrote them down and since the while has to keep going unitl D is = or greater than 16 the farthest # i could go to was 8. no other # until 16 met the requirements for the loop to continue. and It comes out correct. in 3) as long as D doesnt have a remainder = 0 then the loop continues along with D losing 1. There fore the only # it has no remainder to is 12 after that the loop stops and stops the dec(D) leaving me with 12 for the answer. SO I think I am correct and once again Thanks a ton! I knew someone on here would know what I was talking about.
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
27-Sep-2006, 10:55 PM #6
Sounds like you "got it" - way to go.

Dan
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 02:10 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.