Day 1: Hello World program

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
I have decided to help people out, that they can submit programs that follow the assignment I hand out every 2 days in any programming language, and as the assignments get harder, people learn along the way.

The first assignment is really two, but they are rather simple.

Assignment 1: Write a Hello World program.

Assignment 2: Create a program that displays the message "OSNN.net rules my world" on four lines. Such as:

OSNN.net
rules
my
world

Then modify this same program to instead display it with tabs in between the words, and then staggers the output, so if the third word gets output, there are three tabs in front of it.

Post your code here for everyone to see and comment on. I will post my own code after I have a few submissions. If no-one submits any, I may decide to not do this anymore. Any programming language accepted. Massive reps to people who learn Brain**** along the way.
 
Can I cheat and use shellscript or does it have to be a "real" language? :p
 
Can I cheat and use shellscript or does it have to be a "real" language? :p

You can do it in any language you desire, however when I post new challenges, your chosen language may not contain all the required functions or be able to complete all of the assignments.

This is also to help people learn new programming languages by going out and researching how these tasks would be accomplished in a language of choice, and to then let others comment on how it could be improved.
 
How about some 16-bit ASM

Code:
[SIZE=2];=================================================[/SIZE]
[SIZE=2]; Author : Albert Holtsclaw[/SIZE]
[SIZE=2]; Web    : [URL]http://www.albybum.net[/URL][/SIZE]
[SIZE=2]; Purpose: Hello World Program[/SIZE]
[SIZE=2]; Lang   : 16bit x86 ASM - Borland TASM Assembler[/SIZE]
[SIZE=2];=================================================[/SIZE]
[SIZE=2].MODEL  SMALL[/SIZE]
[SIZE=2].STACK  100h[/SIZE]
 
[SIZE=2]puts$ macro msg[/SIZE]
[SIZE=2]push ax    ;preserve ax because we use ah[/SIZE]
[SIZE=2]push dx    ;preserve dx[/SIZE]
[SIZE=2]  mov  dx,offset msg        ;set location for message to display[/SIZE]
[SIZE=2]  mov  ah,09                ;set function to display message[/SIZE]
[SIZE=2]  int  21h                 ;display message[/SIZE]
[SIZE=2]pop  dx    ;restore dx[/SIZE]
[SIZE=2]pop  ax    ;restore ax[/SIZE]
[SIZE=2]endm[/SIZE]
[SIZE=2]putc macro chr1[/SIZE]
[SIZE=2]push ax    ;preserve ax because we use ah[/SIZE]
[SIZE=2]push dx    ;preserve dx because we use dl[/SIZE]
[SIZE=2]mov  ah,02   ;set function to display char[/SIZE]
[SIZE=2]mov  dl,chr1   ;set character to display[/SIZE]
[SIZE=2]int  21h   ;display character[/SIZE]
[SIZE=2]pop  dx    ;restore dx[/SIZE]
[SIZE=2]pop  ax    ;restore ax[/SIZE]
[SIZE=2]endm[/SIZE]
 
[SIZE=2].DATA[/SIZE]
[SIZE=2]STRHELLO db 10,13,'Hello World!','$'[/SIZE]
[SIZE=2]STROSNN  db 'OSNN.net rules my world','$'[/SIZE]
[SIZE=2]STRTAB   db '     ','$'[/SIZE]
[SIZE=2].CODE[/SIZE]
[SIZE=2]mov ax,@data           ;set data segment location[/SIZE]
[SIZE=2]mov ds,ax[/SIZE]
 
[SIZE=2]puts$  STRHELLO   ;print Hello World[/SIZE]
[SIZE=2]putc 10                         ;print CR and LF[/SIZE]
[SIZE=2]putc 13[/SIZE]
[SIZE=2]puts$  STRTAB   ;print first tab[/SIZE]
[SIZE=2]mov  cx,1   ;set number of tabs, start 1[/SIZE]
[SIZE=2]mov  bx,offset STROSNN  ;set location of OSNN string[/SIZE]
[SIZE=2]STLOOP:[/SIZE]
[SIZE=2]cmp  byte ptr [bx],'$'       ;are we at the end of the string?[/SIZE]
[SIZE=2]je   FINLOOP   ;if so, end loop[/SIZE]
[SIZE=2]cmp  byte ptr [bx],' '       ;is the current character a space?[/SIZE]
[SIZE=2]je   ADDTAB                  ;if so, increment tab count[/SIZE]
[SIZE=2];    ;otherwise, we can print character[/SIZE]
[SIZE=2]putc [bx]                 ;print character [/SIZE]
[SIZE=2]inc  bx   ;go to next character[/SIZE]
[SIZE=2]jmp  STLOOP                  ;loop character loop[/SIZE]
[SIZE=2]ADDTAB:[/SIZE]
[SIZE=2]inc cx   ;add a tab for next loop[/SIZE]
[SIZE=2]inc bx                       ;go to next character[/SIZE]
[SIZE=2];    ;print tabs[/SIZE]
[SIZE=2]push  cx   ;save counter register[/SIZE]
[SIZE=2]putc 10                      ;print CR and LF[/SIZE]
[SIZE=2]putc 13[/SIZE]
[SIZE=2]PUTTABS:[/SIZE]
[SIZE=2]puts$ STRTAB   ;print tab[/SIZE]
[SIZE=2]loop  PUTTABS  ;loop[/SIZE]
[SIZE=2]pop   cx   ;restore counter register[/SIZE]
[SIZE=2]jmp STLOOP                   ;continue character loop[/SIZE]
 
[SIZE=2]FINLOOP:[/SIZE]
[SIZE=2]mov  ax,4c00h     ;end program[/SIZE]
[SIZE=2]int  21h          ;[/SIZE]
[SIZE=2]end[/SIZE]

edited - removed a macro I wrote but didn't use/need.
 

Attachments

  • TEST.zip
    595 bytes · Views: 76
Last edited:
Valiant idea, X-Istence..
Since VB.NET is generally a beginner language, here's a decent lesson.

ControlChars.Tab in VB.NET doesn't work in labels and I wanted to keep that 'non-text box' look going. So here is the entire solution + exe in a zip.

Code:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] tab4Lines_Enter([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] tab4Lines.Enter[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] txtOSNN [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"OSNN.NET"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] txtRules [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"rules"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] txtMy [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"my"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] txtWorld [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515]"world!"[/COLOR][/SIZE]
[SIZE=2]lblOSNN.Text = txtOSNN[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] lblOSNN.Text = txtOSNN [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]lblRules.Text = [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + txtRules[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] lblRules.Text = [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + txtRules [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]lblMy.Text = [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + txtMy[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] lblMy.Text = [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + txtMy [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]lblWorld.Text = [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] + txtWorld[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 

Attachments

  • HelloWorld.zip
    46.5 KB · Views: 72
Ruby:

Assignment 1
Code:
#!/usr/bin/env ruby
puts "Hello world".

Assignment 2
Code:
#!/usr/bin/env ruby
%w{OSNN.net rules my world}.each { |w| puts w }

Assignment 2 part 2
Code:
#!/usr/bin/env ruby
index = 0
%w{OSNN.net rules my world}.each do |word|
    print "\t" * (index += 1)
    puts word
end
 
Last edited:
Code:
#include <iostream.h>

main()
{
    for(;;)
    {
        cout << "Hello World ";
    }}
 
Code:
#include <iostream.h>

main()
{
    for(;;)
    {
        cout << "Hello World ";
    }}

This is not valid C++ code on any modern compiler. In the latest C++ standard all header files have gone from having the .h on the end to being without them, so it would be

Code:
#include <iostream>

Now, up next is the function main. It should always return an integer. http://users.aber.ac.uk/auj/voidmain.shtml explains why.

Code:
int main()

Your for loop will go indefinitely and I guess that is what you want it to do. Now, the next line is the cout line, which is also wrong. Nowhere do you specify

Code:
 using namespace std;

So you have to prepend cout with an std::, like this:

Code:
std::cout << "Hello World";

This code would now spit out:

Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World

Maybe not what you had in mind, but it works, better would be to add new lines to the end to make the text start on a new line each time:

Code:
std::cout << "Hello World" << std::endl;
 
I've been asked to do a python example.

Code:
#!/usr/bin/env python
print "Hello world"

Assignment 2
Code:
#!/usr/bin/env python
s = "OSNN.net rules my world"
print "\n".join(s.split())

Assignment 2 part 2
Code:
#!/usr/bin/env python
words = "OSNN.net rules my world".split()
print "\n".join(["\t" * words.index(w) + w for w in words])
 
*shrug* I guess I'll toss in vbscript/Script Host
I suppose I could comment it to explain it all, but, it's not that hard to follow.

edit: this is assignment 1 and 2 (and 2b) all rolled into one.

edit2: hmm.. did you want the 2b part to all be on one line? That's how I wrote it .. i can modify it to display each word on each line.. hmm

edit3: there.. now it does it both, all on one line, and each word on a seperate line..

Code:
option explicit

dim strText, strTemp
dim arrText
dim i
dim intTabCount

strText = "OSNN.net rules my world"
arrText = split(strText)

'assignment 1
wscript.echo "Hello World"

strText = ""
for i = 0 to uBound(arrText)
  
  'if you want a tab in front of the first word, get rid of this if statement
  if i > 0 then 
    for intTabCount = 0 to i
      strText = strText & vbTab
    next
  end if

  strTemp = arrText(i)
  strText = strText & strTemp

  'assignment 2
  wscript.echo strTemp
next

'assignment 2b (all on one line)
wscript.echo strText

'assignment 2b (each word on a seperate line)
for i=0 to uBound(arrText)
  strTemp = ""

  'again, if you wanted a tab in front of the first word, ditch this if
  if i > 0 then 
    for intTabCount = 0 to i
      strTemp = vbtab & strTemp
    next
  end if

  strTemp = strTemp & arrText(i)

  wscript.echo strTemp
next
 
Last edited:
Since you all seem intent on the complex stuff here it is nice and simple:

Code:
#! /usr/local/bin/bash
echo "Hello World"
echo OSNN.net
echo rules
echo my
echo world

Tabs are as simple as pre-pending the correct number of \t's before the text to output :)
 
Since you all seem intent on the complex stuff here it is nice and simple:

Code:
#! /usr/local/bin/bash
echo "Hello World"
echo OSNN.net
echo rules
echo my
echo world

Tabs are as simple as pre-pending the correct number of \t's before the text to output :)
yes, but in all fairness, he said something about "if" the prior line had the tabs, add the tabs to the next line. so shouldn't there be an If statement in there ? :)
 
Ok I caved for simplicity, no IF statements! hehe
Code:
[LEFT][SIZE=2][COLOR=#3f7f5f]//-------------------------------//[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f]//-Author: Eric Rodewald -------//[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f]//-Project: Hello World --------//[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f]//-Language: Java JRE1.6 ----//[/COLOR][/SIZE][/LEFT]
 
 
 
[SIZE=2][COLOR=#7f0055][B]public [/B][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/B][SIZE=2] Hello {[/SIZE]

[LEFT][B][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]static [/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/B][SIZE=2] main(String[] args) {[/SIZE]
 
[LEFT][SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff]"\tOSNN.net"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff]"\t\trules"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff]"\t\t\tmy"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff]"\t\t\t\tworld!"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]

[/LEFT]



 
lol. i havent messed with coding in a while, since my day @ the uni thanks for your breakdown
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back