Well, I'm a college student with a dread C++ online class and now my (insert explicative) professor wants us to write a program for a diamond. The actual assignment is...

Write a program that displays a diamond of given the odd length (an odd integer
size less than or equal to 21). Your program should accept as input from the
keyboard the character used to form the diamond, and the value for the length of
the diamond.

Out of range input values should result in an informative error message (if it does
not meet the specifications of this assignment).

Your program should process only one input case, legal or not. Termination occurs
after either the error message is printed or the diamond is drawn.

It is necessary that the program be written using loops.

Sample runs:

Diamond drawing program
Enter the character, and length (eg. x 7):A 7
A
A A
A A
A A
A A
A A
A

Diamond drawing program
Enter the character, and length (eg. x 7):R 17
R
R R
R R
R R
R R
R R
R R
R R
R R
R R
R R
R R
R R
R R
R R
R R
R

Diamond drawing program
Enter the character, and length (eg. x 7):A 27
You have entered an illegal value for the length. It must be between 1 and 21.

You should use a named constant (const) to define and refer to the diamond size
limit. This will make it easy to modify the program at a later date if the size
restriction is changed. Write a class called Diamond. Have the appropriate data
members for that class. Also have a constructor and a function to output the
diamond.


I'm not asking for the answer but if anyone could point me in the right direction I'd appreciate it. I'm completely lost and the son of a ***** refuses to help despite the fact he never went over this and its not in the text books.

-eyeroll-

But yeah, if anyone can help please do. Thanks!

Alex

...but how do REALLY feel about the guy? ;-)

The diamond could be as simple as a sqaure on its side.
what does he mean by lemgth? inches? cm? pixels?
the most basic diamond is:
*
* *
*
(this is double spced, so it should be tighter than this)
Test for the proper input, (it needs to be odd for a center point), then display what is required per line.
Not too difficult at all, just requires a minute of thought...

He really is evil, I swear. And I'm seriously not sure what he means by length. The diamonds didn't come out when I copy and pasted so I assume its talking about just the perimeter of the diamond (its not filled, just a common outline of the diamond.) And as far as I know, we really haven't gone in squares either. The man's sick, throws out something no one knows anything about and refuses to help.

This looks like a slightly tougher version of a typical nested loops problem.
It seems the forum is squishing your output, so just to be clear, you expect something like :
..x..
.x.x.
x...x
.x.x.
..x..
(where the periods would be printed as blanks.) Have I got this right? In this case, length indicates the number of rows and the maximum width of the (center) line.

You need to use an outer for loop limited by the input size.
In the inner for loop, also limited by size, you need a method of determining if you display a symbol or blank. This would a relationship between half of current value of outer loop variable (the midpoint of the line) and the current value of inner loop variable.

It's nice to hear someone badmouth their prof worse than my students do me. I think?!

Member Avatar for iamthwee

This looks like a slightly tougher version of a typical nested loops problem.
It seems the forum is squishing your output, so just to be clear, you expect something like :
..x..
.x.x.
x...x
.x.x.
..x..
(where the periods would be printed as blanks.) Have I got this right? In this case, length indicates the number of rows and the maximum width of the (center) line.

You need to use an outer for loop limited by the input size.
In the inner for loop, also limited by size, you need a method of determining if you display a symbol or blank. This would a relationship between half of current value of outer loop variable (the midpoint of the line) and the current value of inner loop variable.

It's nice to hear someone badmouth their prof worse than my students do me. I think?!

[code]

[/code] tags would also help to preserve the formatting :P

*
 *   *
   *

Alright, well I got the program to work (as in it counts the right amount of numbers to make the diamond/rejects larger numbers) but I can't change the * that I used originally to make the code to the user specified characters. What I have so far is this...

//Assignment: 5;

#pragma once

class Diamond
{
public:
	Diamond(void);
	~Diamond(void);
};
#include <iostream>
#include <string>

using namespace std;
using std::string;
using std::cout;
using std::cin;
using std::endl;

int main(void)
{

int MyLength; int i,n,j,k,l; char MyCharacter; 


cout << "Enter character in order to outline the diamond: "; 
cin >> MyCharacter;

cout << "Enter an integer between 0 and 21, odd: "; 
cin >> MyLength;

string spaces( MyLength-2, ' '); 


if (MyLength <= 21)
{ 
	for(i=1;i<n;i++)
{
for(j=i;j<n;j++)
{
cout<<" ";
}
if(i==1)
{
cout<<"*\n";
} 
else
{
cout<<"*";
for(k=2;k<((2*i)-1);k++)
cout<<" ";
cout<<"*\n";
}
}
n-=0;
l=n;
for(i=1;i<=n;i++)
{
for(j=1;j<i;j++)
{
cout<<" ";
}
if(i==n)
{
cout<<"*";
}
else
{
cout<<"*";
l-=1;
for(k=0;k<((2*l)-1);k++)
{
cout<<" ";
}
cout<<"*\n";
}
}
cout<< endl; 
	}

else 
cout << "You have entered an illegal integer as the length." << "It must be between 1 and 21." <<endl << endl;

return 0;
}

Its probably ridiculously long/messy/who knows but I'm totally lost and by sheer chance I got it. I just need the cout <<* to change into "MyCharacter" so that it actually prints the character. Any ideas?

Maybe when you see a line like this:

cout << "*";

replace it with this?

cout << MyCharacter;

I've actually been trying that (it seems only right after all) but everytime I do that the "diamond" becomes a jumble of letters and numbers. Its a possessed program.

Initialize n.

You sure the code you posted works? Variable "n" is used several times without being initialized.

And frankly, even when indented properly, the code seems more complicated than it need be. Granted, this is not a really easy one to get right. Here's a solution sample. It probably can be made simpler, but I'm not seeing it right now.

#include <iostream>
using namespace std;

int main(void)
{

    int len, i, j;
    char ch = '#';

    len = 11;

    for( i = 0; i < len; i++ )
    {
        for( j = 0; j < len; j++ )
        {
            if( i < len/2 ) //top half of diamond
            {
                if( j == len/2 - i || j == len/2 + i )
                    cout << ch;
                else
                    cout << "_";
            }
            else    //bottom half of diamond
            {
                if( j == i - len/2  || j == len -(i -len/2) -1  )
                    cout << ch;
                else
                    cout << "_";
            }
        }
        cout << endl;
    }

    return 0;
}
#include <iostream>
using namespace std;
void main()
{
    int row=1, space=1, asterisk=1;


    while(row<=5)
    {
        while( space<=5-row)
        {cout <<" ";
        space++;
        }
        space=1;
        while( asterisk<=2*row-1)
        {cout <<"*";
        asterisk++;
        }
        asterisk=1;
        cout <<endl ;
        row++;
    }
    row=4;
    while( row>=1)
    {
        while( space<=5-row)
        {cout << " ";
        space++;
        }
        space=1;
        while( asterisk<=2*row-1)
        {       
        cout << "*";
        asterisk++;
    }
        asterisk=1;
        cout << endl ;
        row--;
    }
}
#include <iostream>
using namespace std;
void main()
{
	int row=1, space=1, asterisk=1;


	while(row<=5)
	{
		while( space<=5-row)
		{cout <<" ";
		space++;
		}
		space=1;
		while( asterisk<=2*row-1)
		{cout <<"*";
		asterisk++;
		}
		asterisk=1;
		cout <<endl ;
		row++;
	}
	row=4;
	while( row>=1)
	{
		while( space<=5-row)
		{cout << " ";
		space++;
		}
		space=1;
		while( asterisk<=2*row-1)
		{		
		cout << "*";
		asterisk++;
	}
		asterisk=1;
		cout << endl ;
		row--;
	}
}

if I wont to make an empty squre c++ using loops

*  *  *  *  *  *
*              *
*              *
*              *
*  *  *  *  *  *

thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.