Question

Using C++, Write a program that will search a file filled with numbers, and show the...

Using C++,

Write a program that will search a file filled with numbers, and show the largest number and the smallest number. The program will also count how many of the numbers are even (evenly divisible by two). Use the file data71.txt.

File data71.txt numbers:

23
56
78
90
34
123
456
789
986
543
1104
2
4
7
543
124

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

Please find the code below:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;
int main( )
{

   ifstream infile;
   infile.open ("data71.txt"); //name of file here. plz mention Complete path if file is not at root

   int min = 9999;
   int max = 0;
   int temp;
   int evenCount =0;
   int totalNumberCount = 0;
   if (infile.is_open()) //if file opened
   {
       while(infile>>temp ) { //get row from text file
           if(min>temp){
               min=temp;
           }
           if(max<temp){
               max=temp;
           }
           if(temp%2==0){
               evenCount++;
           }
           totalNumberCount++;
       }
       infile.close(); //close file
       cout<<"File scan done........"<<endl;
   }
   else //if file not found show the below message
   {
       cout << "Sorry, we could not find the file." << endl;
   }

   cout<<"Total number count : "<<totalNumberCount<<endl;
   cout<<"Total even number count : "<<evenCount<<endl;
   cout<<"Minimum number is : "<<min<<endl;
   cout<<"Maximum number is : "<<max<<endl;


   return 0;
}

data71.txt

23
56
78
90
34
123
456
789
986
543
1104
2
4
7
543
124

output:

console X terminated> CPP Workspace.exe [C/C++ Application] CAU Total number count 16 Total even number count : 10 Maximum number is: 1104 if file is not

Add a comment
Know the answer?
Add Answer to:
Using C++, Write a program that will search a file filled with numbers, and show the...
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 program in C that reads 20 float numbers from a file. The values are...

    Write a program in C that reads 20 float numbers from a file. The values are stored in a vector (1-dimensional array). Find and display the smallest number, the second smallest number, and the third smallest number of the set. Use only these libraries: stdio.h, stdlib.h, math.h, and string.h. Below is an example of the input file (in1.txt) and the resulting output file (output.txt). In1.txt: 3.14       4.05       -3.73      4.13       1.32 -2.21      0.46       4.57       4.64       -3.42 4.57       -0.14      3.00       -3.58      -0.78...

  • Write a program that reads an integer k from user and finds the number of elements...

    Write a program that reads an integer k from user and finds the number of elements that are divisible by k in the file assignment4.txt. A number n is divisible by k if n = kx for some integer x > 0. You can use the mod operator % to test for divisibility. The file assign4.txt has integer values in the range [0,100 ]. You should use end-of-file controlled loop for this problem. Sample execution is given below. Assume the...

  • write program above in C only Write a program to find statistics on some random numbers....

    write program above in C only Write a program to find statistics on some random numbers. Seed the random number generator with time. In a for loop generate 12 random numbers between the values of 2 and 20. Print the numbers. As the numbers are generated, find the following: the number of even numbers (those evenly divisible by 2) e the sum of the even numbers the product of the even numbers the maximum value of all the numbers e

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • Write a C++ that read a list of numbers from a .txt file into array. The...

    Write a C++ that read a list of numbers from a .txt file into array. The first digit on the list will be array size, while the second number on the list will be our first number in our array list. Example: t1.txt file contain list off input number: 5, 3, 6, 10, 43, 23. notice the first number is 5 which will need to be use as an array size and the rest of the numbers are in the...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a...

    C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a program that reads ANY sequence of integers (can be positive, 0 or negative) from this file. Using a looping construct, you will read numbers from the file till end of the file is reached. Calculate the largest and the smallest numbers among the data in the file. Your code must display both of those on the terminal screen along with total count of numbers...

  • Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

    Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART B: (Statistics Program) – (50%) Please read this lab exercise thoroughly, before attempting to write the program. Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

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