two-dimensional array

J

John

Dear experts,
I selected this problem among off messages of this page:
Daclare a two-dimensional array of size 10 by 10 and read 10
integers into a[0][0] to a[0][9].then repeat the insertion >operation
specified by an integer sequence about 9 times, store >the results from
a[1] to a[9].
Finally,print the array.


I tried as I was able,but the output of my solution is not satisfying
the said matter in the problem.if possible,please show the correct
method.thank you!

The solution is as follows:

#include <stdio.h>

int main()
{
int a[10][10];
int i,j,k;
int x;
for(i=0; i<10; i++){
for(j=0; j<10; j++) a[j]=j+1;

}

for(i=0; i<10; i++){
for(j=0; j<10; j++){
x= a[j];
a[j]=a[j];
a[j]=x;
for(k=0; k<10; k++)printf("%d", a[j][k]);
printf("\n");
}
printf("\n");
}
return 0;
}
 
M

Michael Mair

John said:
Dear experts,
I selected this problem among off messages of this page:

Daclare a two-dimensional array of size 10 by 10 and read 10
integers into a[0][0] to a[0][9].then repeat the insertion >operation

specified by an integer sequence about 9 times, store >the results from
a[1] to a[9].
Finally,print the array.

First: State the full exercise. I do not know in the least
what the "insertion operation" is.

I tried as I was able,but the output of my solution is not satisfying
the said matter in the problem.if possible,please show the correct
method.thank you!

The solution is as follows:

#include <stdio.h>

int main()
{
int a[10][10];
int i,j,k;
int x;
for(i=0; i<10; i++){
for(j=0; j<10; j++) a[j]=j+1;

}

for(i=0; i<10; i++){
for(j=0; j<10; j++){
x= a[j];
a[j]=a[j];
a[j]=x;
for(k=0; k<10; k++)printf("%d", a[j][k]);
printf("\n");
}
printf("\n");
}
return 0;
}


Second: Please format your code in a readable way and state
where the first erroneous behaviour/output creeps in (in your
opinion.


Try this version and state whether it does what you think it
should do or whether I completely misunderstood you. Note that
I use 100 distinct values in order to be able to track what
happens.

My guess is that this is not at all what is intended.

#include <stdio.h>

int main (void)
{
int a[10][10];
int i, j, k;
int x;

/* Get 10*10 _distinct_ values */
k = 0;
for(i=0; i<10; i++)
for(j=0; j<10; j++)
a[j] = k++;

for(i=0; i<10; i++){
for(j=0; j<10; j++){
x= a[j];
/* What do you want to do here?
** 1. Exchange a[j] and a[j]?
** 2. Exchange a[j] and a[j+1]?
** 3. Something else?
** I will settle for 2. In order to not
** produce a violation of array bounds,
** we need to either run with j from 0 to 8
** or work within the remainders modulo 10.
*/
a[j] = a[(j+1)%10];
a[(j+1)%10] = x;
/* WTF are you trying to output here? I will
** go for the i-th "row" */
for(k=0; k<10; k++)
printf("%3d\t", a[k]);
printf("\n");
}
printf("\n");
}
return 0;
}


Please take to heart the two things mentioned at "First"
and "Second" -- otherwise many people will just not bother
to deal with your problem as it is too much effort to find
out what you want and read what you have and find the source
of error. I certainly will not respond to badly and
incompletely phrased problems with badly formatted code
coming from you.

BTW: My guess is that the problem wants you to rotate left
the contents of a[....] and store the result in
a[i+1][....]. This would require i and j to run from
0 to 9 and a modification of the above triangle exchange
to two assignments.


Cheers
Michael
 
M

Michael Mair

Michael said:
Dear experts,
I selected this problem among off messages of this page:

Daclare a two-dimensional array of size 10 by 10 and read 10
integers into a[0][0] to a[0][9].then repeat the insertion >operation


specified by an integer sequence about 9 times, store >the results from
a[1] to a[9].
Finally,print the array.


First: State the full exercise. I do not know in the least
what the "insertion operation" is.

I tried as I was able,but the output of my solution is not satisfying
the said matter in the problem.if possible,please show the correct
method.thank you!

The solution is as follows:

#include <stdio.h>

int main()
{
int a[10][10];
int i,j,k;
int x;
for(i=0; i<10; i++){
for(j=0; j<10; j++) a[j]=j+1;

}

for(i=0; i<10; i++){
for(j=0; j<10; j++){
x= a[j];
a[j]=a[j];
a[j]=x;
for(k=0; k<10; k++)printf("%d", a[j][k]);
printf("\n");
}
printf("\n");
}
return 0;
}



Second: Please format your code in a readable way and state
where the first erroneous behaviour/output creeps in (in your
opinion.


Try this version and state whether it does what you think it
should do or whether I completely misunderstood you. Note that
I use 100 distinct values in order to be able to track what
happens.

My guess is that this is not at all what is intended.

#include <stdio.h>

int main (void)
{
int a[10][10];
int i, j, k;
int x;

/* Get 10*10 _distinct_ values */
k = 0;
for(i=0; i<10; i++)
for(j=0; j<10; j++)
a[j] = k++;

for(i=0; i<10; i++){
for(j=0; j<10; j++){
x= a[j];
/* What do you want to do here?
** 1. Exchange a[j] and a[j]?
** 2. Exchange a[j] and a[j+1]?
** 3. Something else?
** I will settle for 2. In order to not
** produce a violation of array bounds,
** we need to either run with j from 0 to 8
** or work within the remainders modulo 10.
*/
a[j] = a[(j+1)%10];
a[(j+1)%10] = x;
/* WTF are you trying to output here? I will
** go for the i-th "row" */
for(k=0; k<10; k++)
printf("%3d\t", a[k]);
printf("\n");
}
printf("\n");
}
return 0;
}


Please take to heart the two things mentioned at "First"
and "Second" -- otherwise many people will just not bother
to deal with your problem as it is too much effort to find
out what you want and read what you have and find the source
of error. I certainly will not respond to badly and
incompletely phrased problems with badly formatted code
coming from you.

BTW: My guess is that the problem wants you to rotate left
the contents of a[....] and store the result in
a[i+1][....]. This would require i and j to run from
0 to 9 and a modification of the above triangle exchange 0 to 8, actually
to two assignments.


Cheers
Michael
 
J

John

Dear Mr.Michael,
Thnak you for your consideration.
of course,the main aim of to solve this problem is to get an output as:

1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
1 2 3 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10
3 2 4 1 5 6 7 8 9 10
2 4 1 5 3 6 7 8 9 10
4 1 5 3 6 2 7 8 9 10
1 5 3 6 2 7 4 8 9 10
5 3 6 2 7 4 8 1 9 10
3 6 2 7 4 8 1 9 5 10
6 2 7 4 8 1 9 5 10 3
 
M

Michael Mair

John said:
Dear Mr.Michael,
Thnak you for your consideration.
of course,the main aim of to solve this problem is to get an output as:

1 2 3 4 5 6 7 8 9 10
2
3
4
5
6
7
8
9
10
1 2 3 4 5 6 7 8 9 10
2 1 3 4 5 6 7 8 9 10 1 3 2 4 5 6 7 8 9 10
3 2 4 1 5 6 7 8 9 10
2 4 1 5 3 6 7 8 9 10
4 1 5 3 6 2 7 8 9 10
1 5 3 6 2 7 4 8 9 10
5 3 6 2 7 4 8 1 9 10
3 6 2 7 4 8 1 9 5 10
6 2 7 4 8 1 9 5 10 3

So, the exercise is:
For the rows i=1..9 copy the row i-1 to row i and rotate the
array elements a[j] to the left for j = 0..i where rotate
means that the old a[j] goes to the new a[j-1] for j>0
and that the old a[0] goes to the new a.

Try to phrase the problem in clear words. The missing line
in the desired output made this once more guesswork.


Taking the above assignment word by word, we arrive at:

#include <stdio.h>

int main (void)
{
int a[10][10];
int i, j;
int x;

/* Initialise a[0][j] with j+1 */
for(j=0; j<10; j++)
a[0][j] = j+1;

for(j=0; j<10; j++)
printf("%3d\t", a[0][j]);
printf("\n");

for (i=1; i<10; i++){
for (j=0; j<10; j++) {
a[j] = a[i-1][j];
}
for(j=0; j<i; j++){
x= a[j];
a[j] = a[(j+1)%(i+1)];
a[(j+1)%(i+1)] = x;
}
for(j=0; j<10; j++)
printf("%3d\t", a[j]);
printf("\n");
}
return 0;
}


If we see that we do not really have to separate copying and
rotating, we can replace the body of the row loop by

for (i=1; i<10; i++){
for(j=1; j<=i; j++) { /* Copy to the left */
a[j-1] = a[i-1][j];
}
a = a[i-1][0]; /* "rotate" modulo i+1 */
for (j=i+1; j<10; j++) { /* Copy the rest */
a[j] = a[i-1][j];
}
for(j=0; j<10; j++)
printf("%3d\t", a[j]);
printf("\n");
}


Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top