number series program

Pages: 12
since a beginner please help me with the solutions to :


WAP to print
1)
1 1 1 1
1 1 1
1 1
1
2)
1
2 3
4 5 6
7 8 9 10
What do you have so far?

This may help for you: http://www.cplusplus.com/doc/tutorial/control/#for
Ahh.. Good old 'print a triangle' threads. Haven't seen any lately.

Here you go!
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
#include <iostream>

void pt1(int c){
	int i = 0;
	while(i < 1 << c - 1) i = (i << 1) | 1;
	int d = 1;
	do{
		int t = i;
		do if(t & 1) std::cout.put('1');
		while(t >>= 1);
		std::cout.put('\n');
		i ^= d;
		d <<= 1;
	}while(d <= i);
}

void print_number(int i){
	if(i / 10) print_number(i / 10);
	std::cout.put('0' + i % 10); 
}

void print_line(int first, int last){
	print_number(first);
	if(first == last) std::cout.put('\n');
	else{
		std::cout.put(' ');
		print_line(first+1, last);
	}
}

void print(int first, int len, int last){
	print_line(first, first+len-1);
	if(first + len - 1 - last < 0) print(first + len, len + 1, last);
}

void pt2(int c){
	print(1, 1, c);
}

int main(){
	pt1(4);
	pt2(10);

	std::cin.get();
	return 0;
}
@hamsterman ur reply is greatly appreciated but i'm a beginner and am unable to understand the higher concepts u used in ur program......wuld u mind keep it simple lyk using for loop 2 times i thnk in a shorter form of the program pls.... :-(
hamsterman is just having a bit of fun there. You can learn a lot from his over-complicated example though. (Just don't hand it in!)

What we're trying to say is that you must show us that you have at least made an attempt to solve this yourself first. We don't give out solutions to homework problems on this site.

Post the code you have so far and we will guide you...
yup i know that @cnoeval....... i finished the abv 2 questions myself trying a bit more....
but not able to figure out how to do this one ::::
1 1 1 1
1 1 1
1 1
1


pls help in this one.....pls.
Here:
1
2
3
4
#include <iostream>
int main(){
	cout << "1 1 1 1\n1 1 1\n1 1\n1\n";
}
not lyk this .... using for loop and why is nt this site portraying the correct question.
this is not the question is wrote ....
am sry am not able to write the exact question out here after writing in first line 1111 in the next i give a space then write 111 then next line i give 2 spaces and write 11 then next line 3 spaces 1.. i thnk u can figure out what the output shuld be ..... and @rocketboy9000 u can't just simply print the output.. pls do it using for loop..according the changed question as mentioned in this post..
closed account (z05DSL3A)
1 1 1 1
 1 1 1
  1 1
   1
Alright, quit it you three. You're having WAY too much fun. :)

Manmay, you kinda need to show us that you've at least tried the problem yourself. That means a program that:

-a: Has at least one for loop.
-b: Has at least one instance of std::cout.
-c: Returns 0.

..and compiles, but that's optional.

I'll also give you a hint: You need two for loops within each other.

-Albatross
Last edited on
That's how I would do it:

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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

template <int ICUR, int IMAX>
struct SubLoop1
{
    static inline void RUN()
    {
        std::cout << ' ';
        SubLoop1<ICUR+1,IMAX>::RUN();
    }
};

template <int IMAX>
struct SubLoop1<IMAX,IMAX>
{
    static inline void RUN() {}
};

template <int ICUR, int IMAX>
struct SubLoop2
{
    static inline void RUN()
    {
        std::cout << "1 ";
        SubLoop2<ICUR+1,IMAX>::RUN();
    }
};

template <int IMAX>
struct SubLoop2<IMAX,IMAX>
{
    static inline void RUN() {}
};

template <int ICUR, int IMAX>
struct MainLoop
{
    static inline void RUN()
    {
        SubLoop1<0,ICUR>::RUN();
        SubLoop2<0,IMAX-ICUR>::RUN();
        std::cout << std::endl;
        MainLoop<ICUR+1,IMAX>::RUN();
    }
};

template <int IMAX>
struct MainLoop<IMAX,IMAX>
{
    static inline void RUN() {}
};

int main()
{
    MainLoop<0,4>::RUN();

    std::cout << "\nhit enter to quit...";
    std::cin.get();
    return 0;
}

EDIT: Seriously, that's how I would do it. And then I'd challenge the other kids to see if their programs are faster than mine. And I would win, of course, since all loops in my program are unrolled during compilation.
Last edited on
@m4ster r0shi i don't want this long program. Actually i don't get what u hav done as it is being not taught to me yet.
can't we do it using two 'for' loops and @Grey Wolf the picturisation u gav isn't correct. u did lyk an equilateral triangle, i am asking lyk a right angled trianlge form.....with the first line is as u mentioned, second line 1 space 3 '1's, 3rd line 2 spaces 2 '1's, 4th line 3 spaces 1 '1'. in a right angled form... And pls i want u guys to do as we do in Turbo C
Last edited on
Don't you get our point, Manmay? We're not going to give you a solution that you can use. There's a good reason we don't give out usable solutions to homework problems which I wrote out in this article: http://cplusplus.com/forum/articles/31015/

Show us that you've at least tried to solve this problem and we'll take you seriously. Until then, I'm sorry, but we won't.

@m4ster r0shi: Meh. You could have optimized that further. I'll leave it to you to see how. :)

-Albatross

Unrolling the loops enough eventually leads to my program, except heavily obfuscated.
Um... you do know that your code doesn't compile, right rocketboy9000? You have a scope error there, as you forgot an std:: at line 3... :P

Sorry, I couldn't resist. :)

-Albatross
Last edited on
bah...
anyway, have you considered the problem as a cellular automaton?
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdio.h"
#include "stdlib.h"

int main(){
	unsigned long long x=6148914691236517205ULL;
	int y;
	while(x){
		for(y=0;y<64;y++)putchar(x<<63-y>>63?'1':' ');
		putchar('\n');
		x=x/2&x*2;
	}
	return 0;
}
Last edited on
Look, I'm sure there are billions of ways to solve this problem, but all the solutions you all have posted so far only mock. Please, there's no need for this. Let's wait and see if we get an attempt at solving the problem from the OP.

If we do, then we'll actually see what we can do to help him/her, and not do any further mocking. Now that the OP gave their attempt, we'll just follow the standard and courteous procedure. I'm sure you know which one I'm referring to: the one that almost always works well.

If we don't, the we should just let this thread die rather than flamebait with four useless and either overblown or oversimplified solutions. I, along with cnoeval and Null tried to get the OP to try, even while some of you just stood around and touted your 1337 c0d1n9 5k1llz.

Either way, I say enough of this nonsense. Those actions don't help anyone.

-Albatross
Last edited on
Albatross, I think you have a bit too much hope in people. The op has not made any effort in 4 days and several times basically said "do my homework". He even knows how it should be done, he just never tried. You can't help him, if he doesn't want to be helped. Why not mock him?
Last edited on
Hamsterman, just to say, this is not just about this thread; there's dozens of threads sharing the same fate while the OP may be willing to learn. Sure, it's not like that this time, but try thinking in the bigger image. ;)
what does this line from the first solution even do? Bit shifting as part of a conditional statement? O_o

 
while(i < 1 << c - 1) i = (i << 1) | 1;

you think you're getting somewhere near decent at programming then you come on here and realise how shit you are...
Pages: 12