Question

can someone help me fix this. The function that finds the minimum and maximum values of...

can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array.

#include <iostream>
#include <fstream>
#include<iomanip>

using namespace std;
void readArray(ifstream& inF, int arr[], int&ArrSize);
void writeArray(ofstream & outF, int arr[], int & ArrSize);
void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low);
int main()
{
   int arr[100];
   int i = 0;
   int size = 0;
   int high = 0, low = 0;
   ifstream inFile;
   inFile.open("integers.txt");
   ofstream outFile;
   outFile.open("arresults.txt");
   readArray(inFile, arr, size);
   writeArray(outFile, arr, size);
   MaxAndMin(outFile, arr, size, high, low);
   return 0;
}


void readArray(ifstream & inF, int arr[], int & ArrSize)
{

   int x;
   while (!inF.eof())
   {
       inF >> x;
       arr[ArrSize] = x;
       ArrSize++;
   }
   cout << "\nArray elements: \n";
   for (int i = 0; i < ArrSize; i++)
   {
       cout << arr[i] << endl;
   }
   // Close input file
   inF.close();
}


void writeArray(ofstream & outF, int arr[], int & ArrSize)
{

   //outputs 10 integers per line
   for (int i = 0; i <= ArrSize; i++)
   {
       outF << arr[i] << " ";
       if ((i) % 10 == 9)
           outF << endl;
   }
}


void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low)
{
   int i;
   //finds index of smallest and largest value in array//
   for (i = 0; i <= ArrSize; i++)
   {
       if (arr[i] > arr[high])
           high = arr[i];
       else
           if (arr[i]<arr[low])
           low = arr[i];
   }
   outF << "The highest value is " << high << " at index " <<arr[high] << endl;
   outF << "The lowest value is " << low<< " at index " << arr[low] << endl;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include<iomanip>

using namespace std;
void readArray(ifstream& inF, int arr[], int&ArrSize);
void writeArray(ofstream & outF, int arr[], int & ArrSize);
void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low);
int main()
{
int arr[100];
int size = 0;
int high = 0, low = 0;
ifstream inFile;
inFile.open("integers.txt");
ofstream outFile;
outFile.open("arresults.txt");
readArray(inFile, arr, size);
writeArray(outFile, arr, size);
MaxAndMin(outFile, arr, size, high, low);
return 0;
}


void readArray(ifstream & inF, int arr[], int & ArrSize)
{

int x;
while (inF >> x)
{
arr[ArrSize] = x;
ArrSize++;
}
cout << "\nArray elements: \n";
for (int i = 0; i < ArrSize; i++)
{
cout << arr[i] << endl;
}
// Close input file
inF.close();
}


void writeArray(ofstream & outF, int arr[], int & ArrSize)
{

//outputs 10 integers per line
for (int i = 0; i < ArrSize; i++)
{
outF << arr[i] << " ";
if ((i) % 10 == 9)
outF << endl;
}
}


void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low)
{
int i;
// Initialize highValue and lowValue to first element
int highValue = arr[0];
int lowValue = arr[0];
//finds index of smallest and largest value in array//
for (i = 1; i < ArrSize; i++)
{
// Check if highValue < value at index i
// If yes, set highValue to arr[i]
// Set index high = i
if (highValue<arr[i])
{
highValue = arr[i];
high = i;
}
// Check if lowValue > value at index i
// If yes, set lowValue to arr[i]
// Set index low = i
if (lowValue > arr[i])
{
lowValue = arr[i];
low = i;
}

}
outF << "The highest value is " << highValue << " at index " <<high << endl;
outF << "The lowest value is " << lowValue<< " at index " << low << endl;
}

OUTPUT-

Add a comment
Know the answer?
Add Answer to:
can someone help me fix this. The function that finds the minimum and maximum values of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help to fix this program it is written in c++ and needs to run...

    I need help to fix this program it is written in c++ and needs to run in visual studio. Program uses character strings from a file containing on the first line and answer key and every other line after is the students ID number followed by a space then their test answers.Program currently reads only test answer key and student ID and their answers of second line.  Sample data can be found below. TFTTFTFFFTTTFFTTFFFT HUB00123 TFTFFFTFTFTFTTTFTTTT SMU12456 FFTFTTFFTTTTTTFFTTFT #include #include #include...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • Hi! 1. I need some help with sorting string in a text file. My goal is...

    Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • Please help the out keeps printing twice #include #include #include #include #include using namespace std; /**...

    Please help the out keeps printing twice #include #include #include #include #include using namespace std; /** * Function to calculate the future value and return the same */ double calculateFutureValue(double presentValue, double interestRate, int months) {    double futureValue = (double)presentValue * pow((1 + interestRate), months);    return futureValue; } /** * Function to read the input file and assign the value to the variables */ unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months) {    inF >>...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT