C problem with matrix

Hi.
I know that site is dedicated C++ programming, but i have some misunderstanding with a c problem.
How i tried to do it :
Let,s suppose that i have : m=4, n=4 and k=2 :
The matrix is : M(0,0) M(0,1) M(0,2) M(0,3)
M(1,0) M(1,1) M(1,2) M(1,3)
M(2,0) M(2,1) M(2,2) M(2,3)
M(3,0) M(3,1) M(3,2) M(3,3)

The resulting matrix should be :

M(0,2) M(0,3) M(0,0) M(0,1)
M(1,2) M(1,3) M(1,0) M(1,1)
M(2,2) M(2,3) M(2,0) M(2,1)
M(3,2) M(3,3) M(3,0) M(3,1)

(*ptr+i)+j+k=a;

Consider a table of size m*n and a number k (1<=k<=n). Write a program that transfers the first k columns at the end of table, without using other data structures.


#include<stdio.h>
#define p 8
#define q 8

int main()
{
int *ptr, a, m, n, i, j, k, t, A[p][q];
ptr=&A;
printf("\n Give the number of lines m = ");
scanf("%d", &m);
printf("\n Give the numbers of columns n = ");
scanf("%d", &n);
NEXT: ;
printf("\n Give a number k = ");
scanf("%d", &k);
if(k>=n)
printf("\n The number k must be less than the number of columns n!\n");
else
goto PREVIOUS;
goto NEXT;
PREVIOUS: ;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("\n A[%d][%d] = ", i+1, j+1);
scanf("%d", &A[i][j]);
}
}
printf("\n Matrix associated : \n\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("\t %d ", A[i][j]);
printf("\n\n");
}
t=k;
while(t)
{
for(j=0; j<n; i++)
{
for(i=0; i<m; i++)
{
a=(*ptr+i)+j; //a variable receive the value of the first column
(*ptr+i)+j=(*ptr+i)+j+k-1; //the first column receive the value of the second column
(*ptr+i)+j+k=a; // the last column receive the value of the a variable.
printf("\t %d ", A[i][j]);
t--;
}
}
}
}
I see this is your first post, welcome. But from now on, please:
1) Use Code tags (look for the <> button on the right when posting).
2) Be brief.
3) Ask specific questions.
4) Don't ask for full solutions to your homework.

So let's try this again: what is the problem you have, exactly?

Edit: typos, layout.
Last edited on
Your task can be simply done with using standard algorithm std::rotate. But I am not sure that you are allowed to use standard algorithms. In any case do not use the goto statement. You shall forget that there is such statement in C/C++.
Topic archived. No new replies allowed.