home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Homework Problem....

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2465
Discussions: 200,959, Posts: 2,379,551, Members: 246,329
Old October 19th, 2002, 01:31 AM   Digg it!   #1 (permalink)
Member
 
tenor_david's Avatar
 
Join Date: Nov 2001
Location: Bloomington IN
Posts: 219
Homework Problem....

I am stumped on this problem....can anyone help me out:

Create a Perl program that builds a list data structure using either hashes or arrays.
Your program will read values from the keyboard and insert them into the list in the order
that they are entered until it encounters an "x". After that, your program should ask
the user to search for a value. If the value is in the list, print out the left and
right neighbor of the value. Your output should adjust according to cases where there
is no left neighbor and / or no right neighbor. If the output is not in the list your
program should output an appropriate message. If the user enters "done" then your
program should print the contents of the list, thank the user, and then terminate.

I have tried about 15 different ways to do this, and none of them are even close to functional.
tenor_david is offline   Reply With Quote
Old October 19th, 2002, 02:02 AM     #2 (permalink)
Senior Member
 
Join Date: Aug 2002
Location: Meeshigan
Posts: 605
Hashes are really easy. However, since I had to write several of them in C++ for one of my computer classes, I won't deprive you of the experience. I will however, give you some general directions.

First, simply create your array of N elements. This should be at least twice the number of data elements you plan to enter. It should also be a prime number. Initialize every element to an invalid value.

Second, read in a value. Calculate the hash position by taking the value and dividing it by N (from the first step). Look in this position in the array. If there is an element there, look in the next position until you find an empty one. Put the value in that position. Repeat as needed.

Last is your search routine. You'll probably want to write a printing routine to display exactly what's in each position before you do this. This way, you can verify that each value is in the correct position. After this, simply re-calculate the hash position from the value entered (to search for) and look for the value in that position. If it's not there, move on to the next position. Keep doing this until you either hit the value or one of the invalid values that you initialized the array with. Then simply display the values in the positions on either side of this.

Good luck and have fun! I remember doing mine in the lab was a blast! Then again, I am a very odd and somewhat sadistic individual when it comes to coding. (It wasn't nearly as fun as when the blond chick I was helping with her assembler program planted a sloppy wet one right on my lips after I got her program working after she had spent the last 18 hours on it and 10 minutes before the class began, but fun none-the-less. )
__________________
About 5% of the people in the world can't think.
Another 5% can think and do.
The remaining 90% can think, but don't.
Ruler2112 is offline   Reply With Quote
Old October 19th, 2002, 11:15 AM     #3 (permalink)
Member
 
tenor_david's Avatar
 
Join Date: Nov 2001
Location: Bloomington IN
Posts: 219
Cool, thanks for the tips! It is much better to be given clues that lead to the Ultimate Satisfaction of solving thr problem, rather than the answer itself!
tenor_david is offline   Reply With Quote
Old October 21st, 2002, 02:33 AM     #4 (permalink)
Senior Member
 
Join Date: Aug 2002
Location: Meeshigan
Posts: 605
Well I look at it this way- if I were to simply post the code here, your instructor may very well see it (which would be bad) and even if he didn't, you wouldn't learn a damn thing! Even when helping the blazingly hot chicks in the lab, I'd make em struggle, just so they learn *how* and *why* things are done the way they are.

The blond chick I mentioned above with the nice lips was actually about 4 lines away from having her program working. 18 hours straight of working on it and she was going to turn it in completely non-functional. I sat down and showed her how to use debug to trace through the program, dump memory addresses, etc (which the idiot instructor teaching the course never mentioned when I had it or when she was taking it), she told me what needed fixing. She learned, got the program working, and got one out of 4 As in the class on it, all at the same time.
Ruler2112 is offline   Reply With Quote
Old October 21st, 2002, 06:13 PM     #5 (permalink)
Member
 
tenor_david's Avatar
 
Join Date: Nov 2001
Location: Bloomington IN
Posts: 219
Ok Ruler2112, I got it working great, using arrays rather than hashes. The next problem takes it one step further, allowing users to store data in 4 directions [N S E W]and I almost have it working, I think. But, I cannot get it to properly store items in the hashes.

Here is my brute force code:

#!usr/local/bin/perl

%North;
%South;
%East;
%West;

$count = 0;

print "Enter different pieces of data into a list. You may insert data
into 4 directions. When entering data, use the following syntax\:
\"value\:direction\:value\" where direction is either N S E or W. Press
\"x\" to stop:\n";

while (1) {

chomp($String[$count] = <STDIN>);
@input = split /:/, $String[$count];
if ($input[1] == N) {
$North{$input[0]} = $input[2];
$South{$input[2]} = $input[0];
}
elsif ($input[1] == S) {
$South{$input[0]} = $input[2];
$North{$input[2]} = $input[0];
}
elsif ($input[1] == E) {
$East{$input[0]} = $input[2];
$West{$input[2]} = $input[0];
}
elsif ($input[1] == W) {
$West{$input[0]} = $input[2];
$East{$input[2]} = $input[0];
}
print %North;

if ($String[$count] =~ /^x$/i) {
pop @String;
print "Search for a value and its neighbors:";
chomp($search = <STDIN>);
@results = grep { $String[$_] =~ /^$search./ } (0..$#String);
if (@results == ()) {
print "The item is not in the list.\n";
}
foreach (@results) {
print $North{$search};
print $South{$search};
print $East{$search};
print $West{$search};

}
last;
} elsif ($String[$count] =~ /done/) {
pop @String;
print "Thanks user!\n";
print "@String\n";
last;
}
$count++;
}

Right now, if I input:
Center:N:top
then
Center:E:right
and search for center, it prints out:
rightright
when I want it to spit out topright.

Any thoughts?
tenor_david is offline   Reply With Quote
Old October 22nd, 2002, 01:43 PM     #6 (permalink)
Junior Member
 
Join Date: Oct 2002
Posts: 19
I couldnt get your code to work for me to do some testing.. but looking at it mayb change your comparison operator, == (numeric) to eq and see what happens.

if ($input[1] == N) {

to

if ($input[1] eq "N") {
wadeintothem is offline   Reply With Quote
Old October 22nd, 2002, 01:50 PM     #7 (permalink)
Member
 
tenor_david's Avatar
 
Join Date: Nov 2001
Location: Bloomington IN
Posts: 219
I changed the comparison operators to pattern matching, as the values can be either numeric or strings. But, still same output. It has something to do with how the 'search' portion is looping through the hashes I think....

This is a pain in me arse.
tenor_david is offline   Reply With Quote
Old October 22nd, 2002, 03:13 PM     #8 (permalink)
Senior Member
 
Join Date: Aug 2002
Location: Meeshigan
Posts: 605
I don't understand what you mean when you say that the program should be 'allowing users to store data in 4 directions [N S E W]'. Do you mean that you have a large grid and every time you push the same value, the data moves in that direction? If so, what's the point?

Also, I don't really know perl beyond what I've read in a book, so I'm going to be next to no help with the source code.
Ruler2112 is offline   Reply With Quote
Old October 22nd, 2002, 06:09 PM     #9 (permalink)
Member
 
tenor_david's Avatar
 
Join Date: Nov 2001
Location: Bloomington IN
Posts: 219
According to the instructor, you should be able to enter a value, with associated neighbors. Initially, these values shouldn't be 'pushed' anywhere. So the assumption is that the user would only insert certain types of data that would not cause existing data to 'move.'

I am at a loss on this one. Even the instructor was not able to 'point me in the right direction' without giving away the whole answer.

That makes me really question the instructional value of this problem....

Whatever.
tenor_david is offline   Reply With Quote
Old October 23rd, 2002, 02:49 AM     #10 (permalink)
Senior Member
 
Join Date: Aug 2002
Location: Meeshigan
Posts: 605
I'm sorry, but I still don't understand. You enter a value, say '4'. What 'neighbors' are you supposed to enter? If you enter a '4' and have it's neighbors be a '2' on the left/west and an '8' on the right/east, you'd simply have:

2 4 8

If you enter a 1 above/north of the 2, and a 3 below/south it, you'd have an array with the following:

1
2 4 8
3

If you then say enter a 0 above/north of the 1 and a 9 below/south of the 4:

0
1
2 4 8
3 9

In essence, you're writing an overly complex and clunky method of filling an array of unknown size. (This is why I think I've gotta be missing something.)



Frankly, this sounds like one of those BS problems that one of my instructors used to LOVE to give us. No point, nothing that pertains to anything you have a remote chance of doing in an actual program, nothing at all useful. Just struggle to accomplish something with no real point. The first few programs like this I got, I asked the instructor what use it would have. He tried to feed me some line, but then I shot it down (I was working as a software engineer at the time). He then said that it was a 'neat problem'. It was all I could do to NOT roll my eyes. I can understand writing a program to demonstrate the use of programming techniques, but it reaches a point that's rediculous! Drawing fractal ferns for example... nobody understood where the numbers came from and nobody had any idea why changing them produced different things- the instructor didn't either. We just wrote a program and plugged numbers in that the instructor got from a book because he 'thought it was neat'. ("I paid HOW much for this $#!+?!?" was all I could think of for all 3 4-hour periods we spent on this BS. )

Sorry for the rant.
Ruler2112 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2975)
The disrespect of Obama by Russian .. (45)
Making Health Care Worse (181)
Wireless Televisions. (12)
CPU fan stops spinning randomly (10)
Regular Build (11)
windows 7 problem (7)
Laptop with wireless problem. (5)
windows vista security holes (13)
Is the PSU I received dead? (13)
Point and Shoot Camera Suggestions. (5)
Print spooler problem (16)
radeon x850xt platinum & shader.. (6)
HIS HD5770 graphic card question (15)
Recent Discussions
Point and Shoot Camera Suggestions. (5)
CPU fan stops spinning randomly (10)
Is the PSU I received dead? (13)
Print spooler problem (16)
Nvidia GTX 260 problem (0)
windows vista security holes (13)
Kingston Bluetooth Dongle Driver (1)
Multiple Restarts Required at Boot (3)
Open With ..... Win7 (1)
webcam (0)
upgrade for hp a6101 (0)
Laptop with wireless problem. (5)
Modern Warfare 2: Who Bought It? (64)
tv not turn on-makes clicking sound (2)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)


All times are GMT -4. The time now is 10:57 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28