Solved

Problems with for loop to output diamond shape

Posted on 2007-12-05
3
2,994 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

632 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