Ok here is my assignment, i know its alot to read, but i would really appreciate the help:
Write a program that asks the user for a file name. The file contains the part number, cost and quantity for up to 50 parts in the current inventory at a store.
Your program should:
• Read the data into parallel arrays. Use integer arrays for the part number and quantity and a float or double array for the cost.
• Calculate the value of each item. Use another float or double array to store the value. This array will be in parallel with the other 3.
• Display the following information for the value array:
o The lowest number in the array
o The highest number in the array
o The total of the numbers in the array
o The average of the numbers in the array
o The array’s contents sorted in ascending order (use the bubble sort)
o The median value of the array. The median is the middle value, when the data is sorted, or the average of the 2 middle values when there are an even number of values.
• For extra credit, you may dual sort the part number and value arrays (modify the dual sort function in the book to use the bubble sort algorithm and work in ascending order) and display both the part number and value for the items shown above.
You should have separate functions to read the data, perform each calculation shown above, and print the results. In your read function, be sure to count how many numbers are actually read into the array. The rest of your functions should use this count to process the array’s contents.
Write your results to the file results.txt. Your output should be nicely formatted and easy to read.
And here is what my coding looks like so far:
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void sortArray( int[], int);
int main()
{
const int ARRAY_SIZE = 10;
int numbers[ARRAY_SIZE];
int count, total, median, max, min, sum, size;
ifstream inputFile;
ofstream outputFile;
// opens the file containing inventory data
inputFile.open("parts.txt");
for (count = 0; count < ARRAY_SIZE; count++)
inputFile >> numbers[count];
// closes the file
inputFile.close();
sortArray( numbers, 10);
}
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size-1); count++)
{
if( array[count] > array[count+1])
{
temp = array[count];
array[count] = array[count+1];
array[count+1] = temp;
swap = true;
}
}
} while (swap);
}
// Sorts the array and determines the median value.
double median( int x[], int size )
{
// If the size is odd the median is x [size / 2 ]
// If the size is even the median is the average
// of the two middle elements x[(size - 1)/2] and x[size/2]
if (size % 2 != 0) // odd size
{
return x [ size / 2];
else if
{
return ( x [ (size – 1) / 2] + x [ size / 2] ) / 2.0 ;
}
}
}
// min and max
for ( int i = 1; i < arraySize; i++ )
if ( a[ i ] > max )
max = a [ i ];
else if ( a [ i ] < min )
min = a [ i ];
// total
for ( int i = 0; i < arraySize; i++ )
total += a[ i ];
// compute the mean
double mean( const int x[], int arraySize )
{
int sum = 0;
// sum the array values
for ( int i = 0; i < arraySize; i++ )
sum += x[ i ];
return static_cast< double >( sum ) / arraySize;
}
// Open the file that the array will be written to.
outputFile.open("results.txt");
// Writes the array into the output file.
for (count = 0; count < ARRAY_SIZE; count++)
outputFile << numbers[count] << endl;
// Closes the output file.
outputFile.close();
}
Any corrections or help would be great!! Thanks!!