-
help needed
hi guys ...i can,t get to display dis an empty diamond using setw...i,ve written some ov the code..its incomplete..cant get any further..please help! i,ve to submit it in 6 hours.
Code:
*
* *
* *
* *
* *
* *
*
#include <iostream.h>
#include <iomanip.h>
int main()
{
int a,b,c,d,e,x,y,z;
cout<<"enter no. of rows"<<endl;
cin>>a;
for (x=1; x<=a; x++)
{
cout<<setw(x)<<"*"<<endl;
}
for (y=10; y>=a; y--)
{
cout<<setw(y)<<"*"<<endl;
}
return 0;
}
-
Unfortunately that code won't compile on my compiler, your header files are outdated and non-standard. If your class allows it you should switch to <iomanip> and <iostream>, and you'll have to add a using namespace std; line below the #includes.
I also can't read your output since it is in code tags and all whitespace is lost. Can you post the output in code tags to maintain the whitespace?
Finally, while you're waiting for an answer check out the forum rules and posting guidelines. Things like putting "urgent" in the subject and not using proper grammar and spelling tend to make people not want to help.
-
ok i think you will be able to run it now .here is the code and output i get
Code:
*
*
*
*
*
*
*
*
*
*
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a,x,y;
cout<<"enter no. of rows"<<endl;
cin>>a;
for (x=1; x<=a; x++)
{
cout<<setw(x)<<"*"<<endl;
}
for (y=10; y>=a; y--)
{
cout<<setw(y)<<"*"<<endl;
}
return 0;
}
-
Notice that other than the first and last lines, you print two asterisks per line. You probably should use variable names that are more descriptive than a, b, c, d, e, x, y, z.
-
Look at the picture. Now imagine if you combined the two loops, it would look like the bottom half of the diamond (or at least close to it), right? See if you can combine the two to output two asterisks per line as laserlight suggested, and once you get the bottom half of the diamond you can figure out how to do the top half.
-
thats the problem .i can,t combine the two
-
>> thats the problem .i can,t combine the two
How have you tried?
-
offcourse i have tried .i told you i can,t get any further, i am a high school student,just a begginner
-
I said "How have you tried?" I know you tried, but the best way to get help is to make an attempt, and then post that attempt and ask specific questions about your attempt.
So, show us your latest attempt to combine the two loops and output more than one asterisk per line. Also tell us how its not working.
-
Combine the loops as in to nest them so they work together
-
I would do that in one loop… something like
Code:
cout << setw(x) << "*" << setw(f) << "*" << endl;
where x is the distance from the left side of the screen to the first * and f is the gap between the stars.
And then just come up with an algorithm that increases the values (x & j) accordingly… and then decreases them
-
Most C++ compilers don't like “ and ”. " is the proper character. ;)