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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Smartphone Programming

From novice to tech pro — start learning today.