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: - int main()
-
{
-
int i, j, k, l, m, n, o, p, q, r;
-
-
for (i = 1; i < 10; i++)
-
{ for (j = 1; j < (i=1); j++)
-
{cout << "*";}
-
cout << endl;}
-
for (k = 10; k > 1; k--)
-
{ for (l = k; l < (k-1); l--)
-
{cout << "*";}
-
cout << endl; }
-
for (m = 1; m < 10; m++)
-
{ for (n = m; n < (m-1); m++
-
{ cout << "*";}
-
cout << endl; }
-
-
return 0;
-
}
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: - int main()
-
{
-
int integer, divisor, remain, counter;
-
-
divisor = 1;
-
integer = 1;
-
remain = integer%divisor;
-
-
if (remain != 0;)
-
{
-
-
-
return 0;
-
}
Thanks in advance. ^^;
~Reika
5 2444 Banfa 9,065
Expert Mod 8TB -
int main()
-
{
-
int i, j, k, l, m, n, o, p, q, r;
-
-
for (i = 1; i < 10; i++)
-
{ for (j = 1; j < (i=1); j++)
-
{cout << "*";}
-
cout << endl;}
-
for (k = 10; k > 1; k--)
-
{ for (l = k; l < (k-1); l--)
-
{cout << "*";}
-
cout << endl; }
-
for (m = 1; m < 10; m++)
-
{ for (n = m; n < (m-1); m++
-
{ cout << "*";}
-
cout << endl; }
-
-
return 0;
-
}
-
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 -
if (someCondition) {
-
// Some code here
-
}
-
and opening and closing brace on separate lines like this -
if (someCondition)
-
{
-
// Some code here
-
}
-
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 -
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
int i, j, k, l, m, n, o, p, q, r;
-
-
for (i = 1; i < 10; i++)
-
{
-
for (j = 1; j < (i=1); j++)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (k = 10; k > 1; k--)
-
{
-
for (l = k; l < (k-1); l--)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (m = 1; m < 10; m++)
-
{
-
for (n = m; n < (m-1); m++
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
return 0;
-
}
-
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 -
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
int i, j, k, l, m, n, o, p, q, r;
-
-
for (i = 1; i < 10; i++)
-
{
-
for (j = 1; j < (i-1); j++)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (k = 10; k > 1; k--)
-
{
-
for (l = 10; l > (k-1); l--)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
for (m = 1; m < 10; m++)
-
{
-
for (n = 1; n < (m-1); n++)
-
{
-
cout << "*";
-
}
-
-
cout << endl;
-
}
-
-
return 0;
-
}
-
which produces the output -
-
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
********
-
*********
-
-
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
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.
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: - int main()
-
{
-
int integer, divisor, remain, counter;
-
-
divisor = 1;
-
integer = 1;
-
remain = integer%divisor;
-
-
if (remain != 0;)
-
{
-
-
-
return 0;
-
}
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.
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: -
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
********
-
*********
-
**********
-
-
**********
-
*********
-
********
-
*******
-
******
-
*****
-
****
-
***
-
**
-
*
-
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
********
-
*********
-
**********
-
-
**********
-
*********
-
********
-
*******
-
******
-
*****
-
****
-
***
-
**
-
*
-
I need an output like so: -
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
********
-
*********
-
**********
-
-
**********
-
*********
-
********
-
*******
-
******
-
*****
-
****
-
***
-
**
-
*
-
-
*
-
**
-
***
-
****
-
*****
-
******
-
*******
-
********
-
*********
-
**********
-
-
**********
-
*********
-
********
-
*******
-
******
-
*****
-
****
-
***
-
**
-
*
-
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.
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++)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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;
}
|
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++)
{
.....;
.....;
}
|
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 = ...;
....
}
}
|
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....
|
by: Fredrik Lundh |
last post by:
Patrol Sun wrote:
so why exactly are you trying to nest 20 or 100 for-in loops?
</F>
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |