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

Newbie need help on HW

My assignment is to write a program that produces a diamond when the user inputs an integer, if the integer is even it should be increased to the next odd number.

if the number is 7, it should print:

___*
__***
_*****
*******
_*****
__***
___*

(Without the underscores.)

So far I am able to produce the following output with the code I have written:

Enter number: 7
3


*
**
***
Press any key to continue


#include
using std::cout;
using std::cin;
using std::endl;

int main()
{

int n;

cout<<"Enter number: ";
cin>>n;

cout<

mrlucky0
Newbie Poster
3 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

Greetings.
Sorry, I didn't have time to really sit down and think.
Can't get 4 for loops, but I've got a while nested with 3 for loops.
Take it as a reference if you like.
I'll spend some more time to come out with one with 4-For loops. :cheesy:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

     int num, odd, increment, space;
     bool go;

     num = 0;
     increment = 1;
     odd = 1;
     go = false;

     do{
          cout<<"Enter number: ";
          cin>>num;
     }while(num<3);

     if(num%2==0)
          num = num + 1;

     while(odd<=num)
     {
          space = num/2 - odd + increment;
          if(go==false)
               increment += 1;
          else
               increment -= 1;
   
          for(int x=0; x<space; x++)
               cout<<" ";
		
          for(x=0; x<odd; x++)
               cout<<"*";

          for(x=0; x<space; x++)
               cout<<" ";
          cout<<endl;

          if(go==false)
               odd += 2;
          else
               odd -= 2;
          if(odd>num && go==false)
          {
               odd = num - 2;
               increment = num/2;
               go = true;
          }
          if(odd<0)
               odd = num + 1;
     }

     return 0;
}

Hope this helps.

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

red_evolve,you really helpful person.
actually my college is having assignment soon and the title is address book system,since i am very fresh in programming,hope can get help from u too... :idea: hope meet you soon

Eddie
Newbie Poster
3 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 
red_evolve,you really helpful person. actually my college is having assignment soon and the title is address book system,since i am very fresh in programming,hope can get help from u too... :idea: hope meet you soon

I agree, thanks for replying. I can't belive you went out of your way to write that code. I'm not sure if I understanding all the logics of it so I'll continue to study it and pursue the 4 for loops method. I've gotten a bit further already.

mrlucky0
Newbie Poster
3 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

Greetings.
No problemo.
To Eddie,
I'm ready to help as long as you show some effort. ;)
To mrlucky0,
Sorry for being unable to come out with the exact result you wanted. Try to post
what you can get and I'll be sure to help out.

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

hi mrlucky0, heres a similar prog.chk this also out.i feel this is quite simple than that of red_evolves.

#include
#include
void main()
{
clrscr();
int n,k,no,i,j,a,b,c;
cout<<"\nenter n:";
cin>>n;
if(n%2==0)
n++;
for(i=1;i<=n;i+=2)
{
for(j=i;j=1;i-=2)
{
for(j=i;j

3maddy3
Newbie Poster
7 posts since Jul 2004
Reputation Points: 12
Solved Threads: 0
 

Yes, I was attempting to write a program similar to that. Looks like you found it. Eventually I was able to figure it out. Here's my rendition, which is not too different. But I swear, I did it on my own!

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

	int num, p=1;

	
	cout<<"Enter number: ";
	cin>>num;

	if(num%2==0)
		num+=1;

	int sp=num/2;

	for(int i=0; i<=num/2; i++)
	{
		for(int j=0; j<sp; j++)
		{
			cout<<" ";
		}

		for(int k=0; k<p; k++)
		{
			cout<<"*";
		}

		cout<<endl;
		p+=2;
		sp--;
	}
		sp=1;
		p=num-2;
	
	
	 for(i=0; i<num/2; i++)
	{
		for(int j=0; j<sp; j++)
		{
			cout<<" ";
		}

		for(int k=0; k<p; k++)
		{
			cout<<"*";
		}

		cout<<endl;
		p-=2;
		sp++;
	}


return 0;
}
mrlucky0
Newbie Poster
3 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You