Little Man Program

Write a little man program that adds a column of input values and produces the sum as output. the first input value will contain the number of values that follow as input to be added.
Heath3Asked:
Who is Participating?
 
wilcoxonCommented:
How will the values be entered?  The basics of the program are simple.  Here is one that takes pipe-delimited lines of input of the above format and sums them.  If the input will be from standard input or from a file with one value per line, the looping code will need to change.
use strict;
use warnings;
use List::Utils qw(sum);
open IN, 'file.txt' or die "could not open file: $!";
while (<IN>) {
    chomp;
    my @vals = split /\|/, $_;
    my $num = shift @vals;
    if (@vals > $num) {
        splice @vals, $num;
    }
    print "sum: ", sum(@vals), "\n";
}

Open in new window

0
 
wilcoxonCommented:
Here would be one to base it on input from stdin.
use strict;
use warnings;
use File::Utils qw(sum);
my @vals;
my $num = <STDIN>;
chomp $num;
if ($num !~ m{^\d+$}) {
    die "input must be a number: $num";
}
print "expecting $num values\n1:\n";
while (my $val = <STDIN>) {
    chomp $val;
    if ($val !~ m{^\d+$}) {
        die "input must be a number: $val";
    }
    push @vals, $val;
    last if (@vals == $num);
    print scalar(@vals)+1, ":\n";
}
if (@vals != $num) {
   die "expected $num values but got " . scalar(@vals);
}
print "sum: ", sum(@vals), "\n";

Open in new window

0
 
Adam314Commented:
This sounds like homework.  Note that experts can not do your homework for you, but if you show effort, they can help you.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
MurpheyApplication ConsultantCommented:
You included the Programming for iSeries / AS400 zone in your question?

What language should it be written in?

0
 
wilcoxonCommented:
Both of my comments with code solve the question asked (each in slightly different ways).  I think the points should be awarded to one/both of my solutions since nobody else submitted any code.
0
 
wilcoxonCommented:
0
 
MurpheyApplication ConsultantCommented:
2) Delete, because no-one give a good answer in the "littleman" programming language (what was the question)

No refund, because the Author didn't take any time to respond on our remarks.
0
 
wilcoxonCommented:
murphey2, "littleman" is *not* a language - it is a type of programming problem.
0
 
MurpheyApplication ConsultantCommented:
Llittle man is not a programming problem it's a very basic assembler language

Description & command set : http://en.wikipedia.org/wiki/Little_man_computer

Programming tool (emulator) : http://www.acs.ilstu.edu/faculty/javila/lmc/

0
 
MurpheyApplication ConsultantCommented:
0
 
wilcoxonCommented:
I was not aware of that language (and my first attempt at google showed a few references to a problem rather than the language).

However, the author posted it to the perl programming zone (and did not include any additional tags or zones indicating otherwise) so he obviously was fine with a perl solution.
0
 
MurpheyApplication ConsultantCommented:
Yes and in the Programming for iSeries / AS400
Littleman was part of my training..... just try it... its harder then it looks :-)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.