int array to char array
Feb 20, 2017 at 3:11am UTC
How would I convert an int array to a char array? Basically, instead of it printing a value, i was s3 to be "D", s2 to be "V" and s1 to be "H"
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
void initMatrix(int m[SIZE][SIZE], string s1, string s2, int gap)
{
int r,c; // ROW and COLUMN
int numRows;
int numCols;
int gapscore = -2;
int vals1 =0;
int vals2 =0;
int vals3 = 0;
int maxscore =0;
int scoret;
// plus 1 because matrix has extra row/column
numRows = s1.length() + 1;
numCols = s2.length() + 1;
m[0][0]= 0; // initialize the value at (0,0) to 0
// initialize first row
for (r=1;r<numRows;r++)
{
m[r][0] = gap * r;
}
// initialize first column
for (c=1;c<numCols;c++)
{
m[0][c] = gap * c;
}
// Initialize the rest of the matrix...
for (int a=1;a<numRows;a++)
{
vals2 = (m [(a-1)][0] + gapscore );
m[a][0] = vals2;
}
for (int b=1;b<numCols;b++)
{
vals1 =(m[0][(b-1)] + gapscore);
m[0][b] = vals1;
}
for (int a=1;a<numRows;a++)
{
for (int b=1;b<numCols;b++)
{
scoret= score(s1[a],s2[b]);
vals3= scoret + m[(a-1)][(b-1)];
vals2 = (m[(a-1)][b]+ gapscore);
vals1= (m[a][(b-1)] + gapscore);
maxscore = vals3;
if (maxscore < vals1)
{
maxscore =vals1;
}
if (maxscore < vals2)
{
maxscore = vals2;
}
m[a][b] = maxscore;
}}
cout<<"The total score is " <<maxscore<<endl;
Feb 20, 2017 at 3:21am UTC
You can apply an ASCII value to an int, say
int x = 65;
Then you assign the value stored in memory to a char
char ch = x;
A
Then
cout << ch << endl;
would print
It works the other way around also.
Last edited on Feb 20, 2017 at 3:21am UTC
Feb 20, 2017 at 3:30am UTC
What if I couldn't assign a specific value to the integer, since it is based on an specific input? Is there a way to convert a matrix of numbers into a matrix of letters?
Feb 20, 2017 at 3:58am UTC
You could loop over the data and convert,
OR you could cast it.
1 2 3 4 5 6 7 8 9
int main()
{
int x = 65;
char ch = 'A' ;
cout << (char *) &x << endl;
cout << (int ) ch << endl;
}
Topic archived. No new replies allowed.