No subject

Zahid Ahmadzai zahidahmadzai at gmail.com
Mon Feb 5 11:07:19 EST 2007


HI THERE

I NEED HELP WITH THE FOLLOWING EXERSISE CAN YOU PLEASE HELP IF YOU CAN.

PLEASE SEND ME THE CODE ON E-MAIL

MANY THANKS







For presentation to your tutor during your scheduled tutorial in the week
commencing 12 February.

I decided that it might be a good idea to create a suitable program to test
sorting algorithms using simple integer data. Although our student record
program is realistic in terms of the data it uses, integers have the
advantage that they can be generated automatically (using the
*rand()*function to return a random integer). To use
*rand()* you need to *#include <cstdlib>*.

An outline design for a program would be much the same as the initial design
for the student record program:

   - get the data
   - sort the data
   - display the results

In this case the data will be 'generated' using the random number function
(instead of reading data from a file). We need to clarify a little bit how
the user interface would appear. As far as I am concerned, I have decide
that the program will run interactively (keyboard input and display output)
as follows:
How many numbers to sort? 50
Numbers generated ...
41 467 334 500 169 724 478 358 962 464 705 145 281 827 961 491 995 942 827
436 391 604 902 153 292 382 421 716 718 895 447 726 771 538 869 912 667 299
35 894 703 811 322 333 673 664 141 711 253 868

After sorting ...
41 141 145 153 169 253 281 292 299 322 333 334 358 382 391 421 436 447 464
467 478 491 500 538 604 664 667 673 703 705 711 716 718 724 726 771 811 827
827 868 869 894 895 902 912 942 961 962 995

At the moment this only uses the bubble sort (as we have in the notes) and I
have arbitrarily decided to generate random numbers between 0 and 999. The
constant field width is obtained by using the *cout.width(4)* function call.


Using the following data type and prototypes:

typedef int intArray[];

void generate(intArray nums, int size, int low, int high);
//generates size random ints between low and high inclusive

void bubSort(intArray nums, int size);

void displayNums(intArray nums, int size);
//displays numbers nicely laid out
// in this case - of width 4 digits and 15 to a line

void display(int n);
//displays a single integer in a fieldwidth of 4 chars

bool notInOrder(int a, int b);
//returns true if a and b need swapping

and a *main()* function which has the following design:
void main()
{
   const int MAX_SIZE = 5000; // arbitrary
   int numbers[ MAX_SIZE ];
   int howMany;
   cout << "How many numbers to sort? " ;
   cin >> howMany;
   if ( howMany > MAX_SIZE )
      { howMany = MAX_SIZE; }
   generate( numbers, howMany, 0, 999 );
   cout << "Numbers generated..." << endl;
   displayNums( numbers, howMany );
   bubSort( numbers, howMany );
   cout << "After sorting..." << endl;
   displayNums( numbers, howMany );
}

Complete the program so that it behaves much as shown above.

Notes:

The bubble sort code is given in the notes (use the best version) - you need
to change it (very slightly) to work with an array of integers instead of an
array of students.

The function *generate()* is a bit tricky. Think about how to start with a
simple *stub* (i.e. don't worry initially about implementing the whole
functionality - just make some numbers using simple code that can be sorted.


The function *displayNums()* can also initially be implemented in a crude
form (i.e. a *stub*) and improved later.

Think carefully about how you test *generate()*.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070205/8738075b/attachment.html>


More information about the Python-list mailing list