Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction In todays lab, we will read in a file using C++ and then perform a basic Bubble Sort. Lab Objectives C++ File I/O Reading

Introduction

In todays lab, we will read in a file using C++ and then perform a basic Bubble Sort.

Lab Objectives

C++ File I/O

Reading a file with no length marker

Write a bubble sort routine

Prior to Lab

Make sure you can read in and write out to files in C++. This involves using C++ style streams.

Lab Instructions Create two files, lab5.cpp and lab5.h (NOTE: .hpp is also a valid header extension, and could be

useful for distinguishing C headers and CPP headers).

You will be given an input file of variable length that contains integers. Your program should take 2 command line arguments, the input file of integers and the name of an output file where the sorted list of integers will be written.

You may only use C++ style file I/O to manipulate data. You may not use printf or scanf in your program. Additionally, if you use the C++ sort() function, you will receive a zero

Your Objectives

Read through the input file and determine how many integers you need to store

Read through the file a second storing the integers in an array

Pass this array to a bubble sort function, sorting the integers in ascending order

Write the sorted integers to the output file, one integer per line

For example, given the input file number.txt: 3 6 2

Your program should produce an output file:

2 3 6

The basic pseudo-code for this algorithm is as follows:

bubble_sort( array, length ) { while ( number_swaps != 0 ) {

number_swaps = 0 for ( index, index < length - 1, index++ ) {

if array[index] > array[index+1]swap; number_swaps++ }

}}

We keep looping through the array until we do not perform any swaps. At the end of the algorithm, the numbers are sorted from left to right, smallest to largest.

Write a function that implements this algorithm. It should not have any hard coded values!

Header Files and Header Guard Your bubble sort function should have its prototype defined in the lab5.h file. At this point, we

should be following best practice for maintaining our source code, and using header guards. What to turn in

lab5.cpp which reads in a file of integers to an array, sorts them, and writes the sorted list out

lab5.h which has the prototype for your bubble sort function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

=+How might it impede communication? [LO-5]

Answered: 1 week ago