The great thing about pseudocode is that if done right, the code writes itself
First things first, sort out the data. You're right in sorting numbers instead of letters (they're a lot easier to compare ) At the start, we're talking about:
Code:
List[MAX] - Array of Integers (MAX = number of records)
Record - Highest/Lowest in the list
Once you've got your basic data stuff down (don't worry - programs almost always have more data put in ) in let's start at the basic ascending program in one swoop:
Code:
- Look up a number in an array "row"
- If row's number is higher than the Record
- Record = ID of the row
The reason of using the IDs instead of the actual data themselves is so the data doesn't get tangled around. The actual data is insignificant - It's the program's job to simply sort them.
Now that code above will do it for one swoop - The next stage is to add a loop to it
Code:
PROCEDURE Ascending_Search
Record = 1
Do a loop of n = 1 to n = MAX
{
- Look up a number in an array "row"
- If row's number is higher than the Record
- Record = ID of the row
}
That is your basic searching algorithm - It will look at each row and will end up recording the ID of the highest number in the list. Now, we have to do the actual switch.
Unfortunately, switching isn't a case of switching between the two as one line will overwrite one of the data. A third interger, temp, has to be used to hold one of the values during the switch.
Code:
PROCEDURE Switch
{
- Put list[n] into temp
- Put list[Record] into list[n]
- Put temp into list[Record]
}
The neat thing about using the IDs is that you don't need seperate procedures for switching ascending/descending - Because all it's doing is switch the data of each ID, it will work either way (yay for optimisation! )
Now we can put the whole thing together by declaring the switch algorithm and and also adding a record = 1 to reset it each time (a good thing to remember when using global variables)
Code:
PROCEDURE Ascending_Search
Record = 1
Do a loop of n = 1 to n = MAX
{
- Look up a number in an array "row"
- If row's number is higher than the Record
- Record = ID of the row
}
- Execute procedure "Switch"
}
What that will do is reset the record counter, make a loop of looking for the row in the array that holds the highest number, and then make the switch in a three-point-turn fashion.
The next stage involves looking at the main function, the one that will start the program entirely. By this, you can also modify the search & switch procedures to compensate - Using main means you can set it so it doesn't go back on past territory
Code:
PROCEDURE Main
Do a loop of k = 1 to k = MAX
{
- Execute procedure "Ascending_Search" (with m = k)
}
PROCEDURE Ascending_Search (parameter of m = interger)
Record = m
Do a loop of n = m to n = MAX
{
- Look up a number in an array "row"
- If row's number is higher than the Record
- Record = ID of the row
}
- Execute procedure "Switch"
}
PROCEDURE Switch
{
- Put list[m] into temp
- Put list[Record] into list[m]
- Put temp into list[Record]
}
Once this works, the rest is simple - The descending one is pretty much the same but reversing the condition, and then all that is needed is to add a keyboard input how you want
Dunno whether it helps, but I've had a stab at writing this program in C. Although it may be a little daunting, there is usually a lot of similarities between programs so should be able to pick up the jist of it
Code:
list[5] = 3, 4, 2, 1, 9
void switch (int m, int record);
{
int temp;
temp = list[m];
list[m] = list[record];
list[record] = temp;
}
void ascending_search (int m)
{
int record = m;
for n = m ; n < 5 ; n++)
{
if list[n] > list[record]
{
record = n;
}
}
switch(m, record);
}
void descending_search (int m)
{
int record = m;
for n = m ; n < 5 ; n++)
{
if list[n] < list[record]
{
record = n;
}
}
switch(m, record);
}
void main()
{
int k;
char key;
getch(key);
if (key = 'a')
for (k = 1 ; k < 5 ; k++)
{
ascending_search(k);
}
if (key = 'd')
for (k = 1 ; k < 5 ; k++)
{
descending_search(k);
}
}
Hope that helps
Bookmarks