For this computer assignment, you are to write a C++ program to generate random integers in the range LOW = 1
, HIGH = 10000
and to store them in a vector<int> v
of size VEC_SIZE = 200
. Then, sort the contents of the vector (in ascending order) and display it on stdout.
The program is partially implemented. You can obtain the source file assignment01.cc
within this Git repository.
To sort the contents of a vector, use the sort()
function in the STL. In addition to the main()
routine, include the following subroutines in your program:
-
void genRndNums(vector<int> &v)
It generatesVEC_SIZE
integers and stores them invector v
. Initializes the random number generator (RNG) by calling the functionsrand (SEED)
with the seed valueSEED = 1
and generates random integers by calling the functionrand()
. -
void printVec(const vector<int> &v)
It displays the contents ofvector v
on stdout, printing exactlyNO_ITEMS = 12
numbers in a single line, except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each number, allocateITEM_W = 5
spaces on stdout.
Programming Notes:
-
Do not change existing implementation in
assignment01.cc
. But you need to include any necessary headers and add necessary global constants. -
Your source file are named
assignment01.cc
and your header file isassignment01.h
. Guard the statements in your header file using the following format. (This is necessary because you don’t want the statements in a header file processed more than once.)
#ifndef H_ASSIGNMENT01 // should not be defined any place else
#define H_ASSIGNMENT01 // same const value as for ifndef directive
// put all statements for your header file here
#endif
- Include all system header files (that you need in your program) in your header file. For example, to gain access to the
iostream
library, which defines a set of simple I/O operations, insert the line#include <iostream>
in your header file, to gain access the I/O manipulators such assetw
, insert the line#include <iomanip>
in your header file; to access the vector container in the STL, insert the line#include <vector>
in your header file; and to use the functions in the STL such as sort, insert the line#include <algorithm>
in your header file. At the top ofassignment01.cc
, we have inserted your header file#include “assignment01.h”
.
-
You are not allowed to use any I/O functions from the C library, such as
scanf
orprintf
. Instead, use the I/O functions from the C++ library, such ascin
orcout
. -
Add documentation to your source file (e.g. https://google.github.io/styleguide/cppguide.html#Comments).
-
We have included a
Makefile
for your reference, in future assignments you will need to build your own. To compile your code just typemake
in the project directory. To test your program, execute./assignment01.exe &> assignment01.out
, which will put the output (including any error messages) in fileassignment01.out
. You can find the correct output of this program in fileresults01.out
, you can compare the two files by usingdiff
on turing or hopper,diff assignment01.out results01.out
, if the files are the same there will be no output fromdiff
.
This semester you will need to make at least three commits to your local repository and at least one push to your master repository for each assignment. This will show the evolution of your assignments over time and the thought process behind the code. You will need to make sure your final push to your master repository is completed before the assignment is due (the system time stamps actions), if the assignment is late the TA will mark off points.
When your program is ready for grading, follow Assignment Submission Instructions.