There is a problem my professor assigned. It's not suppose to be complicated but I can't grasp the concept. I can grasp it in the english language but if I turn it into Pascal, I have no idea what to do.

Here's the problem and what I have created in pascal so far.

Write a program that reads in four words and displays them in increasing alphabetical sequence and also in decreasing alphabetical sequence. My professor also added to make the program to any number of inputs. (Great hahah)

program alpha;

 word1, word2, word3, word4, Temp : string;

 Writeln ('Enter in the 1st word');
 Readln (word1);
 Writeln ('Enter in the 2nd word');
 Readln (word2);
 Writeln ('Enter in the 3rd word');
 Readln (word3);
 Writeln ('Enter in the 4th word');
 Readln (word4);

 If word1 > word2 then
   Temp := word1;
   word1 := word2;
   word2 := Temp;
   Writeln (word1, word2)

 If word3 > word4 then
   Temp := word3;
   word3 := word4;
   word4 := Temp;
   Writeln (word3, word4)

So you see, I can sort out word1 and word2 fine and word3 and word4. I don't know what statement order to put next to sort out these 4 words and then go on to sort any number of words. :confused: I was thinking of adding a case statement to ask the user in the beginning if he wants to end this program, to type in a letter or string.

My professor just got assigned to teaching this class2 days before it opened since most of us need it to graduate. He is new and doesn't have anything prepared. He goes along with the book as we do and is basically on the same level as us. I just don't know add to this. The book doesn't go into any details into this problem. Arrays are in the later chapters and I he doesnt want us to use that just what we have learned so far.

Any help would be appreciative. This is due today and I have been working on this for over a week so I am desperately trying to find a way to complete this.



11 Years
Discussion Span
Last Post by Stoney


i understood your problem. they are few ways to solve your sorting problem. first you understand what is sorting how does it works? ok.

i will give one type of solving method. you can develop from there ok.

example you have 4 numbers like a,b,c,d are in random position. now we want to keep them in ascending order right..

assume like d>c>b>a ok..our aim is print like a<b<c<d

present they are in random position like b,c,a,d ok

first you check first two variable (b,c) here b<c case is ok leave it (b,c) ; present order is b,c,a,d --ok
next you check it 2nd and 3rd variables (c,a) here c>a swap them (a,c); present order is b,a,c,d --ok
next you chech 3rd and 4th variable(c,d) here c<d is ok leave it; present order is ; b,a,c,d

now you repeat the loop
take first two variables (b,a) here b>a swap it (a,b)- present order is a,b,c,d --ok

here it is completed in two loops. some times it take more than this. how have to findout how many iterations are needed to solve it. by using this method we can keep them in ascending or decending order.

answer to your 2nd question:
if you want to use this logic for n number of word. use the arrays concept. your problem is solved.



Oh thanks very much. Since we just got done reading the next chapter, we are going to do arrays with this. I don't know why the teacher let us sweat for a few weeks trying to make us figure this out.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.