Hi All,
In this program I have these 2 matrices . It will compare each element in each row of matrix[4][4] to 3 . If that value <= 3 then store the characters in the first row of matrix m_c[25][32] in an 2D char array (char clusters[40][40]). For example 3 numbers (0,2,3) of the first row of matrix[4][4] less or equal 3 therefore 3 characters (A,B,D) of first row of matrix m_c[25][32] will be store in the clusters. Similarly 3 numbers (0,3,3) in second row of matrix[4][4] less or equal 3 therefore 3 characters (B,D,F) of first row of matrix m_c[25][32] will be store in the clusters.
My program doesnt work correctly and it contains some white space in the array (char clusters[40][40]).
Please help me to fix this program. It is tough for me because the program only allows to work with char array, no vector, or string.

``````#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main(int argc, char* argv[])
{
int matrix[4][4] = {{0,2,3,4},{4,0,3,3},{5, 3,0,1},{1,2,3,0}};
char m_c[25][32] = {{0,'A','B','D', 'F'},{'A',0,2,3,4},{'B',4,0,3,3},{'D',5,3,0, 1}, {'F' , 1, 2, 3, 0}};

char clusters[40][40];
for (int i = 0; i < 4; i++) {
int Mpnt = 0;
char temp[4][32];
int v = 0;
for (int j =0; j < 4; j++) {
if (matrix[i][j] <=3) {
Mpnt++;
v++;
strcpy(temp[v] , m_c[j+1]);
cout << v << " " << temp[v] << "\n";
}
}

if (Mpnt >= 2) {
for (int p = i; p < v+i; p++) {
memcpy(clusters[p], temp[p], sizeof(clusters[p]));
cout << i << " " << temp[p] << " " << clusters[p] << "\n";
}
}
}

return 0;
}``````

Thank you very much
Tony

Let me ask a question about line 10. When I see the value 0, did you want the character 0 or null or did you want a zero as in '0'?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.