简体   繁体   中英

How to print int and char together from 2d array

What I want to get is 4x4 matrix or larger and replace integers of center to characters. such as

 1   2   3   4
 5   6   7   8
 9  10  11  12
13  14  15  16

fill out numbers not by manual writing. It can be 8X8 10X10 matrix

 1   2   3   4 
 5   A   B   8
 9   B   B  12
13  14  15  16
void (int arr[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE],int nrow){

    for (int row = 0; row < nrow; row++)
    {
        for (int col = 0; col < nrow; col++)
        {
            arr[row][col] = i++;
            if (nrow / 2 == row && nrow / 2 == col)
            {
                arr[row - 1][col - 1] = 'A';
                arr[row - 1][col] = 'B';
                arr[row][col - 1] = 'B';
                arr[row][col] = 'A';
            }
        }
    }
    
    for (int row = 0; row < nrow; row++)
    {
        for (int col = 0; col < nrow; col++)
        {
            cout<< arr[row][col];
        }
        cout << endl;
    }
}

I tried to make it char by

char charArray[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE]=arr[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];

But It shows weird characters and if I do

charArray[row][col] =i +'0';

It shows me numbers before 10 and over 10 would be the same weird charaters.

Can anybody help me please?

                                                                                                      #include<iostream>
using namespace std;
int  main()
{
int arr[6]={'A','B',1,3,4,5};
for(int i=0;i<6;i++)
{
if(arr[i]>0&&arr[i]<9)
cout<<arr[i]<<"\n";
else
{
printf("%c\n",arr[i]);
}


}
return 0;
}

This will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM