By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,166 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,166 IT Pros & Developers. It's quick & easy.

Nested Loops And Other Help

Reika
P: 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
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,950
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
Expert Mod 5K+
P: 8,950
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
P: 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
Expert 2.5K+
P: 3,652
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
Expert Mod 5K+
P: 8,950
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

Post your reply

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