Palindrometer Program

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Palindrometer Program
# 1  
Old 07-20-2011
Palindrometer 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:
\While driving the other day, John looked down at his odometer, and it read 100000. John
was pretty excited about that. But, just one mile further, the odometer read 100001, and
John was REALLY excited! You see, John loves palindromes (things that read the same
way forwards and backwards. So, given any odometer reading, what is the least number
of miles John must drive before the odometer reading is a palindrome? For John, every
odometer digit counts. If the odometer reading was 000121, he wouldn't consider that a
palindrome."
Write a script that solves this problem. The script is invoked with the current odometer
reading on the command line, and it should echo back the total number of miles to drive
to get the next reading that is a palindrome. For example when invoked as
should echo back
1
and
palindrometer 100000
palindrometer 000121
should echo back
979.



2. Relevant commands, code, scripts, algorithms:
I couldn't understand how to make this program, I tried my best to understand the program but I couldn't it.


3. The attempts at a solution (include all code and scripts):
Tried with 6 digit input and in next palindrome subtracted it


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Narasaraopeta Engineering College, Guntur (Andhra Pradesh) INDIA.
Professor: Sudeepthi, Code No: Q0501/R05 (Unix and Shell Programming)

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 07-20-2011
Quote:
Originally Posted by sujithcrazy
2. Relevant commands, code, scripts, algorithms:
I couldn't understand how to make this program, I tried my best to understand the program but I couldn't it.
Still, what did you try? It'd also be good to know what shell we're supposed to use!
# 3  
Old 07-20-2011
I used Borne Shell
# 4  
Old 07-20-2011
"What variety is your shell?" "Bourne"
"What model is your car?" "Blue"

Both very vague answers. If I wrote an answer for you in my shell odds are good it wouldn't work in your shell.

If you don't know what your shell is, do you know what your system is?

Last edited by Corona688; 07-20-2011 at 03:50 PM..
# 5  
Old 07-20-2011
While Corona is absolutely correct in stating that you should document your environment and what you tried so far (btw: it is good style in IT to document thoroughly! You can't start too soon to develop good habits!) i'd like to tackle a different part of your problem meanwhile:

How would you solve the problem of finding the next palindrome number manually? Which mathematical algorithm would you use?

I hope this helps.

bakunin
# 6  
Old 07-30-2011
I agree with the above posters. We need to know precisely what Operating System you have and exactly what Shell you have. Nowadays it is quite rare to encounter a raw Bourne Shell.
The palindrone question is an advanced text manipulation exercise which usually requires you to first determine the length of the string, then reverse the order of the characters in the string, and then compare the before and after string. If they are the same then it is a palindrome.
There are some extraordinarily complex methods for doing this involving arrays however it may be worth checking out the unix "rev" command if this command is allowed by your tutor.

This example script should work with any true Bourne Shell.
Code:
string="abba"
string_rev="`echo "${string}"|rev`"
if [ "${string}" = "${string_rev}" ]
then
    echo "Palindrome: ${string}"
fi


Ps. The straightforward answer to the original question is "zero". I do hope that this isn't an interview question.
However if you continue from 100001 and attempt to find the next palindrone (which I guess is 101101) by incremental methods then this is much more interesting.


Conforming to old Bourne Shell syntax we arrive at something like this:

Code:
counter="100001"
while true
do
        counter=`expr ${counter} + 1`
        reverse=`echo "${counter}"|rev`
        echo "${counter} ${reverse}"
        if [ "${counter}" = "${reverse}" ]
        then
                echo "Palindrone: ${counter} ${reverse}"
                break
        fi
done
# Deliberately ignored the requirement to return either 1 or 979 but this is trivial.
# Also deliberately avoided the requirement for the initial mileage to be a parameter. The core code is to find out the next palindrone

If your course context suggests that the string reversal bit should be done with string arrays, then it is up to you to how you reverse the string.

Last edited by methyl; 07-30-2011 at 09:24 PM.. Reason: Wrong guess.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

3. Programming

Calling c program from another c program

Hi All, Probably this is a repeated question. My knowledge in this is limited and i got confused on all those materials i got in google search. We use #include <> to include a predefined library like stdio.h i saw somewhere that #include "" includes a man made module(another C program). IS... (2 Replies)
Discussion started by: jisha
2 Replies

4. Programming

Python program faster than C++ program.

I wrote a simple program that generates a random word 10,000,000 times. I wrote it in python, then in C++ and compared the two completion times. The python script was faster! Is that normal? Why would the python script be faster? I was under the impression that C++ was faster. What are some of... (2 Replies)
Discussion started by: cbreiny
2 Replies

5. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

6. UNIX for Advanced & Expert Users

C program

Can anyone tell me how to..... program to create multiple threads (one master thread and rest worker threads) and using the threads write into and read from shared memory Restrictions: Only one thread should be able to read at any instant of time into shared memory region. Only one... (2 Replies)
Discussion started by: kumars
2 Replies

7. Programming

A program to trace execution of another program

Hi, I wanted to know if i can write a program using switches and signals, etc to trace execution of other unix program which calls c program internally. If yes how? If not with signals and switches then are there any other methods apart from debugging with gdb/dbx. (3 Replies)
Discussion started by: jiten_hegde
3 Replies

8. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

9. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

10. Programming

executing a program within a program

Read the title: how do i do it? (4 Replies)
Discussion started by: Gekko
4 Replies
Login or Register to Ask a Question