Question

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++.

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

CODE:

#include <iostream>
#include <fstream>

using namespace std;

void summary(string filename,string filename1){
fstream file,file1;
//opening both the input and output files
file.open(filename.c_str());
file1.open(filename1.c_str(),ios::out);
int a,b;
//getting the two numbers from the input file
file>>a;
file>>b;

int oddSum = 0,evenSum = 0,cumprod = 1;
for(int i=a;i<=b;i++){
//finding the sum of even and odd numbers separately
if(i%2 == 0){
evenSum += i;
}else{
oddSum += i;
}
//finding the cumulative product in the range
cumprod *= i;
}
file1<<"Sum of even numbers in the range "<<a<<" to "<<b<<" is: "<<evenSum<<endl;
file1<<"Sum of odd numbers in the range "<<a<<" to "<<b<<" is: "<<oddSum<<endl;
file1<<"Cumulative product of numbers in the range "<<a<<" to "<<b<<" is: "<<cumprod<<endl;
//closing the files
file.close();
file1.close();
}
int main() {
//calling the function
summary("input.txt","output.txt");
cout<<"Output File written"<<endl;

return 0;
}

_________________________________________________

CODE IMAGES:

- jhyhm.cpp - Code::Blocks 17.12 File Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Se- jhvhm.cpp - Code::Blocks 17.12 Eile Edit View Search Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Se________________________________________________

input.txt:

input.txt - Notepad Х File Edit Format View Help 252 5

output.txt:

output.txt - Notepad Х File Edit Format View Help Sum of even numbers in the range 2 to 5 is: 6 Sum of odd numbers in the ran

OUTPUT:

- х C:\Users\Win10\Documents\jhyhm.exe Output File written execution time : 0.297 s Process returned 0 (0x0) Press any key to____________________________________________________

Feel free to ask any questions in the comments section
Thank You!

Add a comment
Know the answer?
Add Answer to:
Write a function in the C++called summary that takes as its parameters an input and output...
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
  • •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...

  • 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...

  • 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...

  • 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

  • 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 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...

  • Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the...

    Python 1 Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. 3 To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use...

  • python program please Write a function called backwards which takes two parameters. The first is a...

    python program please Write a function called backwards which takes two parameters. The first is a file object that has already been opened in read mode called essayfile. The second is called filename, which contains the filename for the output file that will need to be properly opened and closed in your function. The function should read from already opened file object line by line. Each line in the file should have the words on the line reversed. For example,...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • Write a C++ program that contains a function called swapNums. This function takes two integer parameters,...

    Write a C++ program that contains a function called swapNums. This function takes two integer parameters, swaps them in memory, and outputs the results (there is nothing to return). In main, ask the user to enter two different numbers, output them as entered (step 1), call the function swapNums() which will output the numbers swapped (step 2), and then output the values again in main (step 3). You should have three sets of output. Sample run (10 and 5 were...

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