I'm at my limit, I feel as if the teacher shoved way too much material for an intro course to Programming. I just started learning how to program C++ and for our team assignment we have to create a Matrix with 3 functions using 2D dynamic arrays with double pointers (automatic 0 if we use fixed arrays).
One function, MostFreqInt() has to detect the most repeated elements in the 2-dimensional array. Second function, AveMatrix(), is prompted if there's no repeated value or the most repeated value is zero. Third function, AvgRepeated() is prompt if there are equal amount of most repeated value.
Finally, to make things worst, the matrix must be able to rotate even if it's a rectangle.
I'm really at my wit's end, my team is planning to drop the course so they don't even care about the team assignment anymore. Now I'm left alone to finish it before today. The most frequent repeated Algorithm really trumps me and I have no idea what to do; I could figure out how to rotate the matrix if it was a square but not a rectangle. (If I fail my this 10% team assignment I fail my course). Should I drop the class instead of plowing through this massacre?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <array>
#include <vector>
class Matrix
{
private:
std::vector<int> rewrite;
int userinput;
unsigned int matrixrow;
unsigned int matrixcolumn;
bool confirmzero = false;
bool confirmrepeat = false; // allows the confirmation if there is a repeat
int equalrepcounter;
float sumofequals;
float AvgNum;
int AvgDivisor;
int **dMatrixPtr = NULL;
//Figure out double pointers
public:
Matrix();
void inputMatrix();
void inputLoop(unsigned int,unsigned int, int **);
float mostFreqInt();
void AvgMatrix(); //First case: Calculates average of whole matrix
float AvgRepeated(unsigned int, unsigned int, int **);//Second case: Calc
float divideMatrix();
int rotateMatrix(); //Assign new values to Matrix
void printMatrix(); //Print the Matrix to find out what's up.
void deleteMemory(); //deletes the memory before program ends.
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
//matrix.cpp
float Matrix::mostFreqInt()
{
int maxrepcounter = 0;
//rewrite into a vector
for (size_t i = 0; i < matrixrow; i++)
{
for (size_t j = 0; j < matrixcolumn; j++)
rewrite.push_back(*(*(dMatrixPtr + i) + j));
}
//sort the vector into increasing order
sort(rewrite.begin(), rewrite.end());
//iterate through loop
for (size_t i = 0; i < rewrite.size(); i++)
{
int checkcounter = 1;//Resets after every comparison
for (size_t j = i + 1; j < rewrite.size(); j++)
if (rewrite[i] == rewrite[j])
{
checkcounter++;
if (checkcounter > maxrepcounter)
{
//confirmrepeat = false;
maxrepcounter = checkcounter;
if (!rewrite[j] == 0)
{
AvgNum = rewrite[j];
confirmzero = false;
}
}
if (checkcounter == maxrepcounter)
{
//confirmrepeat = true;
equalrepcounter = checkcounter;
AvgNum = AvgRepeated(matrixrow, matrixcolumn, dMatrixPtr);
}
}
}
std::cout << AvgNum;
return AvgNum;
}
void Matrix::AvgMatrix()
{
int sumofmatrix = 0;
for (int sum : rewrite)
{
sumofmatrix += sum;
}
AvgNum = sumofmatrix / rewrite.size();
}
float Matrix::AvgRepeated(unsigned int row, unsigned int column, int **dMatrix)
{
for (size_t i = 0; i < rewrite.size(); i++)
{
int checkcounter = 1;//Resets after every comparison
for (size_t j = i + 1; j < rewrite.size(); j++)
{
if (rewrite[i] == rewrite[j])
{
checkcounter++;
if (checkcounter == equalrepcounter)
sumofequals += rewrite[i] * equalrepcounter;
}
}
AvgNum = sumofequals / equalrepcounter;
return AvgNum;
}
}
|
various failure attempts on debugging my program:
input :
1 2 3
3 3 3
4 5 6
Output of average: 0
input:
4 4 4
4 4 4
4 4 4
Output of Average: 19.556
|
In my main, I test the MostFreq function, and despite having
return AvgNum
, I have to put
std::cout << AvgNum;
in order to actually get the AvgNum. How is that even possible? Doesn't
return
display the value usually?