Create a function named smallestCapital as follows: The function returns an integer. The function has 5 parameters: Two string arrays One integer array An ifstream object An integer for the length of the arrays The function should be coded as follows: If the ifstream object did not open the file, return a value of -1. Otherwise, use a while loop to read data from the file and store each data value into its corresponding array. The ifstream object must be used in the loop condition. Use any type of loop to find the index of the smallest population and return it.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Assume the <iostream>, <fstream>, and <string> headers have already been included and that main() looks like this:

(picture 1)

Each row of the capitals.txt file contains the following data, in this order:

  • The city (string, one word)
  • The state (string, one word)
  • The population (integer)

Here are the exact contents of the "capitals.txt" file to copy:

Phoenix Arizona 1660272

Austin Texas 964254

Ohio Columbus 892553

Indianapolis Indiana 867125

Denver Colorado 716492

Boston Massachusetts 694583

Nashville Tennesseee 635710

Sacramento California 466488

Atlanta Georgia 420003

Honolulu Hawaii 359870

 

Create a function named smallestCapital as follows:

The function returns an integer.
The function has 5 parameters:
Two string arrays
One integer array
An ifstream object
An integer for the length of the arrays
The function should be coded as follows:
If the ifstream object did not open the file, return a value of -1.
Otherwise, use a while loop to read data from the file and store each data value into its corresponding array. The ifstream object must be used in the loop condition.
Use any type of loop to find the index of the smallest population and return it.


Rules:

Submit only the code you are asked to write. You do not have to create a main program or a function prototype.
The code you submit must still follow all C++ syntax rules and compile.
You must use the exact type of loop specified in each part that requires a loop.
The test conditions for all loops must contain a boolean expression with a comparison operator (==, <=, >=, and so on).
No break or continue statements are allowed.
You may create as many local variables in the function as you believe are needed.

 

will upvote!

int main ()
ifstream inFile;
inFile.open ("capitals.txt");
string city[10];
string state[10];
int population[10];
int here = smallestCapital (city, state, population, inFile, 10);
if (here == -1) (
cout << "File did not open." << endl;
} else {
cout <« "Smallest state capital is " <« city [here] << ", " <« state [here] << " with population " << population [here] << endl;
}
inFile.close () ;
return 0;
}
Transcribed Image Text:int main () ifstream inFile; inFile.open ("capitals.txt"); string city[10]; string state[10]; int population[10]; int here = smallestCapital (city, state, population, inFile, 10); if (here == -1) ( cout << "File did not open." << endl; } else { cout <« "Smallest state capital is " <« city [here] << ", " <« state [here] << " with population " << population [here] << endl; } inFile.close () ; return 0; }
Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY