PDA

View Full Version : Pascal Chapter 10 homework




larswik
Apr 18, 2011, 01:15 AM
I have my new Pascal assignment due Wednesday.

Assignment : I have to READ in 2 data files. File 1 has 30 'Y' or 'N' and is the customer requests for the apartment. The second file has 5 line and each line has 30 'Y' or 'N' followed by an address. The goal is to read in the files and match up the customer with the best apartment by how many Yes's match and print out the address with it.

For this assignment I must use an Array[i]. The problem that I have is not a programing issue (yet), but this is the first project that I need to READ in 2 data files. when I run the program I don't know how to have it ask me for the second file?

program test(infile, outfile);

type
ary = array[1..30] of char;

var
custwants : array[1..30]of char;
infile, outfile : text;
i : integer;

begin
reset(infile);
rewrite(outfile);
(**** Read in Customers Wants*)
for i := 1 to 30 do
read(infile, custwants[i]);

reset(infile);

for i := 1 to 30 do (* testing the read in*)
writeln(outfile, custwants[i]);
end.

I tried to use the RESET(INFILE) again hoping it would ask for another file, but it did not.

-Lars



jiminaus
Apr 18, 2011, 02:05 AM
Can you put more the 2 files in program statement?


program test(infile1, infile2, outfile);
(* snip *)
reset(infile1);
reset(infile2);
rewrite(outfile);

larswik
Apr 18, 2011, 02:30 AM
Yes, that worked. I never thought about it that way. I thought I could just call infile again and it would ask me for another file.

Thanks!

-Lars

Bill McEnaney
Apr 18, 2011, 07:05 PM
Yes, that worked. I never thought about it that way. I thought I could just call infile again and it would ask me for another file.

Thanks!

-Lars
Lars, you could associate the name "infile" with another external file instead of the current one. But the computer doesn't know what "infile" means. You could have replaced "infile" with "Donald_Duck," and your program would still have made the computer do what it did when you input file's internal name was "infile." The computer usually doesn't care what you call your variables, your files, your procedures, and so forth. But sometimes it'll complain when you try to use a programming language's keywords as, say, variable names. It'll complain when, for example, you try to declare a variable named "writeln."

Years ago, when I learned Cobol, my professor proved my point when he wrote a whole program where the variable names didn't tell you what they stood for. He wrote something like, "Add Donald-Duck to Woody-Woodpecker giving two-birds." Or how about, "Perform Mac-N-Cheese 20 times."?

larswik
Apr 19, 2011, 01:54 AM
Is what you are talking about 'reserved' words? I named it infile because I knew that was the best item for me to describe the process. When the program launches it asks for all the files up front to process. After jiminaus wrote that answer it clicked that what I write in the 'PROGRAM TEST (test1, test2, test 3);' Those are just 3 variables, or I think of them kind of like gate ways of passing the information from the outside to inside the program. I can make all the tests inputs or outputs.

Thanks for the extra help.

-Lars

Bill McEnaney
Apr 19, 2011, 02:27 AM
Is what you are talking about 'reserved' words
Yes, I'm talking about reserved words.

I named it infile because I knew that was the best item for me to describe the process. When the program launches it asks for all the files up front to process. After jiminaus wrote that answer it clicked that what I write in the 'PROGRAM TEST (test1, test2, test 3);' Those are just 3 variables, or I think of them kind of like gate ways of passing the information from the outside to inside the program. I can make all the tests inputs or outputs.
Oops. Maybe I misinterpreted something Lars told us. I thought he thought the computer would automatically make "infile" stand for another input file any time the computer needed to use another one.

I like the gateway analogy.

Thanks for the extra help.
My pleasure.

jiminaus
Apr 19, 2011, 02:48 AM
Oops. Maybe I misinterpreted something Lars told us.

Lars was under the mistaken impression that it was the reset/rewrite procedures which cause the file prompts, not the program parameters (the gateways, as from now on they'll be called here).

Bill McEnaney
Apr 19, 2011, 11:06 AM
Lars was under the mistaken impression that it was the reset/rewrite procedures which cause the file prompts, not the program parameters (the gateways, as from now on they'll be called here).
Hmm, I wonder what gave him that impression. I don't see any connection between those procedures and those prompts.

ehoui
Apr 19, 2011, 12:45 PM
Out of curiosity and admittedly OT, why is your prof teaching Pascal? :)

Bill McEnaney
Apr 19, 2011, 01:04 PM
Out of curiosity and admittedly OT, why is your prof teaching Pascal? :)
Everybody knows that Pascal is better than C. ;)

larswik
Apr 20, 2011, 01:24 AM
Wow lots of replies. Sorry, what I meant was the reset and rewrite. If I declared 3 variables of type Text I can make them all reset for input or rewrite for output. Before the problem was solved I tried to use 2 resets both named infile. I thought once it finished with 1 file it would then ask for the next, this was not the case.

As for why the teacher is using Pascal he said the next version of smart phones will go back to an easier language, like Pascal :) No, JK, he said it was a straight forward language to learn and most everything we learn will be applied to other languages in the future.

Although I had my midterm Monday and he only gave 1 hour 15 minutes to do it. my 40 year old brain is not as fast as these kids half my age. I only got 2/3's finished. The most I can get is a C if I got everything right on the test that I had time to do.

-Lars