Question

Question 3 (2 points) Suppose, you have a file called numbers.txt where each line of the file contains a number. The number
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

I have done this program in both C and C++ Programming Language

CODE: C Programming Language

#include<stdio.h>
int main(){
    int n, sum = 0, count = 0;
    FILE *fp;
    fp= fopen("numbers.txt", "r"); // reading text file
    //the sum part
    // reading the integers from file
    while( EOF != fscanf(fp, "%i", &n)){
       if(n > 0 && n <= 1000){
           sum = sum + n; // Incrementing the sum between the values 0 to 1000(including)
           count++;   // Incrementing count
       }          
    }
    printf("The Average is :%d", sum/count); // Printing the average
    fclose(fp);
    return 0;
}

SCREENSHOT OF THE CODE:

1 #include<stdio.h> 2 int main() { 3 int n, sum = @, count = ; 4 FILE *fp; 5 fp= fopen(numbers.txt, r); // reading text f

INPUT:

numbers.txt

numbers - Notepad File Edit Format View Help 10 25 -5 1001 10

OUTPUT:

C:\Users\Eswar\Desktop\newg++ open average.exe The Average is :15 Process exited after 0.1856 seconds with return value o Pre

===========================================================================

CODE: C++ Programming Language

#include <iostream>
#include <fstream>
using namespace std;
int main(){
    fstream fp; // declare a file
    fp.open("numbers.txt", ios::in); // open the file
    int sum = 0;
    int count = 0;
    string line;
    while (getline(fp, line)){ //reading a line from the file while possible
        if(stoi(line) > 0 && stoi(line) <= 1000){
            sum = sum + stoi(line); // convert string to number and add it to the sum
            count ++;
       }  
    }
    // closing the file
    cout << "The average is: " << sum / count; // print the sum
    cin.get();
    fp.close();
    return 0;
}

SCREENSHOT OF THE CODE:

1 #include <iostream> 2 #include <fstream> 3 using namespace std; 4 int main() { 5 fstream fp; // declare a file 6 fp.open(n

INPUT:

numbers.txt

numbers - Notepad File Edit Format View Help 10 25 -5 1001 10

OUTPUT:

C:\Users\Eswar\Desktop\newg++ open average.exe The Average is :15 Process exited after 0.1856 seconds with return value o Pre

Thank you. Please ask me if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
Question 3 (2 points) Suppose, you have a file called 'numbers.txt' where each line of 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
  • In python Attached is a file called sequences.txt, it contains 3 sequences (one sequence per line)....

    In python Attached is a file called sequences.txt, it contains 3 sequences (one sequence per line). Also attached is a file called AccessionNumbers.txt. Write a program that reads in those files and produces 3 separate FATSA files. Each accession number in the AccessionNumbers.txt file corresponds to a sequence in the sequences.txt file. Remember a FASTA formatted sequence looks like this: >ABCD1234 ATGCTTTACGTCTACTGTCGTATGCTTTACGTCTACTGACTGTCGTATGCTTACGTCTACTGTCG The file name should match the accession numbers, so for 1st one it should be called ABCD1234.txt. Note:...

  • Description: Create a program called numstat.py that reads a series of integer numbers from a file...

    Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...

  • How to write a Java file out that that reads from numbers.txt with these numbers 2...

    How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner {    public static void main(String[] args)    {        /* For Homework! Tokens*/        Scanner file = null;        PrintWriter fout= null;   ...

  • Basic C program Problem: In this assignment, you have to read a text file (in.txt) that...

    Basic C program Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

  • Assume a text file called film_reviews.txt exists with the following contents: 7 Wall-E 5 The Martian...

    Assume a text file called film_reviews.txt exists with the following contents: 7 Wall-E 5 The Martian 4 Wall-E 4 The Intern 3 ... The first line of the input file shows the number of reviews in the file. Afterward, each film/review p0ari is then written in subsequent lines in the file. Write a Java application which will read the contents of film_reviews.txt and produce an output file named review_averages.txt which contains the film name and the average of reviews in...

  • 6. You pilot a small plane. Your plane receives the following information from nearby planes: the...

    C language 6. You pilot a small plane. Your plane receives the following information from nearby planes: their three digit ID, and distance away in the x, y, and z directions Write a program that reads in the information from the file air test.txt and prints out the ID and distance of the nearby planes. If the distance is less than 12 miles print a warning message. (The file has one aircraft entry per line in the format 123 45...

  • The input file should be called 'data.txt' and should be created according to the highlighted instructions...

    The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the...

  • Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions...

    Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions that might be thrown within them. 1. Create a class that does the following: a. Reads the name of a file to create as the first command line argument. (Overwrite any file with the same name). b. Reads an integer value as the second command line argument. C. The program should generate as many random numbers as are specified in the second command line...

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

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