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:
***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.
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. :(
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.
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;
}
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.
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.
@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.