Assignment 19
Due Wednesday, March 21 at 9:20am on Canvas


Assignment 19.1: Barnyard Animals (10 pts)

  • In this assignment we write a program that copies five numbers from an input stream to an output stream.
  • Copy the following program into a C++ file, save it as animals.cpp, and then compile and run the starter program to make sure you copied it correctly.
    #include <iostream>
    using namespace std;
    
    int main() {
        // Enter your code here
    
        return 0;
    }
    
  • Then, let's create a text file to test our program.
  • If you are using Eclipse (strongly encouraged for File I/O!), right-click the project folder and select New->File. Name your file infile.txt.
  • Make sure that you did not accidentally save your file under the src folder. If you did, you can simply drag and drop it into the main project folder.
  • If you are using another IDE, such as Xcode, you will need to research where to save your files using a Google search. The instructor does not use Xcode and cannot give you support in using Xcode or another IDE with file I/O
  • Here are the values to place in your file.
5
3
8
2
4
  • We will read from this file after writing our program.

  • Add the following new libraries that you need to your source code file:
    #include <fstream>   // for file I/O
    #include <cstdlib>   // for exit()
    
  • Inside main(), declare an input stream variable named fin and an output stream variable named fout.
  • Add code to connect each stream to a file using open() and check for failure:
  • Name your input and output files: infile.txt and outfile.txt respectively
  • Add statements to read two numbers from the input stream.
  • For example, here is possible code for reading the first number:
    int first;
    fin >> first;
    
  • Add 4 more pairs of statements like the ones above to read in the next 4 numbers.
  • Then, add an fout statement to print out a title at the top of your file
    fout << "Animals at our farm" << endl;
    
  • Next, write out the contents of your five variables to the file. For example:
fout << first << " chickens" << endl;
  • Add 4 more statements like the one above, so that your file contains the following when you are finished:
Animals at our farm
5 chickens
3 cows
8 ducks
2 horses
4 pigs
  • Close the streams when finished reading and writing:
    fin.close();
    fout.close();
    
  • Compile and run your program to make sure you made the changes correctly.
  • Notice that you do not see any output on the console screen. The output stream wrote the program output to the output file.
  • Open the output file you created and verify that your file contains the following:

    Animals at our farm
    5 chickens
    3 cows
    8 ducks
    2 horses
    4 pigs
  • Submit your program to Canvas when you are finished



Assignment 19.2: More Barnyard Animals (10 pts)

  • In this assignment we write a program that stores 5 strings in a string array, and 5 ints in an integer array and then writes the contents of the two arrays to the console.
  • Copy the following program into a C++ file, save it as animals2.cpp, and then compile and run the starter program to make sure you copied it correctly.
    #include <iostream>
    using namespace std;
    
    int main() {
        // Enter your code here
    
        return 0;
    }
    
  • At the top of main declare a string array named animals of size 5. 
  • Note that you should store the size of the array in a separate constant variable:
  • In other words, you should now have two variables like these at the top of main:

const int SIZE = 5;
string animals[SIZE];
  • Then, let's declare an integer array of size 5 named number_animals.
  • Store the following values in the string array, one at each index:
"sheep"
"donkeys"
"goats"
"geese"
"cats"
  • Remember: you can do this one of two ways:
Option 1: static initialization:

string animals[SIZE] = {"sheep", "donkeys", "goats", "geese", "cats"};

Option 2: Non-static initialization:

string animals[SIZE];
animals[0] = "sheep";
animals[1] = "donkeys";
//What goes here?

  • Pick ONE of the above options to store the string values in your string array.
  • Next, pick one option to store the following values in the integer array (number_animals), one at each index:

5
3
8
2
4
  • We are now going to write the contents of our arrays to the console.
  • First, inside of main, add the following cout statement:

    cout << "Other animals at our farm" << endl;

  • Next, write a for loop that prints out the the two arrays.
  • Your for loop should print the arrays to the console, side-by-side, like this:
5 sheep
3 donkeys
8 goats
2 geese
4 cats
  • See the class notes if you get stuck for an example of using for loops with arrays.
  • When you have written your for loop, compile and run your code and verify that the following output appears in the console window:

    Other animals at our farm
    5 sheep
    3 donkeys
    8 goats
    2 geese
    4 cats
  • Submit your program to Canvas when you are finished


Assignment 20.3: Number of Numbers (10 pts)

  • For this assignment you will be working with arrays.
  • Open a new C++ file called nums.cpp
  • Prompt a user to enter in the size of the array (the number of numbers).
  • Next, using a for loop, prompt the user to enter that many numbers, and store each one in your array until your array is at full capacity.
  • Using the same, or a second, for loop, sum the numbers and report their sum to the user.
  • Also, multiply the numbers together and report their product to the user.
  • The sum and the product should be printed to one decimal value only.
  • Your program should work identically to the sample output below:

Enter the number of numbers: 4
Enter 4 numbers:

Number 1: 3.2
Number 2: 6.7
Number 3: 2.9
Number 4: 4.6

The sum of the numbers is: 17.4
And the product is: 286.0


  • Another run of the program might give the following output:

Enter the number of numbers: 8
Enter 8 numbers:

Number 1: 2.1
Number 2: -5.6
Number 3: 9.0
Number 4: 8.7
Number 5: -2.2
Number 6: 8.2
Number 7: 9.5
Number 8: 1.4

The sum of the numbers is: 31.1
And the product is: 220931.3

  • Upload nums.cpp to Canvas when you are finished.