Finish a C++ code. Assume the , , and 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) (picture 2, of the capitals.txt text) Text to copy for the capitals.txt file: 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.

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

Finish a C++ code.

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)

(picture 2, of the capitals.txt text)

Text to copy for the capitals.txt file:

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! Thank you

**Population of U.S. State Capitals**

Below is a list of selected U.S. state capitals along with their respective populations:

- Phoenix, Arizona: 1,660,272
- Austin, Texas: 964,254
- Columbus, Ohio: 892,553
- Indianapolis, Indiana: 867,125
- Denver, Colorado: 716,492
- Boston, Massachusetts: 694,583
- Nashville, Tennessee: 635,710
- Sacramento, California: 466,488
- Atlanta, Georgia: 420,003
- Honolulu, Hawaii: 359,870

This data presents a comparison of population sizes among various state capitals across the United States. For any educational purpose, this information can be utilized to study urban demographics, population distribution, and city planning. The capitals are listed with their population for comparative analysis. Note the variations in population size, indicating the diverse scales and impacts of urbanization within different regions.
Transcribed Image Text:**Population of U.S. State Capitals** Below is a list of selected U.S. state capitals along with their respective populations: - Phoenix, Arizona: 1,660,272 - Austin, Texas: 964,254 - Columbus, Ohio: 892,553 - Indianapolis, Indiana: 867,125 - Denver, Colorado: 716,492 - Boston, Massachusetts: 694,583 - Nashville, Tennessee: 635,710 - Sacramento, California: 466,488 - Atlanta, Georgia: 420,003 - Honolulu, Hawaii: 359,870 This data presents a comparison of population sizes among various state capitals across the United States. For any educational purpose, this information can be utilized to study urban demographics, population distribution, and city planning. The capitals are listed with their population for comparative analysis. Note the variations in population size, indicating the diverse scales and impacts of urbanization within different regions.
### Sample C++ Program for Finding the Smallest State Capital by Population

```cpp
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;
}
```

### Explanation:

This C++ program is designed to find the smallest state capital by population. Here is a step-by-step explanation:

1. **Include Necessary Libraries:** The `ifstream` library is included to read from files.
   
2. **Open the File:** The program attempts to open a file named `capitals.txt`. This file is expected to contain information about state capitals, their states, and their populations.

3. **Declare Arrays:**
    - `city[10]`: This array will hold the names of 10 cities.
    - `state[10]`: This array will hold the names of 10 states.
    - `population[10]`: This array will hold the population of each city.

4. **Find Smallest Capital:** The `smallestCapital` function (not shown in the provided code) is called. This function is supposed to scan through the cities, states, and populations arrays and return the index of the city with the smallest population.

5. **Check File Status:**
    - If `smallestCapital` returns `-1`, it means the file did not open correctly. The program then outputs "File did not open."
    - Otherwise, it prints the name of the smallest state capital, along with its state and population.

6. **Close File:** The program closes the file `capitals.txt` to free system resources.

7. **Return Statement:** The `return 0;` statement indicates that the program has executed successfully.

### Diagram and Graphs:

No diagrams or graphs are included in the provided code snippet. The primary focus of this code is to read data from a file, process it, and output relevant information based on the data.
Transcribed Image Text:### Sample C++ Program for Finding the Smallest State Capital by Population ```cpp 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; } ``` ### Explanation: This C++ program is designed to find the smallest state capital by population. Here is a step-by-step explanation: 1. **Include Necessary Libraries:** The `ifstream` library is included to read from files. 2. **Open the File:** The program attempts to open a file named `capitals.txt`. This file is expected to contain information about state capitals, their states, and their populations. 3. **Declare Arrays:** - `city[10]`: This array will hold the names of 10 cities. - `state[10]`: This array will hold the names of 10 states. - `population[10]`: This array will hold the population of each city. 4. **Find Smallest Capital:** The `smallestCapital` function (not shown in the provided code) is called. This function is supposed to scan through the cities, states, and populations arrays and return the index of the city with the smallest population. 5. **Check File Status:** - If `smallestCapital` returns `-1`, it means the file did not open correctly. The program then outputs "File did not open." - Otherwise, it prints the name of the smallest state capital, along with its state and population. 6. **Close File:** The program closes the file `capitals.txt` to free system resources. 7. **Return Statement:** The `return 0;` statement indicates that the program has executed successfully. ### Diagram and Graphs: No diagrams or graphs are included in the provided code snippet. The primary focus of this code is to read data from a file, process it, and output relevant information based on the data.
Expert Solution
steps

Step by step

Solved in 2 steps with 3 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