C Programming Array storage sorting and handling partially f
C+ Programming
Array storage, sorting, and handling partially filled arrays
1. Create a program that reads in a series of sales dollar amounts from a file, stores the amounts in an array, processes the array, and outputs to a file and the console the number of sales amounts, the lowest amount, the highest amount, and a complete list of the amounts in order from lowest to highest. Sample output shown below.
2. The maximum number of amounts in the file is 20, but it could have less. The program must keep track of the number of amounts read in from the file, and stop at the sentinel.
3. Required functions: (1) opening the files and file failure conditions, (2) reading the input and storing the values, (3) sorting the array, (4) writing and displaying the output to file and screen.
4. Create an input file named “salesAmounts.txt” with the following values in the order that they appear below vertically in the file. Zero marks the end of the data set and is not one of the amounts. The file location must be the project folder. 29.95, 3.55, 34.70, 12.34, 2.67, 33.89, 24.99, 18.45, 4.99, 9.95, 0.00, and 5
Recommendation: Use bubble sort in your solution (Code Below)
Sample Input File
Sample Output
void bubblesort (int arr], int length) // Bubble largest number toward the right for (int i = length-1; i > 0; i--) for Cint j 0; j for (int j 0; jSolution
//Sales Amonts Programme
#include <iostream> //input output stream
#include <fstream> //file function header file
using namespace std;
//Bubble Sort
void bubble_sort (float arr[], int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - i - 1; ++j)
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
int main(int argc, char * argv[]) //main program starts here
{
ofstream outfile;
std::fstream myfile(\"salesAmounts.txt\", std::ios_base::in); //open file salesAmounts.txt to read data
float sales[20] = { };
float a;
int i=0,length;
//
while (myfile >> a) //read from file and store data into salses array
{
sales[i]=a;
printf(\"%f \ \", sales[i]); //command dispaly readed data
i++;
}
length=i;
float min=sales[0];
float max=sales[0];
for(int j=1;j<length;j++) //loop to fin max,min values by comparing with remaining elements
{
if(min>sales[j])
min=sales[j];
else
max=sales[j];
}
bubble_sort (sales, length); //function calling buuble sort by passing array & its length
// Writing output data to File
outfile.open (\"example1.txt\"); //open text file to write data
outfile << \"The number of sales amounts in this file : \" <<length << \"\ \"; //number of sales
outfile << \"Smallest sales amount : \" <<min << \"\ \"; //smallest amont
outfile << \"Largest sales amount : \" <<max << \"\ \"; //largest amount
outfile << \"The list of amounts sorted from lowset to highest : \ \"; //display amonts in sorted order using loop
for (int x = 0; x < length; ++x)
outfile <<\"$\"<<sales[x] << \"\ \";
outfile.close(); //close of output file
getchar();
return 0;
}
Output :
Ouput created in exampl1.txt ,if you open file the saple output like shown below
The number of sales amounts in this file : 8
Smallest sales amount : 1.2
Largest sales amount : 67.3
The list of amounts sorted from lowset to highest :
$1.2
$1.5
$6.7
$9
$34
$34.5
$56
$67.3

