#include <iostream> #include <fstream> #include <iomanip> using namespace std; /
ID: 1856892 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// DO NOT ALTER THESE CONSTANTS!
static const string DATAFILE = "array_data.txt";
static const int DIM = 10;
//===============================
/**
* INSTRUCTIONS
* Complete each function in order, beginning with fillArray. Make sure your program compiles
* before moving on to the next function! DO NOT MODIFY THE MAIN() FUNCTION.
*
* SAMPLE OUTPUT
Random array
7 49 73 58 30 72 44 78 23 9
40 65 92 42 87 3 27 29 40 12
3 69 9 57 60 33 99 78 16 35
97 26 12 67 10 33 79 49 79 21
67 72 93 36 85 45 28 91 94 57
1 53 8 44 68 90 24 96 30 3
22 66 49 24 1 53 77 8 28 33
98 81 35 13 65 14 63 36 25 69
15 94 29 1 17 95 5 4 51 98
88 23 5 82 52 66 16 37 38 44
Transposed and multiplied array
35 1225 1825 1450 750 1800 1100 1950 575 225
1000 325 2300 1050 2175 75 675 725 1000 300
75 1725 45 1425 1500 825 2475 1950 400 875
2425 650 300 335 250 825 1975 1225 1975 525
1675 1800 2325 900 425 1125 700 2275 2350 1425
25 1325 200 1100 1700 450 600 2400 750 75
550 1650 1225 600 25 1325 385 200 700 825
2450 2025 875 325 1625 350 1575 180 625 1725
375 2350 725 25 425 2375 125 100 255 2450
2200 575 125 2050 1300 1650 400 925 950 220
*/
/**
* FUNCTION #1 - 10 points
* This function should fill the 2D array with random numbers between 0 and 100
* The numbers must be different every time the program is run, so you need to
* research the best way to generate random numbers in this range.
*/
void fillArray(int arrayData[DIM][DIM]){
}
/**
* FUNCTION #2 - 10 points
* Print the given array in matrix form with sufficient spacing between numbers
* so that the columns line up. The message should be printed at the top of the matrix.
* See the sample output at the top of this file.
*/
void printArray(string message, int arrayData[DIM][DIM]){
}
/**
* FUNCTION #3 - 10 points
* Write the given matrix to a file named by the fileName parameter. The file
* should be written in matrix format (only numbers - no other text or bars)
*/
void writeFile(string fileName, int arrayData[DIM][DIM]){
}
/**
* FUNCTION #4 - 10 points
* Read the file named by the fileName parameter into the
* 2D array arrayData. You can assume that the file is formatted
* correctly and contains integers.
*/
void readFileIntoArray(string fileName, int arrayData[DIM][DIM]) {
}
/**
* FUNCTION #5 - 10 points
* Transpose the input array, multiplying each value by the supplied multiplier
*/
void transposeAndMultiplyArray(int arrayData[DIM][DIM], int multiplier){
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int maxp = 100;
struct ptype
{
string first, last;
string dept;
double salary;
};
const ptype initrec = {"firstname", "lastname", "dept", 0.0};
void initem(ptype p[], int &nump)
{
int i;
for (i=0; i<maxp; i++) p[i] = initrec;
nump = 0;
}
void printem(ptype p[], int nump, ofstream &outf)
{
int i;
string dummy;
for(i = 0; i<nump; i++)
{
dummy = p[i].last + ", " + p[i].first;
outf << left << setw(25) << dummy << setw(20)<< p[i].dept << right
<< setw(12) << p[i].salary << endl;
}
}
void getaverage(ptype p[], int nump, ofstream &outf)
{
int i;
double average=0;
for (i=0; i<nump; i++) average += p[i].salary;
average /= nump;
outf << " The average salary is "<< average << endl << endl;
}
void swapem(ptype &a, ptype &b)
{
ptype temp;
temp = a;
a = b;
b = temp;
}
void sortem(ptype p[], int nump)
{
int i, j;
for (j=0; j<nump-1; j++)
for (i=0; i<nump-1; i++)
if (p[i].dept > p[i+1].dept) swapem(p[i],p[i+1]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.