Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 2010 Lab11 Due: Friday, April 8 2016 *arrays/for loop/functions/File I/O 5 Po

ID: 3685823 • Letter: C

Question

CS 2010 Lab11   

Due: Friday, April 8 2016                                                                             

*arrays/for loop/functions/File I/O                                                                                 

5 Points                                                             Fishers                                        

Purpose: In this lab, you will write a complete C++ program, called Fishers program. This lab helps you to practice the use of arrays, for loops, functions, and File I/O in C++ program. (In this lab, you will exercise Pair Programming- work with a partner.)

Assignment

Create an empty project named Lastname1Lastname2Lab11.

Add a C++ file and name it as lab11_fishers.cpp

Write a banner (comments at the top) as following:

// File Name   : lab11_fishers.cpp

// Description : Lab 11– This program tracks fishers

// Author      : Put BOTH of your names here!

      // Date        : 04/08/16

Write a complete C++ program for the following program description.

Your instructor and his friends just came from a fishing trip and he is asking you to write a program to determine how many fish they caught and the fisher that caught the most fish. He has a file (i.e., “fish.txt”, available on Canvas) which includes the number of fish caught by each fisher. The input file contains no more than 25 numbers. (Hint: use this information to declare a global constant (maxfishers) and declare your array (fish) by using maxfishers in the main.)

In the main program, call a series of three functions, get_fish, show_fish and find_best_fisher. The main function should pass the fish array and the number of array elements (numfishers) to each function. Each task should be performed by a separate function as described below. (Note that all function calls should be made from the main function.)

1. Call get_fish() to read the data from the input file and store it in the array. Use a sentinel loop with eof( ). Inside the loop, Read the values into the array inside the loop and also keep track of the number of fishers (numfishers) which will be returned as a reference parameter to the main function. You will declare the file variable locally inside of this function (ifstream infile;), open, use and close the data file all in the function.

** Your next two functions must use for loops.

2. Call show_fish()function to display the number of fish caught by each fishers on the screen.

3. Call find_best_fisher()function to find the fisher that caught the most fish.

      - the function will return the subscript of the fisher that caught the most fish

4. Have the main function display the fisher that caught the most fish and the number of fish caught.

Output Example:

Explanation / Answer

/*** C++ to find the fisher who caught the maximum number of fishes ***/

#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
using namespace std;

vector<int> get_fish(int *numfishers)
{
ifstream infile;

vector<int> fishes;
int fish;
  
infile.open("fish.txt"); // open file

while (infile >> fish) // reading the file
{
fishes.push_back(fish);
}

infile.close();
*numfishers = fishes.size();

return fishes;
}

void show_fish(vector<int> &fishes, int numfishers)
{
for (int i = 0; i < numfishers; ++i)
{
cout << "Fishes caught by fisher " << i+1 << " is " << fishes[i] << endl;
}

}

int find_best_fisher(vector<int> &fishes, int numfishers)
{
int highest = 0;
for (int i = 1; i < numfishers; ++i)
{

if(fishes[i] > fishes[highest]) highest = i;
}

return highest;
}


int main()
{
int numfishers ;

vector<int> fishes;
fishes = get_fish(&numfishers);


show_fish(fishes,numfishers);
int highest = find_best_fisher(fishes, numfishers);

cout << endl;
cout << "Fisher " << highest+1 << " caught the most number of fishes "<< endl;
cout << "Most fishes caught are " << fishes[highest];
cout << endl;
}