Question

Problem: Create a program that contains two functions: main and a void function to read data...

Problem: Create a program that contains two functions: main and a void function to read data from a file and process it. Your main function should prompt the user to enter the name of the file (Lab7.txt), store this name, and then call the function sending the name of the file to the function. After the function has completed, the main function should output the total number of values in the file, how many of the values were odd, how many of the values were even, and how many of the values were zero. Your other function should accept the string object containing the name of the data file and other parameters as needed. This function should define a file object for input, open the file, skip over the first line (do not store the first line). Then this function should use a single loop to count the total number of values in the file, how many of those values were even, how many of those values were odd, and how many of those values were zero (do not include zeros as even). After all the values have been read and processed, this function should send back to main the number of values in the file, the number of zeros, the number of odd and the number of even values. Carefully consider which parameters should be passed by value and which should be reference parameters.

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

#include <iostream>
#include<fstream>
using namespace std;

void printInfo(char filename[]){
int number;
int zeroCount = 0, evenCount = 0, oddCount = 0, count = 0;
fstream InFile;
InFile.open(filename, ios::in | ios::binary);
while(InFile >> number){
count ++;
if(number == 0)
zeroCount++;
else if(number % 2 ==0)
evenCount++;
else
oddCount++;
}
cout << "The total no of numbers present in the file are " << count << endl;
cout << "The total no of zeros present in the file are " << zeroCount << endl;
cout << "The total no of even numbers present in the file are " << evenCount << endl;
cout << "The total no of odd numbers present in the file are " << oddCount << endl;
}
int main(){

char filename[30];
cout << "Enter the file name: ";
cin >> filename;
printInfo(filename);
return 0;
}

main.cppb7.txt X CAUsers LENOVO DownloadslmagiclbinlDebuglmagic.exe 9 2 3 5 7 Enter the file name: Lab7.txt The total no of n

**Comment for any further queries. Upvote if the answer is satisfactory.

Add a comment
Know the answer?
Add Answer to:
Problem: Create a program that contains two functions: main and a void function to read data...
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
  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

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

  • This program should use a main function and two other functions named playlist and savelist as...

    This program should use a main function and two other functions named playlist and savelist as follows: The main function: The main function should create an empty list named nums and then use a loop to add ten integers to nums, each integer in the range from 10-90. NOTE: In Python, a list is a data type. See Chapter 7. The main function should then call the playlist function and savelist function, in that order. Both of these functions take...

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

  • I'm a bit confused on how to get this program to run right. Here are the...

    I'm a bit confused on how to get this program to run right. Here are the directions: Part 1: Write a Python function called reduceWhitespace that is given a string line and returns the line with all extra whitespace characters between the words removed. For example, ‘This line has extra space characters ‘  ‘This line has extra space characters’ Function name: reduceWhitespace Number of parameters: one string line Return value: one string line The main file should handle the...

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

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write two functions: Main should call the first function. The first function should use getline to...

    Write two functions: Main should call the first function. The first function should use getline to read a line of data: getline(cin,variableToHoldLineOfData); It should then call the second function, sending the line of data it just read. The second function should determine the number of CAPITAL letters, the number of lowercase letters, and the number of everything else (that's not capital or lower case). Count the length of the string and then use a for loop to go through each...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

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