1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
belka [17]
2 years ago
11

Implement the function fileSum. fileSum is passed in a name of a file. This function should open the file, sum all of the intege

rs within this file, close the file, and then return the sum. If the file does not exist, this function should output an error message and then call the exit function to exit the program with an error value of 1. The exit function is provided by the cstdlib library. Here is how you call the exit function if you want the exit function to return a 1 (has the same result as the main function returning a 1).

Computers and Technology
1 answer:
maria [59]2 years ago
5 0

Answer:

/*C++ program that prompts user to enter the name of input file(input.txt in this example) and print the sum of the values in the file to console. If file dosnot exist, then close the program */

//header files

#include <fstream>

#include<string>

#include <iostream>

#include <cstdlib> //needed for exit function

using namespace std;

//function prototype

int fileSum(string filename);

int main()

{

string filename;

cout << "Enter the name of the input file: ";

cin >> filename;

cout << "Sum: " << fileSum(filename) << endl;

system("pause");

return 0;

}

/*The function fileSum that takes the string filename and

count the sum of the values and returns the sum of the values*/

int fileSum(string filename)

{

//Create a ifstream object

ifstream fin;

//Open a file

fin.open(filename);

//Initialize sum to zero

int sum=0;

//Check if file exist

if(!fin)

{

cout<<"File does not exist ."<<endl;

system("pause");

exit(1);

}

else

{

int value;

//read file until end of file exist

while(fin>>value)

{

sum+=value;

}

}

return sum;

}//end of the fileSum

Explanation:

This is a C++ program that prompts user to enter the name of input file(input.txt in this example) and print the sum of the values in the file to console. If file dosnot exist, then close the program.

Check attachment for sample output screenshot.

You might be interested in
Write the method makeNames that creates and returns a String array of new names based on the method’s two parameter arrays, arra
Gwar [14]

Answer:

  1. import java.util.Arrays;
  2. public class Main {
  3.    public static void main(String[] args) {
  4.        String [] first = {"David", "Mike", "Katie", "Lucy"};
  5.        String [] middle = {"A", "B", "C", "D"};
  6.        String [] names = makeNames(first, middle);
  7.        System.out.println(Arrays.toString(names));
  8.    }
  9.    public static String [] makeNames(String [] array1, String [] array2){
  10.           if(array1.length == 0){
  11.               return array1;
  12.           }
  13.           if(array2.length == 0){
  14.               return array2;
  15.           }
  16.           String [] newNames = new String[array1.length];
  17.           for(int i=0; i < array1.length; i++){
  18.               newNames[i] = array1[i] + " " + array2[i];
  19.           }
  20.           return newNames;
  21.    }
  22. }

Explanation:

The solution code is written in Java.

Firstly, create the makeNames method by following the method signature as required by the question (Line 12). Check if any one the input string array is with size 0, return the another string array (Line 14 - 20). Else, create a string array, newNames (Line 22). Use a for loop to repeatedly concatenate the string from array1 with a single space " " and followed with the string from array2 and set it as item of the newNames array (Line 24-26). Lastly, return the newNames array (Line 28).

In the main program create two string array, first and middle, and pass the two arrays to the makeNames methods as arguments (Line 5-6). The returned array is assigned to names array (Line 7). Display the names array to terminal (Line 9) and we shall get the sample output: [David A, Mike B, Katie C, Lucy D]

5 0
3 years ago
_______ integrates all departments and functions throughout an organization into a single IT system (or integrated set of IT sys
aleksklad [387]

Answer:

ERP ( Enterprise Resource Planning )

Explanation:

ERP mainly stands for enterprise resource planning. ERP is system software which is a combination of many application which works together to automate a business process.  

It is a business management software which reduces the wastage of time by making tasks simpler like when we talk about accounting and stock in a company, it takes very much time when maintaining by humans but when it comes to ERP software, it only takes a while to give you detailed information about everything. It automates the manual process of a company and gives speed to the growth of the company.

3 0
2 years ago
is there a way to send files from my PC to my iPhone? My PC’s wifi receptors are broken so it cannot connect to wifi. Is there a
Alex_Xolod [135]

Answer:

I believe you need a internet connection for that or try a Hotspot that can work too.

5 0
2 years ago
Siltstone is made mostly of _____.<br><br> A : silt<br> B : sand<br> C : shells<br> D : gravel
frez [133]
The best and most correct answer among the choices provided by the question is the second choice. <span>Siltstone is made mostly of silt. </span>I hope my answer has come to your help. God bless and have a nice day ahead!
6 0
3 years ago
Read 2 more answers
Write a program that reads a list of words. Then, the program outputs those words and their frequencies. Ex: If the input is: he
Tpy6a [65]

Answer:

#Declare variable to get the input

#list from the user

input_list = input()

#split the list into words by space

list = input_list.split()

#Begin for-loop to iterate each word

#to get the word frequency

for word in list:

#get the frequency for each word

frequency=list.count(word)

#print word and frequency

print(word,frequency)

Explanation:

This program that we are told to write will be done by using python programming language/ High-level programming language.

The question wants us to write a program in which the output will be as below;

hey 1

hi 2

Mark 1

hi 2

mark 1

Just by imputing hey hi mark hi mark.

Just copy and run the code below.

#Declare variable to get the input

#list from the user

input_list = input()

#split the list into words by space

list = input_list.split()

#Begin for-loop to iterate each word

#to get the word frequency

for word in list:

#get the frequency for each word

frequency=list.count(word)

#print word and frequency

print(word,frequency)

4 0
3 years ago
Other questions:
  • Question 1 of 20 :
    15·2 answers
  • What does “int” means in php code
    13·1 answer
  • How can I sent a message?
    13·1 answer
  • ____ refers to data, applications, and even resources stored on computers accessed over the Internet. Answer
    5·1 answer
  • How should font appear in a slide presentation compared to the font in a document
    7·1 answer
  • A ________ is a member function that is automatically called when a class object is
    6·1 answer
  • Light is a key formal element that film artists and technicians carefully manipulate to create mood, reveal character, and conve
    15·1 answer
  • Suppose a host has a 1-MB file that is to be sent to another host. The file takes 1 second of CPU time to compress 50%, or 2 sec
    12·1 answer
  • What is TCP/IP adress​
    10·1 answer
  • B. Some of Company X's new practices and systems are unethical. Business ethics is a set of codes
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!