Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Stupid Question
Author Message
swiftkillah

Posted: Sun Jan 25, 2004 4:08 pm   Post subject: Stupid Question

Im taking basic Turing lessons in my Grade 10 computer science class. The problem is I suck ass at it and don't understand some of the basics. Im reveiwing for my exam and have looked through a couple past tests. I will post the questions that I coudn't get and would really appreciate if someone could explian them to me. thanks.

1. How many times will "Hello World" be outputed in the following example?

for I: 1..10
for J : 1..5
put "Hello World"
end for
end for

2. What will the following program output?

for I :3 .. 12 by 2
if I mod 3=1 then
put I..
end if
end for

3. How would u make the previous program output only even numbers?

4. how would u make the previous program make the numbers go backwards?

i really ned to leanr how to do these kinds of questions. Thx in advance!

Delos

Posted: Sun Jan 25, 2004 4:15 pm   Post subject: (No subject)

I will emulate them so you can come up with the answers yourself.

A for loop is a counted loop.

Therefore:
for i : 1..10
end for

runs 1..10[inclusive] = 10 times.

If you have more than 1 for loop, then use this analogy:
A for loop is like a basket of eggs [/candy/pens/CDs]. Each basket has a specified number of eggs [/etc] in it. If I have 1 basket of 10 eggs, I have 10 eggs. But if I have 4 of these baskets, how many eggs do I have? Remember than the number of eggs in each basket will not change, just the number of baskets.

This works well for a single nested for-loop. More? Just add more layers to the analogy.

Enjoy.
I'm going to let others pick apart your other q's. I'm a little surprised that your Gr10 compSci teacher would introduce you to so much...ah, well, I guess I just had a less-than-perfect gr10 compSci teacher.
McKenzie

Posted: Sun Jan 25, 2004 4:26 pm   Post subject: (No subject)

#1 is called a nested for loop
what you need to be able to see is that the inner for will loop fully for EACH time the outer for loops. Now in this case it's a total of 50. This usually helps clear things up:
 code: for I: 1..10      for J : 1..5           put I:3,J:5      end for end for

#2 There are two things here. Do you know what mod does? and do you know how to trace a program?
mod
~~~
mod is the remainer you get with integer division. So
5 mod 2 = 1
20 mod 3 = 2
When you trace a proram make a table and try to follow the program one line at a time. If I traced that program it would look like:
 code: I | I mod 3 ||  Output ---------------------------  3 |    0    ||  5 |    2    ||     7 |    1    ||    7  9 |    0    ||    11 |    2    ||
Andy

Posted: Sun Jan 25, 2004 5:00 pm   Post subject: (No subject)

ohhh great... the new semester started...

Posted: Sun Jan 25, 2004 5:02 pm   Post subject: (No subject)

I advise nobody to give directly the answers out...and good job for explaining guys.
swiftkillah

Posted: Sun Jan 25, 2004 5:20 pm   Post subject: (No subject)

no no these are the questions on a test i took last month. i just want the answers to help me study for the exam

btw thx for the help guys
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 6 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: