Stuck on array sort project!
Dec 12, 2013 at 5:00am UTC
Okay, what this project is supposed to do is take 6 numbers(money) from an input file, display the numbers, sort them in ascending order, then display the average. This is what I have so far, and right now when I run it I get a logistical error alert. Any help you could give me would be much appreciated! :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void sortArray(double array[], double size);
double Averagenum();
int main()
{
const int array_size = 6;
double numbers[array_size], averageNum;
int count = 0;
ifstream inputFile;
inputFile.open("nubmers.txt" );
while (count < array_size && inputFile >> numbers[count])
count ++;
inputFile.close();
cout << fixed << showpoint << setprecision(2);
cout << "The numbers are: " ;
for (count =0; count < array_size; count++)
cout << numbers[count] << " " ;
cout << endl;
cout << "The sorted numbers are : " << sortArray << "\n" ;
cout << "The average of the numbers is: " << averageNum << "\n" ;
return 0;
system("pause" );
}
void sortArray(double array[], double size)
{
int startScan;
double minIndex, minValue;
ifstream inputFile;
inputFile.open("nubmers.cpp" );
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex=startScan;
minValue = array[startScan];
for (int index = startScan +1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
inputFile.close();
}
}
}
}
double Averagenum()
{
ifstream inputFile;
double nums, sum, average;
int count;
inputFile.open("nubmers.cpp" );
inputFile >> nums;
while ( count = 0, count < 6, count++)
{
sum = sum + nums;
count++;
}
average = sum / 6;
return average;
}
Dec 12, 2013 at 9:26am UTC
you are using your averageNum and sum variables without initializing them
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void sortArray(double array[], const int size);
int main()
{
const int array_size = 6;
double numbers[array_size], averageNum;
int count = 0;
ifstream inputFile;
inputFile.open("nubmers.txt" );
if (!inputFile)
{
std::cout << "Input file failed to open " ;
std::cin.ignore();
exit(1);
}
while (count < array_size && inputFile >> numbers[count])
count++;
inputFile.close();
cout << fixed << showpoint << setprecision(2);
cout << "The numbers are: " ;
for (count = 0; count < array_size; count++)
cout << numbers[count] << " " ;
cout << endl;
sortArray(numbers, array_size);
cout << "The sorted numbers are : " << "\n" ;
for (int i = 0; i < array_size; i++)
{
cout << numbers[i] << " " ;
}
cout << endl;
cin.ignore();
return 0;
system("pause" );
}
void sortArray(double array[], const int size)
{
double temp;
for (int i = size - 1; i >= 0; i--)
{
for (int j = size - 1; j > 0; j--)
{
if (array[j] < array[j - 1])
{
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
}
i'll leave the Averagenum function for you
Last edited on Dec 12, 2013 at 9:30am UTC
Topic archived. No new replies allowed.