Introduction In today’s lab, we will read in a file using C++ and then perform a
ID: 3870508 • Letter: I
Question
Introduction
In today’s 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
Explanation / Answer
number.txt
3
6
2
______________
File.h
#include <string>
using namespace std;
int* readFile(string filename,int& len);
int* bubble_sort(int arr[],int length);
void display(int arr[],int length,string outFile);
_________________
File.cpp
#include <fstream>
#include <iostream>
#include <cstring>
#include "File.h"
using namespace std;
int* readFile(string filename,int& count)
{
//defines an input stream for the data file
ifstream dataIn;
int num,i=0;
//Opening the input file
dataIn.open(filename.c_str());
int* arr;
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
exit(1);
}
else
{
while(dataIn>>num)
{
count++;
}
dataIn.close();
// Creating array dynamically
arr = new int[count];
//Opening the input file
dataIn.open(filename.c_str());
while(dataIn>>num)
{
arr[i]=num;
i++;
}
dataIn.close();
}
return arr;
}
int* bubble_sort(int arr[],int length )
{
//This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < length; i++)
{
for (int j = 1; j < (length-i); j++)
{
if (arr[j-1] > arr[j])
{
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
void display(int arr[],int len,string outFile)
{
//Defines an output stream for the data file
ofstream dataOut;
//creating and Opening the output file
dataOut.open(outFile.c_str());
for(int i=0;i<len;i++)
{
dataOut<<arr[i]<<" ";
}
}
__________________
Main.cpp
#include<iostream>
#include "File.h"
using namespace std;
int main(int argc, char* argv[])
{
string inFile,outFile;
// inFile=argv[0];
// outFile=argv[1];
cout<<"Files names :";
cin>>inFile;
cin>>outFile;
int length=0;
int* arr;
arr=readFile(inFile,length);
arr=bubble_sort(arr,length);
display(arr,length,outFile);
return 0;
}
____________________I Will add the output also
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.