Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need to fix this error i need to figure out how to make the program not count

ID: 3708569 • Letter: I

Question

i need to fix this error

i need to figure out how to make the program not count the same numbers (there are two 12's in the array and now the program sees it as two different nums)

=-----

#include<iostream>

#include<fstream> // to input / output files

#include<iomanip> // for setprecision()

using namespace std;

/**

* A boolean function to validate whether a number is in range 0 to 110 or not

*/

bool validateNumber(double num)

{

if(num < 0 || num > 110)

{

return false;

}

return true;

}

/**

* Function to read the file and assign the number read to the variable num as reference

*/

int readFile(ifstream &inF, double &num)

{

inF >> num;

if(!validateNumber(num))

return -1;

else if(validateNumber(num))

return 1;

else

return 0;

}

/**

* Function to write the read invalid numbers to the output file

*/

void write2File(ofstream &outF, double invalidNum)

{

outF << invalidNum << " ";

}

// main function

int main(void)

{

string filename;

ifstream inF;


cin >> filename; // reading the input file name from user

// opening the input file

inF.open(filename.c_str());

if(!inF) // checking whether the input file was opened or not

{

cout << "File " << """<<filename<<"""<<" could not be opened" << endl;

return -1;

}

// opening the output file

ofstream outF;

outF.open("invalid-numbers.txt");

int validNumbersCount = 0;

int invalidNumbersCount = 0;

int totalNumbersCount = 0;

double validNumbersSum = 0;

cout << "Reading from file " << """ << filename << """ << endl;

outF << fixed << setprecision(2); // setting the fixed precesion to 2 decimal places after decimal into the output file

while(!inF.eof()) // looping untill end of file

{

double num;

int returnVal = readFile(inF, num);

if(returnVal == -1)

{

invalidNumbersCount++;

write2File(outF, num);

}

else if(returnVal == 1)

{

validNumbersCount++;

validNumbersSum += num;

}

totalNumbersCount++;

}

// closing the files

inF.close();

outF.close();

double avg = validNumbersSum / validNumbersCount;

cout << "Total values: " << totalNumbersCount << endl;

cout << "Invalid values: " << invalidNumbersCount << endl;

cout << "Valid values: " << validNumbersCount << endl;

if(validNumbersCount > 0) // if valid numbers count is greater than 0 then print average

{

cout << "Average of valid values: " <<fixed << setprecision(2) << avg << endl;

}

else // else print the error message

cout << "An average cannot be calculated" << endl;

}

zy 5.18. Ldb Lesson 9 (Par x 1 e Search Textbook Solutior x ? secure https://learn.zybooks.com/zybook/UTDALLASCS 11 36Spring2018LabB/Chapter/5/section/18 . ::: Apps D Payments ? Exar'' P: Probabilily! - ?? ? zyBooks y lbxrary CS 1135 home 1 Lsb Lesson9 Part 2 ot 2 ez,000ks catalog @repro ? Hijnsen CrS 4: Compare outputA Output differs. See highlights below. Input ni.Lx Rosaing Ezea fi1"uera2.tut or output vlad valu Re 01?? tron ±ile "nurrersz.txt.' Expected outut Iovalid vales 0/5 cope ouiput Output differs. See highlights below. Input nuricers txt Reading fron f:l "nunhersa.txt" Your output Ilis sle Iotsl values 6: Compare output A 1:14 PM Type here to search 4/15/2018

Explanation / Answer

The data is being read from a file. If the number gets repeated in a file
the it should not be counted more than once. If this is the requirement
then the numbers read must be stored in an array and before updating the
counters we need to check if the read number is in the array or not.If it is
in th array count should not be incremented and if it is not there , it should
be added to the array and counts should be incremented.

Following code can be used in the main

int data[1000]; //1000 is just a large number
int count = 0;

inside the while(!inF.eof()) // looping untill end of file
// after the line int returnVal = readFile(inF, num);

int found = 0;
for (int i = 0; i<count; i++){
     if (data[i] == num)
        found = 1;
}
if (found == 1)
     continue;
else {
     data[count] = num;
     count++;
}