Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → C# Help
C# Help
2009-04-23, 3:47 PM #1
I have to generate the following output using only nested for-loops.

Quote:
6
5 6
4 5 6
3 4 5 6
2 3 4 5 6
1 2 3 4 5 6



This shouldn't be this hard. Anybody wanna help me out? :smith:

2009-04-23, 3:54 PM #2
I don't know C# so I will write in a Java-ish pseudocode

Code:
String output = "";

for (int i=6; i>0; i--) {
  output = output.splice(((Integer)i).toString(), 0); //Add the next lowest number to the beginning of the string

  --OR--
 output = ((Integer)(i)).toString() + output; //append the existing string to the end of the current number

  print output;
}
(Again, this would not compile in Java or another language, it's just to show logic)
一个大西瓜
2009-04-23, 3:55 PM #3
Code:
for (int i = 6; i >= 1; i--)
{
    for (int k = i; k <= 6; k++)
    {
        Console.Write(k + " ");
    }
    Console.WriteLine();
}
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2009-04-23, 4:01 PM #4
Oh, you're not allowed to use variables. Doh.
一个大西瓜
2009-04-23, 4:05 PM #5
What? No, you can, but regardless your code does not work at all.
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2009-04-23, 4:20 PM #6
Err yes it does. Besides the fact that it's not written in C# or any real programming language, why wouldn't it?

Init:
output is ""

For loop:
i = 6
output = ""
output becomes 6
print 6

i = 5
output = 6
output becomes 56
print 56

i = 4
output = 56
output becomes 456
print 456

etc
一个大西瓜
2009-04-23, 4:25 PM #7
Oh sorry, I thought your print was outside the loop.
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2009-04-23, 4:27 PM #8
Thanks a bunch Emon.

I just need help on one more:

Quote:
0 0 0 0 0 1
0 0 0 0 2 1
0 0 0 0 2 1
0 0 0 3 2 1
0 0 4 3 2 1
0 5 4 3 2 1
6 5 4 3 2 1

2009-04-23, 5:01 PM #9
Code:
for (i = 1; i <= 6; i++) {
   for (k = i; i < 6; i++) 
      print(0+" ");
  print(i);
}
2009-04-23, 5:09 PM #10
JL, your code just prints "0 0 0 0 0 6"

Because I'm bored:

Code:
for (int i = 1; i <= 6; i++)
{
    for (int j = 6; j > i; j--)
    {
        Console.Write("0 ");
    }
    for (int k = 1; k <= i; k++)
    {
        Console.Write(k + " ");
    }
    Console.WriteLine();
}
Bassoon, n. A brazen instrument into which a fool blows out his brains.
2009-04-23, 5:21 PM #11
it is the same as Db, if that helps at all
2009-04-23, 5:24 PM #12
Did everyone just do your homework?
twitter | flickr | last.fm | facebook |
2009-04-23, 5:56 PM #13
Code:
def foo(count=1,limit=6):
	if count <= limit:
		foo(count=count+1,limit=limit)
		print " ".join([str(x) for x in range(limit,count-1,-1)])
foo()

def bar(count=1,limit=6):
	if count <= limit:
		bar(count=count+1,limit=limit)
		print " ".join(['0' for x in range(1,count)] + [str(x) for x in range((limit+1)-count,0,-1)])
bar()


I forgot to use C# :(
Detty. Professional Expert.
Flickr Twitter
2009-04-23, 6:25 PM #14
Originally posted by TimeWolfOfThePast:
Did everyone just do your homework?

Pretty much.
Code to the left of him, code to the right of him, code in front of him compil'd and thundered. Programm'd at with shot and $SHELL. Boldly he typed and well. Into the jaws of C. Into the mouth of PERL. Debug'd the 0x258.
2009-04-23, 6:49 PM #15
No actually there's a whole bunch of em and I've just been struggling with these two. But thanks a lot guys. Much appreciated :)

2009-04-23, 7:20 PM #16
Code:
                                  #include\
                                  <stdio.h>
                     #include                <stdlib.h>
                     #include                <string.h>

                    #define w "Hk~HdA=Jk|Jk~LSyL[{M[wMcxNksNss:"
                   #define r"Ht@H|@=HdJHtJHdYHtY:HtFHtF=JDBIl"\
                  "DJTEJDFIlMIlM:HdMHdM=I|KIlMJTOJDOIlWITY:8Y"
                 #define S"IT@I\\@=HdHHtGH|KILJJDIJDH:H|KID"\
                "K=HdQHtPH|TIDRJDRJDQ:JC?JK?=JDRJLRI|UItU:8T"
               #define _(i,j)L[i=2*T[j,O[i=O[j-R[j,T[i=2*\
              R[j-5*T[j+4*O[j-L[j,R[i=3*T[j-R[j-3*O[j+L[j,
             #define t"IS?I\\@=HdGHtGIDJILIJDIItHJTFJDF:8J"

    #define y                  yy(4),yy(5),                yy(6),yy(7)
  #define yy(              i)R[i]=T,T            =O,O=L 
#define Y _(0          ], 4] )_ (1 ], 5] )_ (2      ], 6] )_ (3 ], 7] )_=1
#define v(i)(      (( R[ i ] * _ + T [ i ]) * _ + O [ i ]) * _ + L [ i ]) *2
double b = 32  ,l ,k ,o ,B ,_ ; int Q , s , V , R [8 ], T[ 8] ,O [8 ], L[ 8] ;
#define q( Q,R ) R= *X ++ % 64 *8 ,R |= *X /8 &7 ,Q=*X++%8,Q=Q*64+*X++%64-256,
# define  p      "G\\QG\\P=GLPGTPGdMGdNGtOGlOG"   "dSGdRGDPGLPG\\LG\\LHtGHtH:"
#  define W         "Hs?H{?=HdGH|FI\\II\\GJlHJ"    "lFL\\DLTCMlAM\\@Ns}Nk|:8G"
# define   U           "EDGEDH=EtCElDH{~H|AJk}"       "Jk?LSzL[|M[wMcxNksNst:"
#  define u                  "Hs?H|@=HdFHtEI"             "\\HI\\FJLHJTD:8H"
char  *   x                   ,*X , ( * i )[               640],z[3]="4_",
*Z = "4,8O4.8O4G" r U "4M"u S"4R"u t"4S8CHdDH|E=HtAIDAIt@IlAJTCJDCIlKI\\K:8K"U
 "4TDdWDdW=D\\UD\\VF\\FFdHGtCGtEIDBIDDIlBIdDJT@JLC:8D"t"4UGDNG\\L=GDJGLKHL\
FHLGHtEHtE:"p"4ZFDTFLT=G|EGlHITBH|DIlDIdE:HtMH|M=JDBJLDKLAKDALDFKtFKdMK\
\\LJTOJ\\NJTMJTM:8M4aGtFGlG=G|HG|H:G\\IG\\J=G|IG|I:GdKGlL=G|JG|J:4b"W
S"4d"W t t"4g"r w"4iGlIGlK=G|JG|J:4kHl@Ht@=HdDHtCHdPH|P:HdDHdD=It\
BIlDJTEJDFIdNI\\N:8N"w"4lID@IL@=HlIH|FHlPH|NHt^H|^:H|MH|N=J\\D\
J\\GK\\OKTOKDXJtXItZI|YIlWI|V:8^4mHLGH\\G=HLVH\\V:4n" u t t
"4p"W"IT@I\\@=HdHHtGIDKILIJLGJLG:JK?JK?=JDGJLGI|MJDL:8M4\
rHt@H|@=HtDH|BJdLJTH:ITEI\\E=ILPILNNtCNlB:8N4t"W t"4u"
p"4zI[?Il@=HlHH|HIDLILIJDII|HKDAJ|A:JtCJtC=JdLJtJL\
THLdFNk|Nc|\
:8K"; main (
int C,char**        A) {for(x=A[1],i=calloc(strlen(x)+2,163840);
C-1;C<3?Q=_=       0,(z[1]=*x++)?((*x++==104?z[1]^=32:--x), X =
strstr(Z,z))      &&(X+=C++):(printf("P2 %d 320 4 ",V=b/2+32),
V*=2,s=Q=0,C     =4):C<4?Q-->0?i[(int)((l+=o)+b)][(int)(k+=B)
]=1:_?_-=.5/    256,o=(v(2)-(l=v(0)))/(Q=16),B=(v(3)-(k=v(1)
))/Q:*X>60?y   ,q(L[4],L[5])q(L[6],L[7])*X-61||(++X,y,y,y),
Y:*X>57?++X,  y,Y:*X >54?++X,b+=*X++%64*4:--C:printf("%d "
,i[Q][s]+i[Q ][s+1]+i[Q+1][s]+i[Q+1][s+1])&&(Q+=2)<V||(Q=
0,s+=2)<640
||(C=1));}


This program takes a string and writes a PGM image of the text in elvish.

Program help: http://www0.us.ioccc.org/2004/anonymous.hint

Moar: http://www0.us.ioccc.org/years.html

(Don't worry I didn't make this.)

(I could have found a more suitable program from that list but I'm not THAT bored.)

2009-04-23, 7:39 PM #17
"They're everywhere, the little harlots."
-Martyn
2009-04-24, 12:47 AM #18
Originally posted by Emon:
JL, your code just prints "0 0 0 0 0 6"


D'oh! Quick algorithm development fail.
2009-04-24, 12:50 AM #19
Originally posted by BombayZeus:
using only nested for-loops.


HAI GUYZ. LETZ TEECH OBJECT OREE ENTED PROGRAMMXORZ WITH PROCEDURAL CONSTRUCTS LOLZ!!!!


Seriously, "only using nested for"s isn't C# programming. What an absolutely ridiculous restriction. If I ever saw any of my colleagues writing nested for-loops I would give them a slap upside the head and send them back to OO school.

(Just to be clear, I understand that's the assignment and you don't have a choice. That rant is directed at your teacher. One of the problems with the software industry today is that the vast majority of software developers writing OO code have no idea of the fundamental principles of OO, and end up writing procedural code with a "public class {}" wrapped around it.)
2009-04-24, 12:56 AM #20
Originally posted by Giraffe:
If I ever saw any of my colleagues writing nested for-loops I would give them a slap upside the head and send them back to OO school.
...Iterating over a two-dimensional array?
2009-04-24, 1:02 AM #21
Unless you are seriously suggesting that the teacher should be telling his students, who apparently find an assignment such as this one challenging, to construct a custom iterator object for a two-dimensional array instead of mastering a simple concept such as a nested for loop.

Also, if you really think a loop is a "procedural construct" I think the designers of Smalltalk would like to have a word with you.
2009-04-24, 1:12 AM #22
Originally posted by Giraffe:
What an absolutely ridiculous restriction.

Not if the teacher is trying to tech a specific topic?

Originally posted by Giraffe:
If I ever saw any of my colleagues writing nested for-loops I would give them a slap upside the head and send them back to OO school.

Whats so wrong with nested loops? OO or not.
dr0id
2009-04-24, 1:38 AM #23
Guess what's running under the hood of all that OO stuff you're advocating?

It's pretty silly to condemn nested for loops when you use abstractions powered by nested for loops, don't you think?
"it is time to get a credit card to complete my financial independance" — Tibby, Aug. 2009
2009-04-24, 1:39 AM #24
The method's code executes in order until it meets a return statement (or runs out of code)... that's so procedural.
2009-04-24, 1:59 AM #25
here is my solution, I hope it's OOP enough: http://codepad.org/WNYMrt7w
2009-04-24, 5:06 AM #26
Hey. That code can't iterate over floats. What sort of lousy generic programming is that?
2009-04-24, 7:45 AM #27
He's obviously not taking full advantage of C# and .NET 3.5. He should be required to find his solution use a complex entity data model as well as a WCF service.

:colbert:
2009-04-24, 7:03 PM #28
Originally posted by Jon`C:
here is my solution, I hope it's OOP enough: http://codepad.org/WNYMrt7w

:awesomelon:

Here is my recursive OCaml version, since loops are apparently evil now:
Code:
mini[~/ocaml 166] cat table.ml 
let rec cols n m =
	if n > 0 then (
		print_int 0;
		cols (n - 1) m
	) else if m > 0 then (
		print_int m;
		cols n (m - 1)
	) else
		print_endline "";;

let rec rows n m =
	if m > 0 then cols n m;
	if n > 0 then rows (n - 1) (m + 1);;

let build_table n =
	rows n 0;;

let main =
	build_table 6;;

main;;
mini[~/ocaml 167] ocamlc table.ml 
mini[~/ocaml 168] ./a.out 
000001
000021
000321
004321
054321
654321
[This message has been edited. Deal with it.]

↑ Up to the top!