Assignment: First, you should use Notepad (or any other text editor) to create a
ID: 3547706 • Letter: A
Question
Assignment: First, you should use Notepad (or any other text editor) to create a file ahead of time. The file should have 6 double values on separate lines. These values represent the earnings of a person for each of the first 6 months in a year; thus each double value will have exactly two places to the right of the decimal. Name this file, "earnings.txt", and save it to your desktop.
Your program should open and then read the 6 double values from this file into an array named 'earnings'. Use a 'for' loop to do this. Then use another 'for' loop to display the earnings as they were in the file.
After this, sort the array , and then display the earnings in sorted order.
Finally, compute and display the average of the earnings.
Explanation / Answer
#include #include using namespace std; int main () { double earnings[6]; double sum (0.0), average, temp; int i = 0; ifstream myfile ("earnings.txt"); if (! myfile.is_open()) { cout > earnings[i]; // a 1-to-6 for loop with if(myfile.good() inside i++; } /* I assume you may need the earnings array for something else, and make a copy. If you don't need to do anything else, you can use earnings instead of the copy array I create */ double *copy = earnings; // Now to sort - a bubble sort is simple for (int j = 5; j >= 0; j--) { for (int k = 0; k copy[j]) { temp = copy[j]; copy[j] = copy[k]; copy[k] = temp; } } } for (int m = 0; m < 6; m++) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.