writing math testing program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting writing math testing program
# 1  
Old 04-29-2008
writing math testing program

Can anyone create a program that
would test the math skills of the user. Assume that it would test integer addition, The program should ask the question,collect the integer response, evaluate and notify the user if their answer was correct or incorrect. I would assume integers in the range {1..100}.

either shell scripts or perl should be OK
Thanks a bunch
# 2  
Old 04-30-2008
Are you asking for someone to write you the program or just if it is possible? It is possible.
# 3  
Old 04-30-2008
Can I do it this way?

#!/usr/bin/perl

print "\nplease enter first number, second number, enter M for multiplication, A for addition,S for substraction, and finally your answer";


chomp ( $1=<STDIN>);
chomp ( $2=<STDIN>);
chomp ( $3=<STDIN>);
chomp ( $4=<STDIN>);

if $3 ~/M/i && ($4 == $1 * $2)
{ print " your answer is right";}

elsif
if $3 ~/A/i && ($4 == $1 + $2)
{ print " your answer is right";}

elsif

if $3 ~/s/i && ($4 == $1 - $2)
{ print " your answer is right";}
# 4  
Old 04-30-2008
The code you posted has a few syntax errors. Don't use numbered variables ($1, $2 etc) those are reserved by perl for pattern memory and are read-only. Use the warnings pragma:

Code:
use warnings;

You have the general idea although you should also validate the user input (like make sure the user has only entered integers) and correct the synax errors. Its also good to add a fall-through condition to the end of an if/elsif chain:

Code:
if () {
}
elsif () {
}
elsif () {
}
else {
   fall-through condition
}

# 5  
Old 04-30-2008
Kevin, can you reread the question again, I need to ask user question "One time" only, also should I use some function like rand ()?

collect the integer response, evaluate and notify the user if their answer was correct or incorrect. I would assume integers in the range {1..100}
# 6  
Old 05-01-2008
Quote:
Originally Posted by ccp
Kevin, can you reread the question again, I need to ask user question "One time" only, also should I use some function like rand ()?

collect the integer response, evaluate and notify the user if their answer was correct or incorrect. I would assume integers in the range {1..100}

Use rand for what? I thought the user was going to input the numbers and the operation they want to perform ( x + - / etc).

If you want the user to input all the data at one time:

Code:
print  "Enter two numbers between 1 and 100 and one of the following math operations to perform [ + - x /] and the answer. Example: 3 X 17 = 51. ";

Then you check that there are two numbers, a math operator and the answer.

Code:
chomp (my $input = <STDIN>);
my ($int1, $operator, $int2, undef, $answer)   = split(/\s+/,$input);
here you validate the input and 
perform the math and check the answer

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl math program

I have this math program on Perl and when I run it one of the math problems that comes up is 0/0 and perl makes the answer 4. How can I program perl to not include numbers /0? What I have for the division portion is: $solution = $num1 * $num2; ($solution, $num1) = ($num1, $solution); $question... (2 Replies)
Discussion started by: germany1517
2 Replies

2. Homework & Coursework Questions

Need help writing a shell script program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write the a shell script program to remove all space characters stored in the shell variable TEXT.... (7 Replies)
Discussion started by: kofine05
7 Replies

3. Homework & Coursework Questions

Help with writing a tee program in 4 hours

1. The problem statement, all variables and given/known data: Basic Assignment Write a program similar to the Unix "tee" command. Program The Unix "tee" command is used to pull out copies of a data stream. It is typically used in conjunction with pipes (analogous to a T-joint in plumbing, hence... (1 Reply)
Discussion started by: izzy077
1 Replies

4. Programming

writing a shell program in ubuntu

Hi all, I have an assignment from school to write a shell program in linux. the idea is to exercise fork() and execv() functions.. the shell program is supposed to be the master and every command that the user prints will run in a new process. we also need to try running the command in every... (1 Reply)
Discussion started by: r3vive
1 Replies

5. UNIX Desktop Questions & Answers

need help writing a program to look for doubles

to determine if two two doubles are equal, we check to see if their absolute difference is very close to zero. . .if two numbers are less than .00001 apart, theyre equal. keep a count field in each record (as you did in p5). once the list is complete, ask the user to see if an element is on... (2 Replies)
Discussion started by: rickym2626
2 Replies

6. UNIX for Dummies Questions & Answers

Problem with writing a program

Hi guys I'm having trouble with trying to create a script which calculates the grade of a student and the marks out of 300. The grades are: 0-49% fail 50-59% pass 60-69% credit pass 70-79% distinction 80-100% high distinction less than 0 or greater than 100 displays error message. My... (1 Reply)
Discussion started by: CompNoob
1 Replies

7. Shell Programming and Scripting

Need help on a program I'm writing

Hey guys.. I'm trying to learn how to script in bash... THIS IS NOT AN ASSIGNMENT but my instructor says to learn you must practice I'm trying to add to a program I'm writing that will print and save raw data... What syntax commands would I use to write them? And Thank you... (2 Replies)
Discussion started by: dmosheye
2 Replies

8. UNIX for Dummies Questions & Answers

writing a program

i have written a very simple program in the vi editor, how do i now make it an executable file? (3 Replies)
Discussion started by: carlvernon
3 Replies

9. What is on Your Mind?

I'm writing a new Linux program!

Yep, that's right. I'm writing a Linux binary that requires an X11 Server. It will also be released in a Shell, Win32, and Cocoa (Mac OS X). It's a program that's a text editor and more. It not just creates TXT and RTF files, it also can save in XML, RSS, and a whole lot of other formats. ... (11 Replies)
Discussion started by: Danny_10
11 Replies

10. Programming

basic math program 4 child

Iam writing a script for my sisters friends little brother i want the program to say hello lets say his name is Joe and ask him how he is and he can write a reply back saying fine and then it replys "Iam happy you are (his response) today and then it goes into a basic math where he can put in a... (9 Replies)
Discussion started by: perleo
9 Replies
Login or Register to Ask a Question