Starting from:

$25

CPSC1021 - Lab5pre tar - Solved

 

 Objective

●       Begin programming in C++.

●       Exposure to Version Control (GitHub).

●       Exposure to Vectors and ifstream.  Introduction

C++ is a powerful, modern Object-Oriented Programming (OOP) language. In this lab, we will make the transition to programming in C++. We will utilize a data structure called a Vector in this lab. You likely haven’t covered Vectors in lecture yet; however, we are going to utilize them in this lab due to the power of vectors.

In this lab, we will parse through a file that has 10,000 integers. You will store them into a vector and find the average of all the numbers.

The last portion of this lab will expose you to a version control system called Git. In this lab, we will utilize GitHub for our version-controlled repo; however, there are various other options available you may hear of. Many software engineering firms will ask you to share your GitHub link during interviews, so this is a great way to showcase your skills. I encourage you to continue to use GitHub outside of this lab, as knowing Git is an extremely valuable skillset and it prevents data loss.

Requirements:

-          You must fill the vector using emplace_back;

-          You must use a ranged for-loop (refer to lecture slides) -     You must open the file using ofstream. - You must print the average to the terminal - You must write a makefile for this lab.

Part 1: Opening a File in C++

In C, we utilized a File pointer (FILE *) to open files. In C++, we utilize the fstream library and create an object with either ifstream or ofstream. Ifstream should be utilized in this lab, as it will allow you to read from “numbers.txt”. 


Vectors

Vectors are dynamic arrays. Remember in C, where we either needed to know the size of the array or have some input for the array when we initialized it? There was no way to re-size the array without creating a separate array and copying values into it, but this is extremely difficult to do dynamically.

Vectors in C++ are dynamic arrays. Technically, they are a class, which means we have certain methods we can call on the vector. Here are a few:

-          .emplace_back(<data here>)

o   This allows us to put as much data into the vector as we need. If it is full, it will resize automatically. You will learn about this in greater detail, but this is the method you want to call in this lab to store data inside of your vector.

-          .reserve(<integer here>)

o   Sometimes we know how large our vector is, like in this lab. It is more efficient to go ahead and reserve the size of vector when the size is known.

-          .size() o Remember in C how we had to keep a separate integer to remember the size of an array? The .size() method gives us an option to know the size of the vector at any time.

There are a lot more methods we can call; however, they are beyond the scope of this lab. Click here to learn more about vectors if you’re interested. Compiling C++ Programs

For the most part, you compile and run C++ programs similarly to C. Instead of gcc, we will use g++ to compile. When compiling in C++, I recommend adding the -Weffc++. It is utilizing Scott Meyer’s techniques from Effective Modern C++14, which will ensure you programming utilizing best practices.

Example:

To compile:

-          g++ <name_of_file>.cpp -Weffc++ -Wall -std=c++11 To run:

-          ./a.out


More products