Sand Watch from Numbers

Pages: 12
Konishua,
I want to develop a program with function With argument N - positive integer that displays numbers between 1 and N in a sand watch shape. The figure is presented in N lines. The 1st line contains N number of 1s. The second Line contains N-2 number of 2s and etc. The last line contains N number of Ns.
Example:

Input: N=3 Output:
111
2
333

---------------------
Input: N=6
Output:
111111
2222
33
44
5555
666666

***I couldn't figure out the algorithm for this. Can you help me please.
Thanks in Advance :)
NOTE: I am sorry I couldn't figure out the sand shape in this question, but should be like a sand shape.
Last edited on
This is basically the same thing as the number triangle problem, just that you will use 2 loops here instead of one.
Guys I cannot figure out the algorithm for this program, Can you help me on that. Any help is greatly appreciated. Thanks in Advance.
You did do the triangle thing earlier, right? Do you remember how it works?
Here is my code: It doesn't show the sand watch Like I wanted, I cannot figure out algorithm for this, Can you help me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
    int h;
    cout<<"Enter height: ";
    cin>>h;
    for(int row=h;row>=1;row--){
      for(int i=1;i<=row;i++)
      cout<<i;
      cout<<endl;
      }
      system("pause");
      return 0;
      }
      
Ok, I'll give you a task: write code that works with a variable n, and prints a triangle like this:

ex:

n = 6
111111
2222
33
Sorry guys but I don't have any idea about how to solve this. I spend my whole day for this but nothing comes out. :(((
Sorry hanst99 I know it seems very simple but I can't do that. I did all what I know but I couldn't find that proper algorithm for the program. :(
Last edited on
I'll give you a hint... how is this:

111
22
3


different from this:

111111
2222
33


?

See, I am not tormenting you like that for no reason. It's better you find the solution yourself. I mean, if it's not the best solution it doesn't matter, we can still help you polishing it.
One of the more difficult to spot issues with the code you have so far is a 'scope issue' on line 9. Ask yourself "If the inner for loop is executed everytime the outer for loop iterates, then why am I not getting an error everytime 'int i' is redeclared? Trust me, this is relavent to your output issue.
Sorry guys but I can't do that :(. Nothing comes to my mind. :((((
Then based on your earlier statement made here:
I spend my whole day for this but nothing comes out
I'd say your current problem is fatigue. Walk away from the problem and do something else, then come back to it.
So Far here is my code. I couldn't think of how to do the rest.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"n= ";
    cin>>n;
    for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++)
    cout<<i;
    cout<<'n';
    n-=1;
}
system("pause");
 return 0;
}


**When Enter number 3 it shows:
1
2
111
22

And nothing else I could figure out the rest of the code in order to make the program work properly.
Last edited on
mine prog for even number.
I'm just start learning c, excuse me )

#include <iostream>
using namespace std;

int main () {
int n,i,j;
cout << "Enter N, please\n";
cin >> n;
if (n%2 == 0) {
//drawing upper part of sand watch
for (i=0;i<n/2;i++) {
for (j=0;j<n-i;j++) {
cout << n-i;
}
cout << "\n";
}
//drawing downer part of sand watch
for (i=n/2;i<n;i++) {
for (j=0;j<i+1;j++) {
cout << n-i;
}
cout << "\n";
}
}
return 0;
}

Odd number's case kinda obvious from even one.
janibeg but it doens't show the sand watch shape I wanted exactly upside down two simetrical triangles with numbers
1
2
3
4
5
6
11111
 222
  33
  44
 555
66666

I couldn't draw it precisely but it should be something like that.
Uzumaki - why didn't you say so earlier? You can just
#include<iomanip> , change the alignment to middle, push the numbers for each line on a stringstream, cout the string from the stringstream while setting the width to n.

http://www.cplusplus.com/reference/iostream/manipulators/
http://www.cplusplus.com/reference/iostream/stringstream/

Well, you don't have to do it like that, but then adjusting the alignment would be a bit harder.
hanst99 the problem is not spacing itself I cannot figure out algorithm for it to how write numbers
1
2
3
4
5
6
7
 N=6
111111
 2222
  33
  44
 5555
666666

basically I cannot figure out algorithm for this program.
Alright, I guess I could help you out a bit.

Basically, you are outputting two triangles here, one from
1 - N/2

1 * N
2 * N-1
N/2 * N/2

However, this is not yet what you want (at least I deduct so from the rest what you write), because for N = 6 this would be

111111
22222
3333

While what you want is

111111
2222
33


The solution is trivial:

from i:= 0 to N/2
output(i+1)  N- (2*i) times


for the other way round, it looks a bit more complicated, but you should also understand it:

for i to N
output(i+1) N- 2*(N-(i+1)) times

i starts here at N/2. To make sense out of this, you will have to consider that these are integer divisions. Example, 5/2 is 2, not 2.5.

I will walk you through one sandglass to make it more comprehensible:

N = 4
i = 0;
loop1:
N/2 == 2, so loop 2 times
:iteration 1
i == 0,
4-2*0 == 4
print 0+1 4 times
i = i+1
:iteration 2
i == 1
4-2*1 == 2
print 1+1 2 times
i = i+1
#i is now 2, so exit loop

loop 2:
4-2 is 2, so loop 2 times
:iteration 1
i == 2
4- 2*(4-(2+1)) == 2
print(2+1) 2 times
i = i+1
:iteration 2
i == 3
4-2*(4-(3+1)) == 4
print(3+1) 4 times
i = i+1
#i is now 4, so exit loop


btw: the formula is so complicated for the second loop cause otherwise the printout for uneven N's would be wrong.
Last edited on
it works for any N
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

int main () {
    int n,i,j;
    cout << "Enter N, please\n";
    cin >> n;
    
        //drawing upper part of sand watch
       for (i=0;i<n/2;i++) {
            for (j=0;j<i;j++){
                cout <<"*";
            }
            for (j=i;j<n-i;j++) {
                cout << n-i;
            }
            for (j=0;j<i;j++){
                cout <<"*";
            }
            cout << "\n";
        } 
        if (n%2 == 1) {
            for (i=0;i<(n-1)/2;i++) { 
                cout <<"*";
            }
            cout << (n-1)/2+1;
            for (i=0;i<(n-1)/2;i++) { 
                cout <<"*";
            }
            cout << "\n";
         }
        //drawing downer part of sand watch     
        for (i=n/2+1;i<n;i++) {
            for (j=0;j<n-i-1;j++){
                cout <<"*";
             }
             for (j=0;j<2*i-n+2;j++) {
                cout << n-i;
             }
            for (j=0;j<n-i-1;j++){
                cout <<"*";
            }
 
            cout << "\n";
        } 
    
    return 0;
}
Last edited on
@janibeg My solution requires 4 for loops, and 15 lines of code - including input and individual line variable declaration because I think that's more readible. I'd say it's better :O

ohps: forgot the sandwatch form. That adds another 7 line function i need, as well as 2 additional lines in my main code. Though I gotta admit I use 2 additional headers.
Last edited on
Pages: 12