开发者

Not giving the right output

The program should create a 2D table 8*8 which consists o random number<3

it should print that table.

Another task is to translate this table into another

For Example

120

210

111

The number in the center should be changed to the sum of all numbers around it 1+2+0+2+0+1+1+1=8

and that should be done for everything; then the program should be printed

if there are any numbers larger than 9 it shoul be translated to hexadecimal..... I didn't do the hexadecimal yet. but it is still not working ....

#include <stdio.h>  
#include <stdlib.h>  
#define cols 8   
#define rows 8  
void printA(int A[][cols]);  
void printC(char C[][cols]);  
void SumThemUp(int A[][cols], char C[][cols]);  
int main()  
{  
   srand(time(NULL));  
   int A[rows][cols];  
   char C[rows][cols];  
   int i, j;  
   for(i=0; i<rows; i++)  
       for(j=0; j<cols; j++)  
   A[i][j]=rand()%3;  
   printA(A);  
   SumThemUp(A,C);  
   printC(C);  
    return 0;  
}

void printA(int A[][cols])  
{   int  i, j;  
    for(i=0;i<rows;i++)  
        {for(j=0;j<cols; j++)  
    {printf("%d ", A[i][j]);}  
    printf("\n");}  
    return ;  
}  
void printC(char C[][cols])  
{  
     int  i, j;  
    for(i=0;i<rows;i++)  
         {for(j=0;j<cols; j++)  
    {printf("%ch ", C[i][j]);}  
    printf("\n");}  
    return ;  
}  
void SumThemUp(int A[][cols], char C[][cols])  
{  
    int i,j;  
       for(i=0;i<开发者_Go百科rows;i++)  
           {for(j=0;j<cols; j++)  
    C[i][j]=0;}  
    for(i=0;i<rows;i++)  
       {for(j=0;j<cols; j++)  
    A[i][j]=C[i++][j];  
       }  
    for(j=0;j<cols; j++)  
       {for(i=0;i<rows;i++)  
       C[i][j]+=A[i][j++];  
       }return;  
}


So - I'm not entirely sure I know what you want the output to be -- but there are several problems with what you have:

0: For your arrays, the names should describe what the array actually holds, A and C are quite ambiguous.

1: Use { } for scoping, and put the { } on their own lines. (Maybe it just pasted poorly in Stack Overflow)

2: You have a set of loops which basically sets everything in C to 0:

for(i=0;i<rows;i++)  
{
   for(j=0;j<cols; j++)
   {
      C[i][j]=0;
   }
}

Then immediately after that you have:

for(i=0;i<rows;i++)
{
    for(j=0;j<cols; j++)
    {  
         A[i][j]=C[i++][j]; // <--- problem here
    }
}

So after that, both A and C are full of all 0s. On top of that, you have i++ inline when accessing columns in C. This actually changes the value that your for loop is using, so i is getting incremented for every row and every column. Presumably you want:

A[i][j]=C[i+1][j];

3: You have a similar problem here:

for(j=0;j<cols; j++)
{
   for(i=0;i<rows;i++) 
   {
      C[i][j]+=A[i][j++]; // Presumably you want j+1
   }
}

4: Why are you using a char array for C? If it's holding the sum of integers it should probably be declared int. If this was your idea of printing the ints as hex (or just plain ints), it would be easier to simply use printf to output the ints as hex:

// use %d to print the integer "normally" (base 10)
// use %x if you want a hex value with lowercase letters
// use %X if you want a hex value with capital letters
printf("125 as hex is: 0x%x", 125); // 0x7d 

I hope that points you in the right direction.

-- Dan


Do I understand correctly, that given matrix A, you want to get matrix C in SumThemUp, where each cell in C is a sum of its adjacent cells? In that case, these lines look suspicious as you modify the loop counters

A[i][j]=C[i++][j];

and

C[i][j]+=A[i][j++];

.

Anyway, a simple example, how I would do the summing part. NB! Note that I use int type for matrix C. Given that you want to convert it to hex and you happend to have values 3 in all adjacent cells somewhere, you get decimal value of 3 * 8 = 24, which requires more than one character to represent. Thus, you should convert to hex during printing. (I understand that char can contain intergral values up to 255 also, but for the sake of consistency)

void SumThemUp(int A[][cols], int C[][cols]) {
  int i, j, di, dj, i2, j2;
  // iterate through all the rows
  for (i=0 ; i<rows ; ++i) {
    for (j=0 ; j<cols ; ++j) {
      // initialize the cell to zero
      C[i][j] = 0;
      // iterate over nearby cells
      for (di=-1 ; di<=1 ; ++di) {
        for (dj=-1 ; dj<=1 ; ++dj) {
          // do not count in the center
          if (di == 0 && dj == 0) {
            continue;
          }
          // make sure, we do not try to count in cells
          // outside the matrix
          i2 = i + di;
          j2 = j + di;
          if (i2 < 0 || j2 < 0 || i2 >= rows || j2 >= cols) {
            continue;
          }
          // append the score here
          C[i][j] += A[i2][j2];
        }
      }
    }
  }
}

Also, I did not test this code, so it may contain mistakes, but maybe it helps you finishing your summing part.

NB! And take note of comments of @Dan.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜