Question

In: Computer Science

(C++) Create a data file and name it "input.txt". manually save 10 integers into the file....

(C++) Create a data file and name it "input.txt". manually save 10 integers into the file. Write a program to read the data and calculate the average of events and odds, separately. Print out the average values.

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <ctime>
#include <stdio.h>      
#include <stdlib.h>
#include <fstream>

using namespace std;

int main()
{
   // WRITING TO FILE
   srand((unsigned)time(NULL));
    //Declaring variables
    string fileName = "input.txt";
    float grade;
    
    //Opening file
    ofstream myfile;
    myfile.open(fileName.c_str());

    for (int i = 0; i < 10; i++) {
        myfile<<(1 + rand() % 10)<<endl;
    }
    //Cloing file
    myfile.close();
    
    // READING FROM FILE
    ifstream file;
   file.open (fileName.c_str());
    if (!file.is_open()) return 0;
   int evenSum = 0, oddSum = 0, evenCount = 0, oddCount = 0;
    int val;
    while (file>>val)
    {
       if(val%2==0){
          evenCount++;
          evenSum+=val;
      }
      else{
         oddCount++;
         oddSum+=val;
      }
    }
   
   cout<<"Average of evens = "<<(1.0*evenSum/evenCount)<<endl;
   cout<<"Average of odds = "<<(1.0*oddSum/oddCount)<<endl;
   
    return 0;
}

Related Solutions

Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not...
Write a C++ pgm which 1.reads an input file named 'input.txt' 2.content of input.txt are not known to you 3.copies content of input file into an output file named 'output.txt' 4.contents of output.txt should be exactly the same as contents of input.txt 5.the user should be given a choice to select input and output file in any folder 6.remember to check that input file opened successfully
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is...
Construct an array of 1000 random integers within range [0, 100] An input file input.txt is provide. Each line of input.txt is a query integer that you need to check how many of that number is in your random integer array. For each query integer, fork a new child process to do the counting. The output is for each input query, output the count and child process id. For example: $> query: 13    count: 5    pid: 13342 $> query: 22   ...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What command gives you a long listing of your filenames in the current directory, including permissions attached to each file. What command would you use to change a file’s permissions to include read, write and execute permission for the owner of the file only. Explain the following file permissions    a) 777          b) 765          c) 400          d) 666          e) 600         ...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Create a C# application for SummerSchool that reads the file Participants. participant's data on the screen....
Create a C# application for SummerSchool that reads the file Participants. participant's data on the screen. Create a Participant class that contains field first name, last name, age, and course taken for the summer school and To fields of records in the file are separated with semicolon (). 3 marks) E.g. of a record in the file Participant.txt: 123,John,Smith35:Math using System; using static System. Console; using System. I0; class SummerSchool static void Main() /7Your code as answer
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT