CSE202Lab8

CSE202Lab8 - , which will represent the number of elements...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 202 Lab 8 - Sorting Introduction: The purpose of this lab assignment is to write a program that can sort an array of doubles in ascending order. For example, if you were presented with the array A[0] = 0.45 A[1] = -324.23 A[2] = 543 etc The sorted array would appear as: A[0] = -324.23 A[1] = 0.45 A[2] = 543 etc once sorted. To implement this, you are going to use a sorting algorithm described by this pseudo- code (this is not actual code, but a procedural description of an algorithm) : Algorithm 1: for(i = 0 to arraySize) { for(j = i to arraySize) { if(A[j] > A[i]) swap(A[i] and A[j]); // Line A } } Algorithm 1 loops through the entire array, comparing each element with the next, swapping them if necessary. Take some time to convince yourself that this algorithm will in fact leave the array in sorted order. Instructions: 1. Write a program that creates an array of doubles, using 50,000 elements. 2. Ask the user to input a value, arraySize
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: , which will represent the number of elements in the array to use. 3. Fill the array, from index to arraySize , with random numbers (for a refresher on how to do this, consult Lab 5, dont forget to include the necessary libraries!). 4. Output these numbers to the screen in un-sorted order. 5. Write an algorithm to sort these numbers, using Algorithm 1 as a guide. a. This step will involve you having to write a function that swaps two values (for use at Line A in Algorithm 1). Use the following function template: void swap(double& a, double& b) { // Create a temporary variable, tmp // Put a into tmp // Put b into a // Put tmp into b } This function is being passed two values by reference . That means, when you modify these arguments, you are modifying the values passed into the function directly. 6. Output these numbers to the screen in sorted order. 7. Turn in a printed copy of your source code with your name on it....
View Full Document

Page1 / 2

CSE202Lab8 - , which will represent the number of elements...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online