Take 3X12 matrix and it is represented usingsingle diemensional array.

``````input:
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  26  27  28 29   30 31  32   33 34  35

Output:
24  25  26  27  28 29   30 31  32   33 34  35
12  13  14  15 16  17   18 19  20   21 22  23
0     1   2    3    4   5     6   7     8    9   10  11

#include<stdio.h>
main()
{
int tmp,index_of_max,max_value,i,j,k,m;
int a[36]={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,26,27,28,29,30,31,32,33,34,35};
int rows=3,columns=12,tsize;
tsize=rows*columns;
int temp;
j=0;
printf("\ninput\n");
for(i=0;i<tsize;i++)
{   if(j==12)
{
j=0;
printf("\n");
}
printf("%d\t",a[i]);
j++;
}
for ( k=0;k<rows/2;k++)
{
for(  i = 0; i <columns; i++)
{
temp = a[(rows-(k+1)) * columns + i];
a[(rows-(k+1)) * columns + i] = a[k*columns + i];
a[k*columns + i] = temp;
}
}
j=0;
printf("\noutpu\n");
for(i=0;i<tsize;i++)
{   if(j==12)
{
j=0;
printf("\n");
}
printf("%d\t",a[i]);
j++;
}
getch();
}
``````

Edited by Reverend Jim: Fixed formatting

8
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by William Hemsworth
Featured Replies
• 1

[QUOTE=Tom Gunn;898070]A moderator hasn't told me that I've done anything wrong yet. Besides, the announcements are specific about homework without showing any effort. That's not the case here. The "Read this first!" even says that giving away answers is subjective. If you want me to change how I post, you …

• 1

It's not an official rule that you're not allowed to give away free solutions, so if he wants to give them away, he has that choice. He wont be given any warnings by a moderator. On the other hand, if he wants to help the OP, he shouldn't be giving …

huh?

What is your problem?

Source program must be surrounded with BB code tags: See # icon at toolbar and also read How to use bb code tags?.

If you want to treat each row as an individual object, a 2D array is easier to work with and visualize. A good way to swap rows is a second array of pointers that point into the matrix. You swap those pointers for different sorting options and the matrix doesn't ever change:

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define XSZ 3
#define YSZ 12

void PrintMatrix(int **pmat, int xsz, int ysz);
void ReverseMatrix(int **pmat, int sz);

int main()
{
int matrix[][YSZ] =
{
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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35
};
int *pmat[XSZ];
int x;

for (x = 0; x < XSZ; ++x) pmat[x] = matrix[x];

PrintMatrix(pmat, XSZ, YSZ);
printf("\n");
ReverseMatrix(pmat, XSZ);
PrintMatrix(pmat, XSZ, YSZ);

return EXIT_SUCCESS;
}

void PrintMatrix(int **pmat, int xsz, int ysz)
{
int x, y;

for (x = 0; x < xsz; ++x)
{
for (y = 0; y < ysz; ++y)
{
printf("%-3d", pmat[x][y]);
}

printf("\n");
}
}

void ReverseMatrix(int **pmat, int sz)
{
int x, y;

for (x = 0, y = sz - 1; x < y; ++x, --y)
{
int *temp = pmat[x];
pmat[x] = pmat[y];
pmat[y] = temp;
}
}``````

It also works with a 1D array like the one in your code. As long as you work with pmat instead of matrix, the only difference is how pmat is constructed:

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define XSZ 3
#define YSZ 12

void PrintMatrix(int **pmat, int xsz, int ysz);
void ReverseMatrix(int **pmat, int sz);

int main()
{
int matrix[] =
{
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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35
};
int *pmat[XSZ];
int x, k = 0;

for (x = 0; x < XSZ * YSZ; ++x)
{
if (x % YSZ == 0) pmat[k++] = &matrix[x];
}

PrintMatrix(pmat, XSZ, YSZ);
printf("\n");
ReverseMatrix(pmat, XSZ);
PrintMatrix(pmat, XSZ, YSZ);

return EXIT_SUCCESS;
}

void PrintMatrix(int **pmat, int xsz, int ysz)
{
int x, y;

for (x = 0; x < xsz; ++x)
{
for (y = 0; y < ysz; ++y)
{
printf("%-3d", pmat[x][y]);
}

printf("\n");
}
}

void ReverseMatrix(int **pmat, int sz)
{
int x, y;

for (x = 0, y = sz - 1; x < y; ++x, --y)
{
int *temp = pmat[x];
pmat[x] = pmat[y];
pmat[y] = temp;
}
}``````
Your "Good" intentions are leading the OP in a "Wrong" way !!!
if you're gonna spoonfeed entire solutions, you should at least have the decency to FedEx a DVD-ROM and spiral bound instruction manual.

A moderator hasn't told me that I've done anything wrong yet. Besides, the announcements are specific about homework without showing any effort. That's not the case here. The "Read this first!" even says that giving away answers is subjective.

If you want me to change how I post, you have to tell me exactly what I'm doing wrong and how you would do it the right way. Then convince me that your right way is better than my right way.

Now I tell you that you've done something wrong!

A moderator hasn't told me that I've done anything wrong yet. Besides, the announcements are specific about homework without showing any effort. That's not the case here. The "Read this first!" even says that giving away answers is subjective.

If you want me to change how I post, you have to tell me exactly what I'm doing wrong and how you would do it the right way. Then convince me that your right way is better than my right way.

It could be that a moderator hasn't told you, but I hope you agree with me that if you're saying that, then you're just provocating any member on this forum, are you maybe thinking that you're a God or something? Do you want to wait till the very latest moment?
You've to understand that we're just warning you.

BTW, Is it that difficult to read a block of text?

Don't give away code!

You might feel inclined to help someone else. That's great! But don't solve the problem for them. Our goal is to help people to learn, and giving away answers doesn't achieve that goal. Naturally giving away the answer is a subjective thing, so just make sure that the person you help can't just take your code and turn it in for a grade. We want the people we help to do enough work to learn something meaningful.

I marked the most important in RED, do you understand it now?

Do you want to wait till the very latest moment?

I want to be told by an authority that I'm breaking the rules, not some net nanny who just doesn't like that I'm actually helping instead of bashing people about code tags, "rusty code", and using compilers that are forced on them as if they had a choice.

You've to understand that we're just warning you.

No offense, but all I understand so far is that a bunch of people around here get off on putting others down and try to make themselves look important. I understand that there are people like that, and I have no intention of getting into a pissing match with the local good ol' boys. But I'm also not going to do what you say just because you say it. I've read the rules, and all of the links you keep giving me, and I think your interpretation is overly strict.

BTW, Is it that difficult to read a block of text?

It sure seems that way, because you missed this part in red:

Naturally giving away the answer is a subjective thing, so just make sure that the person you help can't just take your code and turn it in for a grade.

I say that he can't turn my code in for a grade because it uses two tricks with pointers that a student asking for help on this problem couldn't figure out independently.

It sure seems that way, because you missed this part in red: (well I marked it in Green, that's a color which doesn't express frustration)

Naturally giving away the answer is a subjective thing, so just make sure that the person you help can't just take your code and turn it in for a grade.

Seems like you're a bit dyslectic (but with sentences): You only read parts BEFORE the comma, if you quote someone's words, then please repeat all his words, not just a part of it, you're just an expert in taking things out of it's context.
And be sure, a moderator will tell it to you (seems like you really want it), you can sleep on both ears...

I say that he can't turn my code in for a grade because it uses two tricks with pointers that a student asking for help on this problem couldn't figure out independently.

Conclusion: You are the number one!!
Are you maybe implying that the OP is too stupid for using Google?
No one is too stupid to just Google up something, you just don't have to be too lazy! (And I'm not trying to blame the OP for this legitimate question)

Edit::

I want to be told by an authority that I'm breaking the rules, not some net nanny who just doesn't like that I'm actually helping instead of bashing people about code tags, "rusty code", and using compilers that are forced on them as if they had a choice.

If it helps: the post was written by a Super Moderator, is this "Moderator enough" for you ?

Edit:: To avoid hijacking this thread further I'm not going to continue your useless discussion...

It's not an official rule that you're not allowed to give away free solutions, so if he wants to give them away, he has that choice. He wont be given any warnings by a moderator.

On the other hand, if he wants to help the OP, he shouldn't be giving the poster full/nearly complete solutions. By doing so, he's ultimately making it harder for OP to write code independantly in the future. Also there will be the same debate on every thread he does this with, not to mention he will destroy his own reputation, so hopefully he will realize the smart thing to do now.

I believe I heard this for AD some time ago, "Give a man a fish and he will eat for a day. Teach a man to fish and he will eat for a lifetime", or something like that anyway :icon_lol: