*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof.
Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions:
Here is the content of the file data.txt.
9
8
4
7
2
9
5
6
1
3
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdlib>
using namespace std;
//function that reads the number of integers and those
integers
//from data.txt and stores in arr passed to it.
//there is a mistake in passing *arr , it only modifies the arr
variable
//in function once the function is executed no modifications are
done to arr variable
//that's why double pointer is used and in main function address of
arr variable of type int * is passed.
//see the main code for readData function call.
int readData(int **arr)
{
int len = 0;
//open file
ifstream inp("data.txt");
string line;
int num = 0;
//get first line from data.txt
getline(inp,line);
//pass it to stringstream object
stringstream s(line);
//copy the integer type value in stringstream to
num
s >> num;
//if num is 0, or instead of number sum text is there
in data.txt
//stringsream assigns nothing and 0 will be still
there in num var
//at that time prompt the user and exit.
if(num == 0)
{
cout<<"\nWarning: Number of
Integers specified as 0 or not a number in data.txt.\n";
cout<<"Not reading
file\n";
inp.close();
return 0;
}
//create memory for array of size num(which is first
line value in data.txt)
*arr = new int[num];
//till file is read completly
while(getline(inp,line))
{
stringstream ss(line);
//here stringsream is used it will
automatically
//assigns the read string value
into passed datatype variable
//here num variable of type int is
passed. so stringstream will convert
//the string into integer.
//if it founds other than integer
that is stored in it then
//it will assigns nothing.that's
why for every wrong num in file
//0 is inserted to achieve this
after completion of while block statements
//each time num is set to 0.
ss >> num;
//assign read num into len_th
position of array .
//since arr var is double pointer
use * to get the value in it(base address of type int *arr
passed).
*((*arr)+len) = num;
//increment length
len++;
//increment position
//num is set to 0 , to achieve
invalid number in file are set to 0.
num = 0;
}
inp.close();
//return the length
return len;
}
//function that prints the array contents
void writeToConsole(int *arr,int last)
{
cout<<"\n------- Array Elements
--------\n\n";
//from start element to last element print the array
elements
for(int i =0;i<last;i++)
{
cout<<*(arr+i);
if(i<last -1)
{
cout<< " ,
";
}
}
cout<<"\n";
}
//function that sorts the array in ascending order
void bsort(int *arr,int last)
{
int i,j,temp;
//i -> 0 to last
for(i = 0;i<last;i++)
{
//-> j = 0 to last - j - 1
for(j = 0;j<last-i-1;j++)
{
//swap arr[j]
with arr[j+1] if arr[j] is greater than arr[j+1]
//below is the
pointer notation for accessing array values at position j.
if(*(arr + j)
> *(arr+j+1))
{
temp = *(arr + j);
*(arr+j) = *(arr+j+1);
*(arr+j+1) = temp;
}
}
}
}
int main()
{
//intially set arr to null
int *arr = NULL;
//call readData() by passing arr address because we
want to
//assign memory and insert data.
int len = readData(&arr);
//if number of integers read are 0 then exit.
if(len == 0)
{
cout<<"Due to error . No
Integers are read in array. Qutting...";
exit(1);
}
cout<<"\n------ Before Sort -----\n";
//if not print the integers before sort
writeToConsole(arr,len);
//call bsort() to sort the array
bsort(arr,len);
cout<<"\n------ After Sort -----\n";
//finally print the sorted arry to console.
writeToConsole(arr,len);
return 0;
}
/*
* ----- data.txt -------
10
9
23
2
24
12
10
5
7
6
7
*/
/*
* ----------- OUTPUT -----------
*
------ Before Sort -----
------- Array Elements --------
9 , 23 , 2 , 24 , 12 , 10 , 5 , 7 , 6 , 7
------ After Sort -----
------- Array Elements --------
2 , 5 , 6 , 7 , 7 , 9 , 10 , 12 , 23 , 24
------------------
(program exited with code: 0)
Press any key to continue . . .
*/
Get Answers For Free
Most questions answered within 1 hours.