Formatting a table in an array

so i need to format a table into an array with targets candidates and the score. the targets and candidates holding six preset digits and the score being the difference such as, if the target pattern is 242234 and the candidate pattern is 342121, the candidate pattern ;would be given a score of 6: score = abs(2 − 3) + abs(4 − 4) + abs(2 − 2) + abs(2 − 1) + abs(3 − 2) + abs(4 − 1) = 6. Note that the score is the sum of the absolute values of the differences between each pair of digits in the pattern, and is not just the difference between the numbers. and im not sure how to get the numbers into the right spot of the table




#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>

using namespace std;

void readit();
void calcit();
void writeit();
int targts[3][6], candidtes[10][6], cnt = 0, scre[3][10], arrayvar,
bestfit[3];

ifstream infile("C:\\EGR 111\\Targets.txt");
ifstream infile2("C:\\EGR 111\\Candidates.txt");


int main()
{
readit();
calcit();
writeit();
return 0;
}

void readit ()
{
while (!infile.eof())
{
infile >> arrayvar;

for (int i = 0; i < 6; i++)
{
targts[cnt][i] = arrayvar % 10;
arrayvar = arrayvar / 10;
}
cnt++;
}

cnt = 0;

while (!infile2.eof())
{
infile2 >> arrayvar;

for (int i = 0; i < 6; i++)
{
candidtes[cnt][i] = arrayvar % 10;
arrayvar = arrayvar / 10;
}
cnt++;
}
}

void calcit ()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 6; k++)
{
scre[i][j] += abs(targts[i][k] - candidtes[j][k]);
}

if (j == 0)
{
bestfit[i] = scre[i][j];
}

if (bestfit[i] > scre[i][j])
{
bestfit[i] = scre[i][j];
}
}
}
}

void writeit ()
{
for (int i = 0; i < 3; i++)
{
cout << "Target" << i+1 << endl;
cout << "Target Candidate Score" << endl;
cout << "==============================" << endl;
cout << left << fixed << setw (11) << "Target" << setw (11)
<< "Candidate" << setw (11) << "Score" << endl;

for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 6; k++)
{
cout << targts[i][k] << candidtes[j][k] << scre[i][k] << endl;
}
}

cout << "The Candidate that best fits the Target was number: "
<< bestfit[i] << endl;

}
}
Last edited on
Topic archived. No new replies allowed.