Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count to 10
#1
I've been reading some depressing stories about programming when a bit caught my eye:

Quote:If you can successfully write a loop that goes from 1 to 10 in every language on your resume, can do simple arithmetic without a calculator, and can use recursion to solve a real problem, you're already ahead of the pack!

Makes me wonder, how many ways can Southperry count to 10? Anything that a computer can run to print an output counts. JavaScript to C++. Python to Assembly.

Guidelines:
  • Using a language multiple times is fine, as long as the methodologies are different.
  • The quote says "loop". Don't hardcode it in. eg: Don't type out 1 through 10 and have the program print it out.
  • Simpler, more elegant solutions are better. Strive for those.
Reply
#2
Define "every language" does that include the obsolete ones?
Reply
#3
Unless it's something that no one has ever heard of, yes.

Nevermind. If it's a 'language' in even the loosest sense it counts.
Reply
#4
Got bored after the first three, since most others are just variations of these.
VB
 Spoiler

TSQL
 Spoiler

C
 Spoiler
It's not having what you want - It's wanting what you've got.
Reply
#5
I would give you a brohug if i were in front of you.

This reminded me that i had to look up ways to find how to know when a number is even or odd while making algorithms (And later coding it), and now i remembered how to calculate multiples of any number to boot too Biggrin

Im still fairly new to programing and have to restart my education of it because of a few problems that i won't bother mention, but someone would be nice to explain this to me? "printf( "%d\n", x)" only one that didn't understand exactly what it says (I can guess, sort of, but i would rather know exactly what it is).
Reply
#6
dante9898 Wrote:I would give you a brohug if i were in front of you.

This reminded me that i had to look up ways to find how to know when a number is even or odd while making algorithms (And later coding it), and now i remembered how to calculate multiples of any number to boot too Biggrin

Im still fairly new to programing and have to restart my education of it because of a few problems that i won't bother mention, but someone would be nice to explain this to me? "printf( "%d\n", x)" only one that didn't understand exactly what it says (I can guess, sort of, but i would rather know exactly what it is).

printf can be read as "print formatted", that is, printing something to the output in a formatted manner. You can dictate that the thing you print be an interger (d), a floating point value (f), a string (s) or possibly more. Basically, it's a function, it receives order to do something and HOW to do it, then do it.

In your example: printf("%d\n", x) says to print x (whatever it may be) as an integer (%d part) - so it probably will cut off the decimal part if x is a floating point value.
The \n part, if I'm not mistaken, universally means "insert a newline" where \ is the escape sequence so that n could mean new (and not the literal character n).

As for the actual topic, I only know Java as of now (so probably I can do it in C and C++ after a 10-minute session digging up?), and here's the most obvious ways:
Code:
for(int i=1; i <=10; i++) {
   System.out.println(i);
}
Code:
int i = 1;
do {
   System.out.println(i++);
} while (i <= 10);
Code:
public static void addOne(int i) {
   if(i < 10) {
      i++;
      System.out.println(i);
      addOne(i);
   }
}
Reply
#7
dante9898 Wrote:This reminded me that i had to look up ways to find how to know when a number is even or odd while making algorithms (And later coding it), and now i remembered how to calculate multiples of any number to boot too Biggrin

Modulo for both?

dante9898 Wrote:Im still fairly new to programing and have to restart my education of it because of a few problems that i won't bother mention, but someone would be nice to explain this to me? "printf( "%d\n", x)" only one that didn't understand exactly what it says (I can guess, sort of, but i would rather know exactly what it is).

C is a low level language, it does not have any natural ways to print values without explicitly casting them into a character string via a format conversion function like printf. The other languages you can just say print @variable and the engine knows to bring the value of the variable, but with C you have to be more specific, because print requires a character string and x is a number, therefore you need to convert x into a character string to print it, either via that function, or a more torturous route such as creating a character array and printing to that array the value of the variable. Could've possibly done a print( itoa(x)); as well.
It's not having what you want - It's wanting what you've got.
Reply
#8
R
lol Wrote:seq(1:10)

Matlab
Quote:for(i=1:10)
i

C++
Quote:for(i=1;i<=10;i++)
{
cout<<i;
}

C++ Recursive
Quote:void main()
{
printseq(1,10);
}
void printseq(int number, int end)
{
cout<< number;
if(number <10)
{
printseq(number+1,end);
}

}

The java one looks almost exactly like C++ cept it uses System.out.println(i); instead of cout<<i;
Reply
#9
Eosian Wrote:Modulo for both?

Pretty much yes. I remember that i was taught that but forgot about it, while i got caught up recently on a problem that needed to find if a number was Odd and Even and could not remember the method to do so.

Eosian Wrote:C is a low level language, it does not have any natural ways to print values without explicitly casting them into a character string via a format conversion function like printf. The other languages you can just say print @variable and the engine knows to bring the value of the variable, but with C you have to be more specific, because print requires a character string and x is a number, therefore you need to convert x into a character string to print it, either via that function, or a more torturous route such as creating a character array and printing to that array the value of the variable. Could've possibly done a print( itoa(x)); as well.

This is still a little fuzzy but i can understand my way from what you and kalovale said, need to see it in action to completely understand it. Thx for the answers btw.
Reply
#10
 Python
Reply
#11
Aww, I was hoping for some degree of creativity from you guys. Sad

Code:
HAI
CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
    UPZ VAR!!1
    VISIBLE VAR
    IZ VAR BIGR THAN 10? GTFO. KTHX
KTHX
KTHXBYE
Reply
#12
Hazzy Wrote:Aww, I was hoping for some degree of creativity from you guys. Sad

Code:
HAI
CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
    UPZ VAR!!1
    VISIBLE VAR
    IZ VAR BIGR THAN 10? GTFO. KTHX
KTHX
KTHXBYE

I actually WAS considering posting this one xD
Reply
#13
Hazzy Wrote:Aww, I was hoping for some degree of creativity from you guys. Sad

You stated elegence was the target, short and succinct.
What did you expect?
It's not having what you want - It's wanting what you've got.
Reply
#14
Hazzy Wrote:Aww, I was hoping for some degree of creativity from you guys. Sad

Code:
HAI
CAN HAS STDIO?
I HAS A VAR
IM IN YR LOOP
    UPZ VAR!!1
    VISIBLE VAR
    IZ VAR BIGR THAN 10? GTFO. KTHX
KTHX
KTHXBYE

Code:
while(!derpington) {
   derp++;
}
Reply
#15
Jeez, I gotta re-learn all this stuff. Haven't in years.
I would post binary code but that's too simple compared to what you guys wrote =3=
Reply
#16
GMSInfighter Wrote:Jeez, I gotta re-learn all this stuff. Haven't in years.
I would post binary code but that's too simple compared to what you guys wrote =3=

Indeed. Machine code is simpler than .NET
Reply
#17
Just for fun

 Joy
Reply
#18
Fiel Wrote:Indeed. Machine code is simpler than .NET

:f6:

How so?

vb.net is almost identical to the VB i posted.
Even C# and C++.net work with the C examples I gave.
It's not having what you want - It's wanting what you've got.
Reply
#19
Sorry, Eosian. Let me clarify.

<sarcasm>
Indeed. Machine code is simpler than .NET
</sarcasm>

I was making fun of his statement that binary code is easier than "the examples posted here".
Reply
#20
Ah, OK. Internet. Lack of tone. Programming discussion. Bad chin.
It's not having what you want - It's wanting what you've got.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)