By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 927 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

i need urgent help pleaaaase

P: 40
First off, I need to mention this: I am in a basic programing (c++) class. So please refraim from using methoods above basic functions. I am a student who has no idea on how to solve this problem:

1. Write a program that asks the user to enter an odd integer between
1 and 10 (both inclusive). If the integer value was 3, then print out the following pattern on screen.
*
***
*
If the value was 5, then the pattern printed is as follows:
*
***
*****
***
*

*Note: the stars are in a diamond shape... website wont let me do spacing...

And so on for other odd values less than 10.
Use nested control statements in your program to get the desired output.

If it wasn't for the last sentence, i'd do a bunch of if then statements, but that methood isn't allowed... any idea? i been at this for the past 12 hours now...

--------------------------------------------------------------------------------
Sep 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: 62
First off, I need to mention this: I am in a basic programing (c++) class. So please refraim from using methoods above basic functions. I am a student who has no idea on how to solve this problem:

1. Write a program that asks the user to enter an odd integer between
1 and 10 (both inclusive). If the integer value was 3, then print out the following pattern on screen.
*
***
*
If the value was 5, then the pattern printed is as follows:
*
***
*****
***
*

*Note: the stars are in a diamond shape... website wont let me do spacing...

And so on for other odd values less than 10.
Use nested control statements in your program to get the desired output.

If it wasn't for the last sentence, i'd do a bunch of if then statements, but that methood isn't allowed... any idea? i been at this for the past 12 hours now...

--------------------------------------------------------------------------------
To solve this you dont need methods just use simple for loop,

generate the required logic from the for loops, the steps u should consider are

the total no of rows is equal to number u entered and middle row is having

number of stars same as the number u entered and in the above & below of that

rows the number of stars decreases by odd value, use this logic in for loop
Sep 21 '06 #2

P: 40
To solve this you dont need methods just use simple for loop,

generate the required logic from the for loops, the steps u should consider are

the total no of rows is equal to number u entered and middle row is having

number of stars same as the number u entered and in the above & below of that

rows the number of stars decreases by odd value, use this logic in for loop
thanks for the quick answer, I have come up with this code but Iknow that I am missing someting. also the prof gave us this hint ( if using double loop , you may need the modulus operator % )

so can you please tell me what is wrong in this code.
thanks again
Sep 21 '06 #3

10K+
P: 13,264
thanks for the quick answer, I have come up with this code but Iknow that I am missing someting. also the prof gave us this hint ( if using double loop , you may need the modulus operator % )

so can you please tell me what is wrong in this code.
thanks again
Sorry, but where did you post the code?
Sep 21 '06 #4

P: 40
I AM SORRY , HERE IS THE CODE.

Expand|Select|Wrap|Line Numbers
  1.  #include <iostream> 
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6.  
  7. {
  8.  
  9. int height;
  10.  
  11. cout << "How tall is your diamond?" << endl;
  12.  
  13. cin >> height;
  14.  
  15. for (int i = 0; i <= height; i++) {
  16.  
  17. for (int j = 0; j <= i; j++)
  18.  
  19. {
  20.  
  21. cout << '*';
  22.  
  23. }
  24.  
  25. for ( int x=0; x >=i; x--)
  26.  
  27. {
  28.  
  29. cout << '*';
  30.  
  31. }
  32.  
  33. cout << endl;
  34.  
  35. }
  36.  
  37. return 0;
  38.  
  39. }
  40.  
Sep 21 '06 #5

P: 13
sin
I hope that will help

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3.  
  4. using    namespace std;
  5.  
  6. int
  7. main()
  8. {
  9.     int n;
  10.     int i, j, x;
  11.  
  12.     cout << ": ";
  13.     cin >> n;
  14.  
  15.     // you said this must be an odd number between 1 and 10, right ?
  16.     if ((n % 2) == 0 || n < 1 || n > 10) {
  17.         cerr << "bad number" << endl;
  18.         exit(1);
  19.     }
  20.  
  21.     // only one '*', no diamond
  22.     if (n == 1) {
  23.         cout << "*" <<  endl;
  24.         return (0);
  25.     }
  26.  
  27.     // increasing number of writen '*'
  28.     for (i = 1; i <= n; i += 2) {
  29.         x = (n - i) / 2; // number of spaces before '*' and after
  30.         for (j = 0; j < x; j++)
  31.             cout << " ";
  32.         for (j = 0; j < i; j++)
  33.             cout << "*";
  34.         for (j = 0; j < x; j++)
  35.             cout << " ";
  36.         cout << endl;
  37.     }
  38.  
  39.     // decreasing
  40.     for (i = n - 2; i > 0; i -= 2) {
  41.         x = (n - i) / 2;
  42.         for (j = 0; j < x; j++)
  43.             cout << " ";
  44.         for (j = 0; j < i; j++)
  45.             cout << "*";
  46.         for (j = 0; j < x; j++)
  47.             cout << " ";
  48.         cout << endl;
  49.     }
  50.  
  51.     return (0);
  52. }
  53.  
  54.  
sorry for any small typing error, it happends..
Sep 21 '06 #6

P: 40
sin;
you are the best.

thanks a million.
ayman723
Sep 23 '06 #7

Post your reply

Sign in to post your reply or Sign up for a free account.