Solved

Problems with for loop to output diamond shape

Posted on 2007-12-05
3
2,977 Views
Last Modified: 2012-06-21
I am doing this exercise to output a diamond entering a size. I have messed around with this using a odd and a positive and didn't like that way of doing it. So I have been trying to get it to enter the size of the middle of the shape. What is wrong with my for loop it displays it all wrong.


#include <iostream>

#include <iomanip>
 

using namespace std;
 

int main()

{
 

	int size;
 

	cout << "Enter a size of the middle of the diamond: ";

	cin >> size;

	cout << endl;
 

 for (int i = 1; i < size; i = i + 1)

 {

   cout << setw(size);

    for (int j = 0 ; j < i; j++)

     cout << "* ";

       cout << endl;

        --size;

        }

          for (int i = 1; i < 4; i = i + 1)

        {

                cout << setw(size + 2);

                 for (int j = 4 ; j > i; j--)

                  cout << "* ";

                   cout << endl;

                    ++size;

        }

}

Open in new window

0
Comment
Question by:jschmuff
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 350 total points
ID: 20416946
some comments:
to print a diamond you will need a main loop from 0 to size-1:
for (int i=0; i<size; i++)

then you will need two loops inside, one to print spaces, and another loop to print the diamond
Looking for the diamond below, size = 9
So, you will need to deduce 2 equations:
1) calculate the white spaces, based on i
2) calculate the diamond marks, based on i

in the example first space is 4 and body is 1
then space decrements and body increments

when i = (size-1)/2, then body = size, and space = 0
I would use something like:
body = abs(i-(size-1)/2+1)
space = abs((size-1)/2 - i)

to write the space or body, would be simpler to print char by char, like this:
for (j=0; j<body; j++)
     cout << '#';

well, hope all this information will be enough to help you complete your homework

    #

   ###

  #####

 #######

#########

 #######

  #####

   ###

    #

Open in new window

0
 

Author Comment

by:jschmuff
ID: 20416963
This isn't actually my homework assignment, but it is similar. Thanks though I will go over what you said and try and fix what I have.
0
 

Author Closing Comment

by:jschmuff
ID: 31413038
Thanks for the help again you could have just in plain text said what was wrong with my code cause it was all messed up it was printing out a diamond but not correctly and wrong size. Instead of me figuring out what you were saying. I have bad reading comprehension.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
multi threaded debug dll option in visual studio 2 96
how to understand recursion 12 219
returning a dereferenced pts in C++ 10 142
GUI: DIalog Stacking and Popping in MS C++ 4 65
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now