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

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...
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
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read data back from the file and display the data in increasing order. After writing the file to disk, the input file should be read into an array, sorted using the static Arrays.sort() method from the Java API and then displayed in the...
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at...
C++ sort a file with 140 integers where only 20 integers maybe placed into memory at any one time Main idea: break data into blocks, sort blocks into runs, merge runs less. Call inFile1 our source file (the one with the initial 140 records to be sorted. You will also need an inFile2, and 2 other files outFile1 and outFile2. Break the file into blocks of size 20: in this case there will be 7 blocks ( 140/20 ) Sort...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Create by using Linux server • Create a file name it foo.txt • Remove all permissions...
Create by using Linux server • Create a file name it foo.txt • Remove all permissions from foo.txt • What happen if you try to read the file? • Change foo.txt permission to read and write only for owner • Change foo.txt permission to read for group • Change foo.txt permission to read and write everyone
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT