954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Printing a diamond

Hello!
I have made a program that prints a diamond shape. But my program should read an odd number in the range 1 to 19 to specify the number of rows in the diamond, then it should display
a diamond of the appropriate side in which it is at present. I am a beginner. I don't know how to do that. Can any one help? I am in a need of it...!

#include <iostream.h>
#include <conio.h>

void main()
    {
    int i, j, k;
    for(i=1; i<=5; i++)
        {
        for(j=1; j<=5-i; j++)
        cout << " ";
        for(k=1; k<=2*i-1; k++)
        cout << "*";
        cout << endl ;
        }
        int l,m,n;
        for (n = 4; n > 0;n--) 
        {
        for (l = 1; l <= 5- n; l++)
        cout<<" ";
        for (m = 1; m<= 2 * n- 1; m++)
        cout<<"*";
        cout<<endl;
        }
            getch();
       }
the great
Newbie Poster
10 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Here is one of my beginning projects last year :

const int row = 1;

	for(int i = 0; i < row; i++)
	{
		for(int j = row-i; j ; j--)
			cout<<" ";
		
		for(int k = i*2; k >= 0; k--)
			cout<<"*";	

		cout<<endl;
	}

	for(int i = row; i >= 0; i--)
	{
		for(int j = i; j < row; j++)
			cout<<" ";
		
		for(int k = i*2; k >= 0; k--)
			cout<<"*";	

		cout<<endl;
	}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This might help you

#include <iostream>
#include <conio.h>

using namespace std;

int main()//you should use int main not void main
    {
    int i, j, k, rows;
    cout<<"Specify the number of rows (between 1-19) in the dimond: ";
    cin>>rows;
    cout<<endl;
    for(i=1; i<=rows; i++)
        {
        for(j=1; j<=rows-i; j++)
        cout << " ";
        for(k=1; k<=2*i-1; k++)
        cout << "*";
        cout << endl;
        }
        int l,m,n;
        for (n = rows-1; n > 0;n--)
        {
        for (l = 1; l <= rows- n; l++)
        cout<<" ";
        for (m = 1; m<= 2 * n- 1; m++)
        cout<<"*";
        cout<<endl;
        }
            getch();
return 0;//since i used int main i returned a value
       }


Didnt changed much so you can understand it

poncho4all
Light Poster
34 posts since Jul 2009
Reputation Points: 10
Solved Threads: 2
 

Well, Its quite simple, Use the same formula that you have in the existing code, But just change all the instances of '5' with a variable.

For Eg:

for(i=1; i<=5; i++)

This could be something like/

int len=5;
for (i=1;i<=len;i++)


Now you will then need to further need to take in the value of len from the user,.

Check if it lies between 1 - 19 and then look if it is odd. with the % operator. .

Then if all those conditions are satisfied. continue with the for loops or display an message stating that you have entered a wrong value or . Put a default value for len and continue.poncho4all Your effort of posting down the code is much appreciated, However This forum tends to provide paths to the solution, But not to the solution itself. Read the rule Here. .

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

I love the idea but can you please explain this in c for me.

osei-akoto
Newbie Poster
12 posts since Jul 2009
Reputation Points: 9
Solved Threads: 0
 

its good that you liked the idea but tell me what do you want to understand?

the great
Newbie Poster
10 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Please i want to know how this can be solved in c.
So i can also play around it.

osei-akoto
Newbie Poster
12 posts since Jul 2009
Reputation Points: 9
Solved Threads: 0
 

Well,

To get this program running in C, there are only a few minor editions that have to be done.

Firstly,

header must be replaced with the standard 'C' Header file 'stdio.h'

Next consider all cout<< statements as printf statements, Note that cout <> refers to scanf()..

Now since you have a brief Idea. Try experimenting and get to the solution.
And maybe from the next-time, Start a new thread instead of Continuing this one.

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You