Question

This assignment should be relatively easy. Write a program in any reasonably high-level language, that reads...

This assignment should be relatively easy. Write a program in any reasonably high-level language, that reads in numbers from a file, one per line, until it detects that a 0 (zero not an oh') has been entered The 0 does not count as part of the number series. After all numbers have been entered print out the number of items entered, the sum, the minimum value entered, the maximum value entered, and the arithmetic mean (one on each line), in the order listed here.

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

\color{red}\underline{In\;Java:}

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class NumbersFile {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter file name: ");  // ask for file name
        File file = new File(in.nextLine());    // read file name
        try {
            Scanner fin = new Scanner(file);    // open file
            int count = 0;  // create variables
            double min = 0, max = 0, total = 0, num;
            while (fin.hasNextInt()) {  // as long as there are number in file
                num = fin.nextInt();    // read numbers from file
                if (num == 0)   // if number read was 0, then stop processing
                    break;
                if (count == 0) {
                    min = num;
                    max = num;
                }
                // update min, max, total and count
                if (num > max) max = num;
                if (num < min) min = num;
                total += num;
                ++count;
            }
            // calculate arithmetic mean
            double mean = total / count;

            // display results
            System.out.println("Number of items read is " + count);
            System.out.println("The sum of numbers is " + total);
            System.out.println("The smallest of numbers is " + min);
            System.out.println("The largest of numbers is " + max);
            System.out.println("The arithmetic mean of numbers is " + mean);
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " is not found!");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
This assignment should be relatively easy. Write a program in any reasonably high-level language, that reads...
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
  • This assignment should be relatively easy. Write a program in C language, that reads in numbers...

    This assignment should be relatively easy. Write a program in C language, that reads in numbers from any file, one per line, until it detects that a 0 (zero not an oh') has been entered The 0 does not count as part of the number series. After all numbers have been entered print out the number of items entered, the sum, the minimum value entered, the maximum value entered, and the arithmetic mean (one on each line), in the order...

  • Easy 68k code - assembly language Write a 68K program that reads two integers (>10 and...

    Easy 68k code - assembly language Write a 68K program that reads two integers (>10 and <225) of the keyboard, adds them together and multiplies them, writes the numbers and the results of addition and multiplication on the screen. If the numbers are not on the range, writes an error message and requests a new number. Example of the execution: Insert the first number: 10 Insert the second number:15 The sum is: 25 The product is: 150 Please take a...

  • Please write in Python language. Assignment 5 0 Write a program that reads in integers and...

    Please write in Python language. Assignment 5 0 Write a program that reads in integers and then determines whether input value is prime number or not(using for statement) • First input Input number: 18457 yes . Second input Input number: 52 no

  • Assignment #9 will be the construction of a program that reads in a sequence of integers...

    Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers. Then compute the minimum number, compute the largest number among the numbers that are divisible by 2, count even numbers, and compute the...

  • Write a C language program that will prompt for 12 different resistor values, entered one at...

    Write a C language program that will prompt for 12 different resistor values, entered one at a time via the keyboard, expressed in Ohms. Valid values are 0.01 Ohm up to 10,000,000,000 Ohms. Write each entry to a text file on the disk thus: Resistor 1 Ohmic value = 2200. Update the resistor number for each of the 12 entries. Do not repeat any resistance value. Write a second C language program to read the 12 entries from the text...

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

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

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • In C language This program reads in a series of words. All words consist of only...

    In C language This program reads in a series of words. All words consist of only lower-case letters ('a' through 'z'). None of the words entered will be longer than 50 letters. The program reads until ctrl-d (end-of-file), and then prints out all the lower-case letters that were missing in the input or a statement indicating the input has all the letters. Two executions of the program are shown below. Enter your input: the quick brown fox jumps over the...

  • Write a Python program that reads user input from the command-line. The program should define a...

    Write a Python program that reads user input from the command-line. The program should define a function read Position, which reads the values for t, v0, and h0. If there is an IndexError, print 'Please provide the values for t, vO, and hO on the command line.'. If t, v0, or h0 are less than 0. print 't = # is not possible.' for each variable respectively. Note that the # represents the number entered on the command-line by the...

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