A SET OF PSEUDOCODE PROBLEMS (rev 9.97)

Here, the idea is that you learn programming from seeing/trying lots of small problems, and possibly re-using these bits in other new problems. Solutions are available, but try the problems before looking at the answers!

The pseudocode that you use may depend on your course: for exampme. some people use while/endwhile, and some use while/wend. Some use = and <> to mean equal and not equal, whereas some use = = and != Some use input and output, some use read/print , some use accept/display. It doesn't really matter - pseudocode is more concerned with the logic - the control flow issues.

-------

Write pseudocode to ...

1.1 Input the dimensions of a rectangle and print its area.

1.2 Input the dimensions of a rectangle and print area and perimeter.

 

 

1.3 Input the length of the side of a square, and print its area. Produce an error message if the length is negative. (i.e.validation)

1.4 Input 3 numbers and print either an "all 3 equal" or a "not all equal" message.

 

1.5 Input 3 numbers, and print the biggest.

2.1 Print the numbers 1 to 100 inclusive- with a while loop.

2.2 Print the numbers 1 to 100 inclusive with a for loop.

2.3 Input 2 values into the variables: start and finish,

then print the integers from start to finish inclusive. (use for)

2.4 Input 2 values into the variables: start and finish,

then print the integers from start to finish inclusive. (use for)

BUT, if start is bigger than finish, don't print the numbers: an error message instead!

2.5 Input 2 values into the variables: start and finish, then print the integers from start to finish inclusive. (use for)

BUT if start is bigger than finish, swap over their values so they are now in the proper order, then print the integers as specified.

2.6 Input 10 numbers, and print their total.

 

2.7 Input 10 numbers, and print their total - but any numbers>100 should be ignored, i.e should not be summed.

2.8 Input a count, which specifies how many numbers will follow it. Print the total of the following numbers.

(eg the input might be: 4 33 52 67 83 - where the count is 4)

2.9 Input a series of positive (>=0) numbers, ended by a negative one. Add up the numbers, and print the total. The negative one is not to form part of the sum.

2.10 Input 100 positive (>=0) numbers. Add up the numbers, and print the total. If a negative number is encountered mid-way, the program should terminate, and print the sum so far.

2.11 Using nested fors, print an hours and minutes table, of the form:

0 0

0 1

0 2

0 3

...

0 59

1 0

1 1

1 2

...

as far as 11 hours 59 mins.

 

Files

Assume the following file pseudocode:

open "fred" for input (reading)

open "june" for output (writing)

read data items from "fred" (you may read the whole line as a string, or read it into separate variables - it depends on the problem)

write data items to "june"

close "fred"

test for end of file, eg:

while not end of file

etc

 

3.1 Display each line of file "fred" on the screen.

3.2 Read every line in "fred", and write them to file "june"

3.3 Count the number of lines in "fred"

In the following, assume that each line (record) contains a persons name and age

3.4 Display the ages of everyone called "smith"

3.5 Display the names of everyone who is over 40.

3.6 write the names of everyone who is over 40 onto a new file.

3.7 Write the names of everyone who is over 40 to one file, and the names of everyone <= 40 to another file.

Arrays

The following assume 2 integer arrays, a and b, with elements a[1] to a[100],and b[1] to b[100]

Note that in e.g C++, arrays are numbered from 0, - but that is a minor problem. Imagine that we choose to avoid using element number 0. In VB, you will use ( ) for arrays - in pseudocode, it doesn't really matter.

4.1 Set every element of b to 0

4.2 Set a[1] to 1, a[2] to 4, a[3] to 9 etc

 

 

4.3 Read in 100 numbers, and store them in a. Display them on the screen, then in reverse order.

 

4.4 Read in a count, then read in the data values, storing them in a.

eg- the data could be: 4, 55,1232,786,456

If the count is above 100, the program should terminate without reading any numbers.

 

4.5 Read a series of positive numbers into a. The numbers are ended by a negative one, which is not to be stored. (Assume there is not more than 100 numbers)

4.6 Assume that 100 numbers have already been stored in a.

Copy each one from the array a into the array b.

 

4.7 Assume that 100 numbers have already been stored in a.

Find the biggest value in a.

 

4.8 Assume that 100 numbers have already been stored in a.

Find the position of the biggest value.

4.9 Assume that 100 numbers have already been stored in a.

Search the array a for the value 9876. Either print its position, or print a 'not found' message.

 

4.10 Read 100 numbers into a, then print the sequence in reverse order.

(eg- 123 43 -23 47 ... 667 is printed as:

667 ... 47 -23 43 123 )

4.11 Assume that 100 numbers have already been stored in a.

Input 2 numbers ( position numbers in the range 1 to 100) then print the values stored in the elements between the two positions. (eg an input of 3 7 should cause the printing of the contents of a[3], a[4], a[5], a[6], a[7] )

4.12 Input 2 values - then look at the value stored in each element of the array (100 of them) and print each value that falls between the 2 input values

 

4.13 100 students sit a 'before' exam, and their marks can be assumed to be in the array b. Later, they do an 'after' exam, and those marks are in a. Print the subscript number (i.e in range 1 to 100) of the student who:

a) got the lowest mark overall (before + after)

b) improved the most between b and a.

 

-----------------------------------------------------------------------------------------------