Question

Okay, so I'm still learning the basics of programming. Still learning to use pseudocode. I have read, and practised to write pseudocode operations. I still get so confused, and when I ask questions to my instructor I feel like I bother her. Maybe this is not the case, but this is how I always feel whenever I have questions, but I'm trying to break that barrier. So I'm going to give an example from my book, and I'll make some questions see if someone can help me understand.

The example is about inserting records into a sequential file. My textbook says:

"Let’s assume that the contents of the grades file lists records alphabetically according to student name. Suppose a new student joins the class. Now we need to insert a new record. We must place data in each field of that record. In this example, grades has two fields—one for the student’s name and one for the student’s score. So we want to insert values that are stored in the variables called NewName and NewScore. These values will be entered by the user and will be inserted into this file at the appropriate place, retaining alphabetical order."

Then, it express the pseudo-code:

1  Declare NewName As String
2  Declare NewScore As Integer
3  Open “grades” For Input As GivenFile
4  Open “scratch” For Output As TempFile
5  Write “Enter name and score for the new student:” 
6  Input NewName, NewScore
7  Set Inserted == 0
8  While (NOT EOF(GivenFile)) AND (Inserted == 0)
9    Read GivenFile, Student, Score 
10    If NewName < Student Then
11       Write TempFile, NewName, NewScore
12      Set Inserted = 1
13     End If
14   Write TempFile, Student, Score 
15 End While
16 If Inserted == 0 Then
17   Write TempFile, NewName, NewScore 
18 End If
19 While NOT EOF(GivenFile)
20   Read GivenFile, Student, Score 
21   Write TempFile, Student, Score
22 End While
23 Close GivenFile, TempFile 
24 Copy scratch onto grades

I don't understand line 3 and 4, why one is Input and the other is Output? I thought Input is what is going to be inserted in the program, and output is the results of what it was inserted in the program.

Also, I have used the relational operators just with numbers, so I'm getting confused now that I see them as it is on line 10. What is it trying to tell me? Is (NewName < Student) telling me if there is a new student to then write the TempFile, Student, and Score?

In the first While loop is giving a Inserted = 1, then it ends the loop. Then repeats the Inserted == 0 (line 16), does this means in case there is another new student to repeat the while loop? I really got lost on this one.

At the end, why do we have to copy scratch onto grades?

I'm sorry for all the questions, I'm so confused, and I'm taking the classes online, which I have to teach myself, and I have nobody to discuss this with.

Note: This have nothing to do with an assignment, just trying to understand better the logic of programming.

Was it helpful?

Solution

You might try and develop an algorithm to write the new record to the correct place in the file, but we run into problems:

while (more lines to read) {
   if new name comes after name in file {
       this might be the right place but I can't tell - depends what the 
       next name is but I have't read that yet - don't know what to do!
   } else {
       now it's too late, should have written new record earlier!
   }
}

Instead, we use a 2nd file, copy the records from it, inserting the new record at the right time:

while (more lines to read) {
    if name in file comes after new name, and I have not yet written new name {
        write new name and score to temp file
    }
    copy name and score from input file to temp file
}

After doing that, the temp file contains all the same records as the input file, but with the new record added in the right place.

The rest of the pseudo code just copies the new file back into the original file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top