Question

Write a complete program that uses the functions listed below. Except for the printOdd function, main...

Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates.
•Write a function called sumOfSquares which takes as its parameter an input file. The function should return the total sum of all the squared numbers in the file up to 0.
•Write function called countMult5, that takes as its parameter an input file. The function should read two integers, start and end, to counts how many numbers between start and end are multiples of 5. Return the answer to the calling function.
•Write a function called printOdd that takes as its parameters an input file stream and output file stream. The function should print only the odd numbers in a file up to -999.
•Write a function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function.
•Define a function called calcAverage, that takes as its parameter an input file. The function should read the numbers from a file up to the sentinel value and calculates their average. Return the answer to the calling function.

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>

using namespace std;

int sumOfSquares(string filename);
int countMult5(string filename);
void printOdd(string filename,string outputfile);
int productEven(string filename);
double calcAverage(string filename);
int main() {
   //Declaring variables
   string filename,outputfile;
   int res;
  

cout<<"Enter the input file name :";
cin>>filename;

res=sumOfSquares(filename);
cout<<"Sum of Squares :"<<res<<endl;
  
cout<<"Enter the input file name :";
cin>>filename;
res=countMult5(filename);
cout<<"No of Multiples of 5 :"<<res<<endl;

cout<<"Enter the input file name :";
cin>>filename;
   cout<<"Enter the output file name :";
cin>>outputfile;
printOdd(filename,outputfile);
  
cout<<"Enter the input file name :";
cin>>filename;
res=productEven(filename);
cout<<"Product of Even numbers :"<<res<<endl;
cout<<"Enter the input file name :";
cin>>filename;
double avg=calcAverage(filename);
cout<<"Average of Numbers :"<,avg<<endl;
   return 0;
}

int sumOfSquares(string filename)
{
   ifstream dataIn;
   int num,sumSquares=0;
  
   dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **"<<endl;
   exit(0);
}
else
{
//Reading the data from the file
while(dataIn>>num)   
{
   if(num==0)
   break;
   else
   sumSquares+=num*num;
   }
dataIn.close();
return sumSquares;
}    
  
}

int countMult5(string filename)
{
       ifstream dataIn;
   int num1,num2,cnt=0;
  
   dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **"<<endl;
   exit(0);
}
else
{
//Reading the data from the file
dataIn>>num1>>num2;
for(int i=num1;i<=num2;i++)
{
   if(i%5==0)
   {
       cnt++;
       }
   }

dataIn.close();
return cnt;
}    
  
}
void printOdd(string filename,string outputfile)
{
           ifstream dataIn;
           ofstream dataOut;
   int num;
  
   dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **"<<endl;
   exit(0);
}
else
{
   dataOut.open(outputfile.c_str());
   while(dataIn>>num)
   {
      if(num!=-999)
      {
          if(num%2!=0)
          {
              dataOut<<num<<endl;
           }
       }
   }
   dataIn.close();
   dataOut.close();
}
}

int productEven(string filename)
{
        ifstream dataIn;

   int num1,num2,prod=1;
  
   dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **"<<endl;
   exit(0);
}
else
{

   dataIn>>num1>>num2;
     
      for(int i=num1;i<=num2;i++)
      {
          if(i%2==0)
          {
              prod*=i;
           }
       }
     
   dataIn.close();
   return prod;
}  
}
double calcAverage(string filename)
{
          ifstream dataIn;

   int num,sum=0,cnt=0;
  
   dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **"<<endl;
   exit(0);
}
else
{
while(dataIn>>num)
{
    if(num!=-999)
    {
        sum+=num;
        cnt++;
       }
       else
       break;
   }
  
   dataIn.close();
   return ((double)sum/cnt);
}  
}

========================================Thank You

Add a comment
Know the answer?
Add Answer to:
Write a complete program that uses the functions listed below. Except for the printOdd function, main...
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
  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • please write program in C++ Write a function called productEven, that takes as its parameter an...

    please write program in C++ Write a function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. please write the program in C++

  • Write a function called productEven that takes as its parameter an input file. The function should...

    Write a function called productEven that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. c++ program

  • •The multiplication operator works by summing some value, x, by another value, y. Write a function...

    •The multiplication operator works by summing some value, x, by another value, y. Write a function definition called multiply that takes two integer parameters to perform the operation described. Return the answer to the calling function. •Write a function called randCount with no parameters. The function should randomly generate 5 numbers between 8 to 15 and counts how many times a multiple of 2 occurred. Return the answer to the calling function. •Write a function called summary that takes as...

  • Write a complete C++ program that is made of functions main() and rShift(). The rShift() function...

    Write a complete C++ program that is made of functions main() and rShift(). The rShift() function must be a function without return with 6 parameters. rShift() should do following. Shift the first 4 formal parameters' value one place to right circularly and send the updated values out to the caller function, i.e. main. Furthermore, after calling this function with first 4 actual parameters, say a1, a2, a3, a4, and these actual parameters should shift their value one place to right...

  • Write a function in the C++called summary that takes as its parameters an input and output...

    Write a function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values. Please write program in C++.

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • Write a complete C++ program that at least consists of the main() function and a recursive...

    Write a complete C++ program that at least consists of the main() function and a recursive function gcd() with return value. Define function gcd() with two and only two parameters that calculates the greatest common divider of two given parameters. Hint: use the difference between two parameters or the remainder obtained using one parameter divide the other. In main() Read 2 positive integers with proper prompt. Call gcd() with proper syntax. Display the result, i.e. the greatest common divider of...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

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