943,614 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3419
  • C++ RSS
Jul 5th, 2004
0

Print a Diamond

Expand Post »
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 <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

int n;

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

cout<<n/2<<endl<<endl;

for(int i=0; i<=n/2; i++){
cout<<" ";

for(int st=1; st<=i; st++)
{
cout<<"*";
}
cout<<endl;
}

return 0;
}

This is an assignment on nested loops, so we're supposed to use 4 for loops. I suspect that there's going to be 2 pairs of nested for loops. Can anyone help me out?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mrlucky0 is offline Offline
3 posts
since Jul 2004
Jul 5th, 2004
0

Re: Newbie need help on HW

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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5.  
  6. int main()
  7. {
  8.  
  9. int num, odd, increment, space;
  10. bool go;
  11.  
  12. num = 0;
  13. increment = 1;
  14. odd = 1;
  15. go = false;
  16.  
  17. do{
  18. cout<<"Enter number: ";
  19. cin>>num;
  20. }while(num<3);
  21.  
  22. if(num%2==0)
  23. num = num + 1;
  24.  
  25. while(odd<=num)
  26. {
  27. space = num/2 - odd + increment;
  28. if(go==false)
  29. increment += 1;
  30. else
  31. increment -= 1;
  32.  
  33. for(int x=0; x<space; x++)
  34. cout<<" ";
  35.  
  36. for(x=0; x<odd; x++)
  37. cout<<"*";
  38.  
  39. for(x=0; x<space; x++)
  40. cout<<" ";
  41. cout<<endl;
  42.  
  43. if(go==false)
  44. odd += 2;
  45. else
  46. odd -= 2;
  47. if(odd>num && go==false)
  48. {
  49. odd = num - 2;
  50. increment = num/2;
  51. go = true;
  52. }
  53. if(odd<0)
  54. odd = num + 1;
  55. }
  56.  
  57. return 0;
  58. }
Hope this helps.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 6th, 2004
0

Re: Newbie need help on HW

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... hope meet you soon
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Eddie is offline Offline
3 posts
since Jul 2004
Jul 6th, 2004
0

Re: Newbie need help on HW

Quote originally posted by Eddie ...
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... 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mrlucky0 is offline Offline
3 posts
since Jul 2004
Jul 6th, 2004
0

Re: Newbie need help on HW

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.
Reputation Points: 53
Solved Threads: 1
Posting Whiz
red_evolve is offline Offline
313 posts
since Jun 2003
Jul 7th, 2004
0

Re: Newbie need help on HW

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

#include<iostream.h>
#include<conio.h>
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<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
for(i=n-2;i>=1;i-=2)
{
for(j=i;j<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
getch();
}
Reputation Points: 12
Solved Threads: 0
Newbie Poster
3maddy3 is offline Offline
7 posts
since Jul 2004
Jul 11th, 2004
0

Re: Newbie need help on HW

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!

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5.  
  6. int main()
  7. {
  8.  
  9. int num, p=1;
  10.  
  11.  
  12. cout<<"Enter number: ";
  13. cin>>num;
  14.  
  15. if(num%2==0)
  16. num+=1;
  17.  
  18. int sp=num/2;
  19.  
  20. for(int i=0; i<=num/2; i++)
  21. {
  22. for(int j=0; j<sp; j++)
  23. {
  24. cout<<" ";
  25. }
  26.  
  27. for(int k=0; k<p; k++)
  28. {
  29. cout<<"*";
  30. }
  31.  
  32. cout<<endl;
  33. p+=2;
  34. sp--;
  35. }
  36. sp=1;
  37. p=num-2;
  38.  
  39.  
  40. for(i=0; i<num/2; i++)
  41. {
  42. for(int j=0; j<sp; j++)
  43. {
  44. cout<<" ";
  45. }
  46.  
  47. for(int k=0; k<p; k++)
  48. {
  49. cout<<"*";
  50. }
  51.  
  52. cout<<endl;
  53. p-=2;
  54. sp++;
  55. }
  56.  
  57.  
  58. return 0;
  59. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mrlucky0 is offline Offline
3 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: storing references to classes
Next Thread in C++ Forum Timeline: acceleration and brakes in a car game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC