PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
And your question or problem is?
I will have to dig though our code more to see if I can figure what your question might be,, but for not what jumps out at me is in main you create "myfile" as an output stream and then close it before you could use it, which you do not, so what is the point.
Write a program that reads salaries from a file, sorts them, determines and outputs the median salary. The median should be calculated and returned by the function getMedian that calls the selectionSort function. The median value is the middle value when an odd number of values are sorted, and the mean of the two middle values when an even number of values are sorted.
Consider the following salaries: 75000, 45000, 65000, 100000, 50000.
When sorted, the numbers are 45000, 50000, 65000, 75000, 100000.
The median is 65000 because two values are lower and two values are higher.
Now consider the following salaries: 75000, 45000, 65000, 100000, 50000, 80000.
When sorted, the numbers are 45000, 50000, 65000, 75000, 80000, 100000.
Since we have an even number of salaries (six), no salary has the same number of higher values and lower values. We therefore take the mean of the two middle values as the median. The two middle values are 65000 and 75000. The median is (65000+75000)/2 = 70000. Note that even if all the values are integers the median may not be.
The prototype for the median function should be similar to:
double getMedian(int array[], int);
The median function should call the selection sort function
void selectionSort (int array[], int size)
Made some changes and comments in your program, the following code, be sure to read the comments in the code.
After seeing the program requirements I would suggest you find out more about selection sort and finding the median then start over because what you have now does not work.
The " selectionSort" function causes an endless loop and I not even sure that it does any sorting. You like me need to find out more about selection sorting. If I am correct what I use most often is a bubble sort.
The function "getMedian" does not work the way it should and the function definition returns a double, but the function only returns zero. Not what you want. My comments in this function should help.
Take a look at the program requirements this way:
Write a program that reads salaries from a file,
sorts them,
determines and outputs the median salary.
The median should be calculated and returned by the function getMedian that calls the selectionSort function.
The median value is the middle value when an odd number of values are sorted,
and the mean of the two middle values when an even number of values are sorted.
Consider the following salaries: 75000, 45000, 65000, 100000, 50000.
When sorted, the numbers are 45000, 50000, 65000, 75000, 100000.
The median is 65000 because two values are lower and two values are higher.
Now consider the following salaries: 75000, 45000, 65000, 100000, 50000, 80000.
When sorted, the numbers are 45000, 50000, 65000, 75000, 80000, 100000.
Since we have an even number of salaries (six), no salary has the same number of higher values and lower
values. We therefore take the mean of the two middle values as the median. The two middle values are
65000 and 75000. The median is (65000+75000)/2 = 70000.
Note that even if all the values are integers the median may not be.
The prototype for the median function should be similar to:
double getMedian(int array[], int);
The median function should call the selection sort function
void selectionSort (int array[], int size)
This should help to understand what needs to be done. Also if you break this down into smaller parts it is easier to work on.
Slightly corrected code, but still does not work right:
#include <iostream>
#include <fstream>
//using namespace std;
void selectionSort(int A[], int S);
double getMedian(int A[], int S);
int main()
{
std::ofstream myfile("salary.text");
constint Size = 5;
int Array[] = { 75000, 45000, 65000, 100000, 50000 };
myfile.close();
selectionSort(Array, Size); // <--- Just need array name not "[]".
getMedian(Array, Size); // <--- Just need array name not "[]".
std::cin.get();
return 0; // <--- Not needed, but good programming.
}
void selectionSort(int A[], int S) // <--- Using Size here is easier to understand than "S". Since it is
// local scope it does not affect the original deffinition in main.
{
std::ifstream myfile("salary.text"); // <--- Not used what is the purpose? Need to check that the file
// opened.
{ // <--- Not needed.
for (int i = 0; i<(S - 1); i++)
{
int minelement = A[i]; // <--- What is this for?
int index = i; // <--- What is this for?
for (int j = (i = 1); j<S; j++)
{
if (minelement>A[j]) // <--- Not the best approach.
minelement = A[j];
int index = j; // <--- What is this for?
}
int sortedelements = A[i]; // <--- What is this for?
for (int i = 0; i<S; i++)
{
std::cout << "Sorted Emements: " << sortedelements << " " << std::endl; // <--- should be "S" not "sortedelements".
}
}
myfile.close();
} // <--- Not needed.
}
double getMedian(int A[], int S) // <--- Using Size here is easier to understand than "S". Since it is
// local scope it does not affect the original deffinition in main.
{
for (int i = 0; i<(S - 1); i++)
{
if (S % 2 == 0)
{
i = S / 2;
int median = (A[i] + A[i + 1]) / 2;
std::cout << "Medain: " << median << std::endl;
}
if (S % 2 != 0)
{
i = S / 2;
int median = A[i];
std::cout << "Medain: " << median << std::endl;
}
}
return 0; // <--- Needs to return "median" not zero.
}
When opening files I use this code. it is a little much and can be shortened, but good when first learning about using files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
std::string iFileName{ "" };
std::ifstream inFile;
inFile.open(iFileName);
if (inFile.is_open())
{
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1);
}
std::ios::trunc | std::ios::ate can later be changed to just std::ios::app to append the file. As is it clears the existing file if there is anything there. This helps when first working with the program.