473,394 Members | 1,841 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,394 software developers and data experts.

Trying to create a triangle but with spaces leading the * (hope that makes sense)

Expand|Select|Wrap|Line Numbers
  1. ****
  2.  ***   
  3.   **
  4.    *
That is what i am trying to get, seems really simple but I have been through about a million variations of my code and am just getting really frustrated.

Expand|Select|Wrap|Line Numbers
  1.     for (j = 0; j < 3; j++)
  2.     {
  3.         for (i = 0; i < j; i++)
  4.         {
  5.         cout << " ";
  6.         }
  7.     cout << "*" << endl;
  8.     }
  9.  
Here is what i am using, thoughts?
Jan 22 '14 #1

✓ answered by Banfa

I assume you have tried what you have, looking at it it appears that it would put spaces before every * not just before the first one.

Think about the problem in a general way, its general structure should be

Expand|Select|Wrap|Line Numbers
  1. For Each Line
  2.     Output 0 or more spaces
  3.     Output 1 or more *
  4. EndFor
  5.  
Since you are using a loop to output the right number of spaces and the right number of * that suggests a code structure of

Expand|Select|Wrap|Line Numbers
  1. for() // For each line
  2. {
  3.   for() // the right number of spaces for this line
  4.   {
  5.       // output space
  6.   }
  7.   for() // the right number of * for this line
  8.   {
  9.       // output *
  10.   }
  11. }
  12.  
Compare that with your code and you will see you have 1 loop too few.

5 1101
Banfa
9,065 Expert Mod 8TB
I assume you have tried what you have, looking at it it appears that it would put spaces before every * not just before the first one.

Think about the problem in a general way, its general structure should be

Expand|Select|Wrap|Line Numbers
  1. For Each Line
  2.     Output 0 or more spaces
  3.     Output 1 or more *
  4. EndFor
  5.  
Since you are using a loop to output the right number of spaces and the right number of * that suggests a code structure of

Expand|Select|Wrap|Line Numbers
  1. for() // For each line
  2. {
  3.   for() // the right number of spaces for this line
  4.   {
  5.       // output space
  6.   }
  7.   for() // the right number of * for this line
  8.   {
  9.       // output *
  10.   }
  11. }
  12.  
Compare that with your code and you will see you have 1 loop too few.
Jan 22 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
Try a simple approach.

Start with an array of *****.
Display the array.
Change array[0] to a space.
Display the array.
Change array[1] to space.
Display the array.
etc...

There even simpler approaches than this.
Jan 22 '14 #3
donbock
2,426 Expert 2GB
Be careful to use an actual array, not a pointer to a string literal. From 6.4.5 in the C Standard "If the program attempts to modify such an array [a string literal], the behavior is undefined."
Jan 22 '14 #4
Expand|Select|Wrap|Line Numbers
  1. //object 4
  2.     for (j = 0; j < 3; j++)
  3.     {
  4.         for (i = 0; i < j; i++)
  5.         {
  6.             cout << " ";
  7.         }
  8.         for (k = 3 - 1;k > j;k--)
  9.         {
  10.             cout << "*";
  11.         }
  12.     cout << "*" << endl;
  13.     }
  14.  
Wow that was really easy after thinking about it as you put it. The whole time I was thinking of nesting a third loop inside the second. Would it be possible to do that way? Or what is the shortest way to write something like this? I just want to be able to see some other options to open my mind a little more. Thanks again for the help, last night i was super frustrated.
Jan 23 '14 #5
Banfa
9,065 Expert Mod 8TB
Putting the 3rd loop inside the second is unlikely to work because you would be executing the third loop multiple times for each line.

If you think about what you are outputting it is a grid of characters, that has 2 dimensions and again that suggests 2 loops or only 2 loops active at one time which is what your code does. The suggestion of only 2 loops leads to an alternate structure for the code though like this

Expand|Select|Wrap|Line Numbers
  1. For Each Line
  2.     For Each Character of the Line
  3.         If At or Past the Point Where * is Required
  4.             Output *
  5.         Else
  6.             Output Space
  7.         EndIf
  8.     EndFor
  9. EndFor
  10.  
Another exercise you could try would be to re-write your code without the variable k which is possible.
Jan 23 '14 #6

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

Similar topics

7
by: Tom | last post by:
I'm having a problem using a path with spaces as a parameter to os.rename() in a program on WinXP. This works fine at the command line (where the folder "c:\aa bb" exists) > os.rename( "c\aa...
1
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration...
45
by: Debashish Chakravarty | last post by:
K&R pg.66 describes two situations when using goto makes sense. Has anyone here come across situations where using goto provided the most elegant solution. --...
7
by: Eric Johannsen | last post by:
Hi, My C# code is calling VB6 code, which expects all (fixed-length) strings to be padded with spaces. The strings are contained with a struct, something like this (attributes to simulate...
1
by: Wlliam Cornwill | last post by:
Hi, In VB6 you could declare and enumeration with spaces using the type syntax. I am now trying to implement an enum in VB.NET using this syntax and I am having an issue. Public Enum...
5
by: s99999999s2003 | last post by:
hi i have a dir that contains directories with names and spaces in between example rootdir | ----> ABC DEF A | ---> BDD SD N I wanted to touch a file with the same name as the directories...
3
by: IchBin | last post by:
I am trying to pad a number with spaces to the left. I can pad it with zeros as in this code: printf("<option value='%d'>%04s %s</option>n", $row->id, $row->NumberOfQuotes,...
3
by: Developer.Man4 | last post by:
Hi all, i've been investigating for quite a while for files that are mandatory to be deployed to the server for an asp.net v1.1 application to run, to make it more clear, we don't need to move...
5
by: yoni | last post by:
Hi, I am trying to write a regexp to find all the code on the header of entities in SQL Server (views, SPs, etc...) I got something like this: (.|\n)*((create view)|(create proc)|(create...
5
kapura
by: kapura | last post by:
I found the syntax to change a user password through command prompt. I am using: net user username password I have sucessfully changed the password to a regular password. I can not figure out how...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.