Newbie need help on HW

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2004
Posts: 3
Reputation: mrlucky0 is an unknown quantity at this point 
Solved Threads: 0
mrlucky0 mrlucky0 is offline Offline
Newbie Poster

Print a Diamond

 
0
  #1
Jul 5th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: Newbie need help on HW

 
0
  #2
Jul 5th, 2004
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 3
Reputation: Eddie is an unknown quantity at this point 
Solved Threads: 0
Eddie's Avatar
Eddie Eddie is offline Offline
Newbie Poster

Re: Newbie need help on HW

 
0
  #3
Jul 6th, 2004
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
:twisted: Eddiesay hello :D
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 3
Reputation: mrlucky0 is an unknown quantity at this point 
Solved Threads: 0
mrlucky0 mrlucky0 is offline Offline
Newbie Poster

Re: Newbie need help on HW

 
0
  #4
Jul 6th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: Newbie need help on HW

 
0
  #5
Jul 6th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 7
Reputation: 3maddy3 is an unknown quantity at this point 
Solved Threads: 0
3maddy3 3maddy3 is offline Offline
Newbie Poster

Re: Newbie need help on HW

 
0
  #6
Jul 7th, 2004
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();
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 3
Reputation: mrlucky0 is an unknown quantity at this point 
Solved Threads: 0
mrlucky0 mrlucky0 is offline Offline
Newbie Poster

Re: Newbie need help on HW

 
0
  #7
Jul 11th, 2004
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!

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3125 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC