473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Nested Loops And Other Help

Reika
5
Hello,
I need help understanding some things in programming. I'm taking a high school level programming course, however my teacher is less than helpful in explaining or even understanding the assignments he gives us himself.

I'm unsure what version of Codewarrior we use, but its Metroworks Codewarrior, Windows not mac, and this is in C++.

My first problem is understanding how to use nested for loops in order to display a (crooked) diamond of stars. I so far have:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int i, j, k, l, m, n, o, p, q, r;
  4.  
  5.     for (i = 1; i < 10; i++)
  6.     {    for (j = 1; j < (i=1); j++)
  7.         {cout << "*";}
  8.     cout << endl;} 
  9.     for (k = 10; k > 1; k--)
  10.     {    for (l = k; l < (k-1); l--)
  11.         {cout << "*";}
  12.     cout << endl; }
  13.     for (m = 1; m < 10; m++)
  14.     {    for (n = m; n < (m-1); m++
  15.         { cout << "*";}
  16.     cout << endl; }
  17.  
  18.     return 0;
  19. }
Now, I understand this probably looks incoherent to those of you that understand these loops, but I am having trouble. I am trying to use the outer for loop to make the *'s and the inner to set the spaces. But I have a feeling I am doing something wrong. I don't know the error code at the moment as I do not have codewarrior on my home computer.

Secondly, I need to write a program that uses loops to reduce a fraction to its lowest possible form. I'm having trouble planning this out, if anyone can help with getting started on it, I would appreciate it. I really only have this:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int integer, divisor, remain, counter;
  4.  
  5.     divisor = 1;
  6.     integer = 1;
  7.     remain = integer%divisor;
  8.  
  9.     if (remain != 0;)
  10.     {
  11.  
  12.  
  13.     return 0;
  14. }
Thanks in advance. ^^;
~Reika
Jan 10 '07 #1
5 2444
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int i, j, k, l, m, n, o, p, q, r;
  4.  
  5.     for (i = 1; i < 10; i++)
  6.     {    for (j = 1; j < (i=1); j++)
  7.         {cout << "*";}
  8.     cout << endl;} 
  9.     for (k = 10; k > 1; k--)
  10.     {    for (l = k; l < (k-1); l--)
  11.         {cout << "*";}
  12.     cout << endl; }
  13.     for (m = 1; m < 10; m++)
  14.     {    for (n = m; n < (m-1); m++
  15.         { cout << "*";}
  16.     cout << endl; }
  17.  
  18.     return 0;
  19. }
  20.  
Firstly Formating
If you format the layout of your code well it will be easier to read and maintain. Of particularly importance is putting you braces {} in a sensible place.

2 common methods are opening brace { on the same line of code that starts the new code block, closing brace } on a separate line like this
Expand|Select|Wrap|Line Numbers
  1. if (someCondition) {
  2.     // Some code here
  3. }
  4.  
and opening and closing brace on separate lines like this
Expand|Select|Wrap|Line Numbers
  1. if (someCondition)
  2. {
  3.     // Some code here
  4. }
  5.  
Code inside the code clocks should be indented 1 level

Personally I like the second form.


Compile this and you get lots of errors on cout, you need to include the right header, iostream, and you need to use the right namespace.

Doing these 2 things to your code you get

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int i, j, k, l, m, n, o, p, q, r;
  8.  
  9.     for (i = 1; i < 10; i++)
  10.     {    
  11.         for (j = 1; j < (i=1); j++)
  12.         {
  13.             cout << "*";
  14.         }
  15.  
  16.         cout << endl;
  17.     } 
  18.  
  19.     for (k = 10; k > 1; k--)
  20.     {    
  21.         for (l = k; l < (k-1); l--)
  22.         {
  23.             cout << "*";
  24.         }
  25.  
  26.         cout << endl; 
  27.     }
  28.  
  29.     for (m = 1; m < 10; m++)
  30.     {    
  31.         for (n = m; n < (m-1); m++
  32.         {
  33.             cout << "*";
  34.         }
  35.  
  36.         cout << endl; 
  37.     }
  38.  
  39.     return 0;
  40. }
  41.  
If you compile this you get a single error because you have left a ) of the end of the last for statement.

However this does not result in a working program.

There is a logic error in the inner for loop of the first nested loop pair. The end condition for the loop is (i=1) what this means is that everytime this is execute i is reset to a value of 1 and this in turn means that the outter loop, dependent on i never reaches its end condition so you have an infinite loop. I guess you meant (i-1).

There is a logic error in the inner for loop of the second nested loop pair

for (l = k; l < (k-1); l--)

if you set l to the value of k it will never be < (k-1) because k < (k-1) is always false, I think you meant l = 10 as your initialiser.

There are 2 logic errors in the inner for loop of the third nested loop pair

for (n = m; n < (m-1); m++)

if you set n to the value of m it will never be < (m-1) because m < (m-1) is always false, I think you meant n = 1 as your initialiser.

Additionally you have m++ as the 3rd expression of the for loop, since n is never changed if the loop condition is true the loop will execute infinately, I think you meant n++.


making all these changes gives

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int i, j, k, l, m, n, o, p, q, r;
  8.  
  9.     for (i = 1; i < 10; i++)
  10.     {    
  11.         for (j = 1; j < (i-1); j++)
  12.         {
  13.             cout << "*";
  14.         }
  15.  
  16.         cout << endl;
  17.     } 
  18.  
  19.     for (k = 10; k > 1; k--)
  20.     {    
  21.         for (l = 10; l > (k-1); l--)
  22.         {
  23.             cout << "*";
  24.         }
  25.  
  26.         cout << endl; 
  27.     }
  28.  
  29.     for (m = 1; m < 10; m++)
  30.     {    
  31.         for (n = 1; n < (m-1); n++)
  32.         {
  33.             cout << "*";
  34.         }
  35.  
  36.         cout << endl; 
  37.     }
  38.  
  39.     return 0;
  40. }
  41.  
which produces the output

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. *
  4. **
  5. ***
  6. ****
  7. *****
  8. ******
  9. *******
  10. *
  11. **
  12. ***
  13. ****
  14. *****
  15. ******
  16. *******
  17. ********
  18. *********
  19.  
  20.  
  21. *
  22. **
  23. ***
  24. ****
  25. *****
  26. ******
  27. *******
I am not sure what output you wanted (you don't say) you indicate that you wish to space the output triangles but you have now code statements outputing space characters.
Jan 10 '07 #2
Banfa
9,065 Expert Mod 8TB
Secondly, I need to write a program that uses loops to reduce a fraction to its lowest possible form. I'm having trouble planning this out, if anyone can help with getting started on it, I would appreciate it. I really only have this:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int integer, divisor, remain, counter;
  4.  
  5.     divisor = 1;
  6.     integer = 1;
  7.     remain = integer%divisor;
  8.  
  9.     if (remain != 0;)
  10.     {
  11.  
  12.  
  13.     return 0;
  14. }
You should start a second thread for this, 2 questions = 2 threads. Anyway I think this one will get quite busy enough.

I will add the hint that to simplify a fraction you have to find an integer that exactly divides both the numerator and denominator for instance in the fraction

23655/3474624

23655 and 2474624 are both divisible by 3 so a simplified form would be

7885/1158208

However I do not now if this fraction is in its simplest form, there may be another factor you can divide both sides by.
Jan 10 '07 #3
Reika
5
Thank you, and sorry for putting two questions in one thread.
I'm sorry for the bad formatting, I was taught the latter style you described but I have to type my programs at home on a word processor and it isn't so kind to my C++ formatting.

In any case, I appreciate your feedback as it really helps me to understand the for loops better. The output is not exactly what is needed but it is definitely better than where I was. I need an output like so:
Expand|Select|Wrap|Line Numbers
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********
  11.  
  12. **********
  13.  *********
  14.   ********
  15.    *******
  16.     ******
  17.      *****
  18.       ****
  19.        ***
  20.         **
  21.          *
  22.  
  23.          *
  24.         **
  25.        ***
  26.       ****
  27.      *****
  28.     ******
  29.    *******
  30.   ********
  31.  *********
  32. **********
  33.  
  34. **********
  35. *********
  36. ********
  37. *******
  38. ******
  39. *****
  40. ****
  41. ***
  42. **
  43. *
  44.  
Jan 10 '07 #4
Ganon11
3,652 Expert 2GB
I need an output like so:
Expand|Select|Wrap|Line Numbers
  1. *
  2. **
  3. ***
  4. ****
  5. *****
  6. ******
  7. *******
  8. ********
  9. *********
  10. **********
  11.  
  12. **********
  13.  *********
  14.   ********
  15.    *******
  16.     ******
  17.      *****
  18.       ****
  19.        ***
  20.         **
  21.          *
  22.  
  23.          *
  24.         **
  25.        ***
  26.       ****
  27.      *****
  28.     ******
  29.    *******
  30.   ********
  31.  *********
  32. **********
  33.  
  34. **********
  35. *********
  36. ********
  37. *******
  38. ******
  39. *****
  40. ****
  41. ***
  42. **
  43. *
  44.  
In order to get this output, think about each triangle separately:

Triangle 1:

There is no need to produce spaces here, as the ending spaces will 'appear' there with a newline. So you need to produce i stars, where i is the row line (a.k.a. 3rd row has 3 stars), and then a newline.

Triangle 2:

Spaces will have to be provided here to move the triangle to the right side. How many spaces will you need for the first row? For the second row? For the 5th row? For the nth row? Develop a rule to produce these spaces.

Next, how many stars are needed for the first row? For the second row? For the 5th row? For the nth row? Again, develop a rule to create the stars.

Finally, use a newline to go to the next row.

Triangle 3:

How many spaces do you need for the first row? For the second? For the nth?

How many stars do you need for the first row? For the second? For the nth?

Triangle 4:

How many stars do you need for the first row? For the second? For the nth?

No spaces needed here.

Answering these questions should give you a good idea for what your code must produce.
Jan 10 '07 #5
Banfa
9,065 Expert Mod 8TB
1 final tip, a lot of your for loops are of the form

for (i = 1; i < 10; i++)

This only produces 9 iterations of the loop with i value 1,2,3,4,5,6,7,8 and 9. To produce a loop with 10 iterations it is normal to write it as

for (i = 0; i < 10; i++)
Jan 10 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
34
by: sushant | last post by:
hi all, suppose i have 2 loops one inside the other, like this 1) for(i=0;i<=100;i++) { for(j=0;j<=10;j++) { some code; }
10
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.