简体   繁体   中英

How to print backwards column of matrix represented by array in C?

I am working on program that takes matrix from input file like this. where first row represents parameters of matrix - rows,cols,0 for changing odd cols and 1 for changing even cols

5 5 0
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

and now I have to take odd clomuns (example: 7 4 1) and print it backwards so it will look like this ( 1 4 7 ) if I had 3x3 matrix it would looks like:

1 2 3
4 5 6
7 8 9

output matrix with odd cols printed backwards

7 2 9
4 5 6
1 8 3

and this is code I have so far

int r = 0, c = 0, odds = 0,n = 0,i,j;
FILE *fp,*fp2;
fp = fopen(argv[1],"r");
fp2 = fopen(argv[2],"w");
if(!fp){
    printf("file doesnt exist\n");
}
if(!fp2){
    printf("file doesnt exist\n");
}
fscanf(fp,"%d %d %d", &r, &c, &odds);
n = r *c;
int* matrix= (int*)malloc(r*c* sizeof(int));

for(int i = 0; i < n; i++)
{
    fscanf(fp,"%d",&matrix[i]);
}

if(odds == 0){
for(int i = 0; i < n; i++){

    if(i%2==0){
    matrix[i] = i;
    }
}
}else if(odds == 1){
   for(int i = 0; i < n; i++){

    if(i%2!=0){
    matrix[i] =  ;
    }
} 
}




for(i = 0;i < n; i++)
{    
        if(i % s == 0 ){
        fprintf(fp2,"\n");
        }
        fprintf(fp2,"%d ",matrix[i]);



}
    fclose(fp);
    fclose(fp2);
return 0;

}

and my problem is with backwarding the cols, which is supposed to happen here

if(odds == 0){
for(int i = 0; i < n; i++){

    if(i%2==0){
    matrix[i] = i;
    }
}
}else if(odds == 1){
   for(int i = 0; i < n; i++){

    if(i%2!=0){
    matrix[i] =  ;
    }
} 
}

1st if is for printing even cols backwards and 2nd is for odd cols and as you can see the matrix is in my program represented by normal array which wasn't my idea, but teachers, so its supposed to work like this

1 2 3|4 5 6|7 8 9  ----> 7 2 9|4 5 6|1 8 3

ok I just found out about map indexing, so now each position in array is presented as matrix[j+(i*r)] so for example 1st position in the 3x3 matrix above would be something like this: matrix[1+(0*3)], 4th pos would be matrix[1+(1*3)] etc...

So now my question is how to index the opposite postion in the column.

code update:

for(int i = 0; i < r; i++){

    for(int j = 1; j < c; j++){
        if(i%2!=0){
            matrix[j+i*r] = ....;
    }

}
}

In both instances you want to replace matrix[i] with an element further up in the matrix.

n n
1 2 3
4 5 6
7 8 9

In this example for replacing odd values, 1 and 7 should be switched. The difference in the indices is 6. However this is dependent on the matrix dimensions. The general formula would be:

n^2 - n - r*(2n) + i

where r is the row you are on. Since you are flipping the matrix you only have to do rows up to (nn%2)/2.

Here is some python code to clarify

for i in range(0,int((matrixdim-matrixdim%2)/2)*matrixdim):     
    if ((i-r)%2) == 0:
        temp = matrix[i]
        matrix[i] = matrix[matrixdim*matrixdim - matrixdim - r*2*matrixdim + i]
        matrix[matrixdim*matrixdim - matrixdim - r*2*matrixdim + i] = temp

    if ((i+1)%matrixdim) == 0:
        r += 1

Using i,j notation

for i in range(0,int((matrixdim-matrixdim%2)/2)):
    for j in range(0, matrixdim):
        pos = j + matrixdim*i
        temp = matrix[pos]

        if j%2 == even:
            matrix[pos] = matrix[matrixdim*matrixdim - matrixdim - i*2*matrixdim + pos]
            matrix[matrixdim*matrixdim - matrixdim - i*2*matrixdim + pos] = temp 

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