Confusion on output

E

engartte

hi all,
I did the solution on the following question,but no desired output is
acheived.if you know some way on,please show me with its comments.
-----------------------------------------------------
(Declare a two-dimensional array"a" of size 10 by 10 and
read 10 integers into a[0][0] to a[0][9].Then repeat the
insertion specified by an integer sequence nine times,and
store the results from a[1] to a[9].Finally,print the array.
Output Example:
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)
-------------------------------------------------------

I did as :

#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<i+1; 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;
}


but,the output is compeletly different.
tanx in advance
 
N

Neo

engartte said:
hi all,
I did the solution on the following question,but no desired output is
acheived.if you know some way on,please show me with its comments.
-----------------------------------------------------
(Declare a two-dimensional array"a" of size 10 by 10 and
read 10 integers into a[0][0] to a[0][9].Then repeat the
insertion specified by an integer sequence nine times,and
store the results from a[1] to a[9].Finally,print the array.
Output Example:
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)
-------------------------------------------------------

I did as :

#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<i+1; 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;
}


but,the output is compeletly different.
tanx in advance


What you actually want to store in 2-D array?
Would you make it clear, is it a predefined pattern/sequence?

-Neo
 
D

Dietmar Schindler

engartte said:
I did the solution on the following question,but no desired output is
acheived.if you know some way on,please show me with its comments.
-----------------------------------------------------
(Declare a two-dimensional array"a" of size 10 by 10 and
read 10 integers into a[0][0] to a[0][9].Then repeat the
insertion specified by an integer sequence nine times,and
store the results from a[1] to a[9].Finally,print the array.
Output Example:
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)
-------------------------------------------------------

#include <stdio.h>

void println(int a[10])
{
int i = 10;
do printf("%d", *a++); while (--i && printf(" "));
printf("\n");
}

int main()
{
int a[10][10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int i, j, k;

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

/* Feel free to add more comments to this. */
 
E

engartte

Dear Dietmar Schindler,
Thanks a lot for solution.but,If possible,please tell me more about
"println"
and the operation of this program.as for me, the use of the
term"println" is
new because I am new with C and array.

Thanks in advance

engartte
 
E

engartte

Dear Neo,

The main aim is to store the integers in 2-D array and it is same as
sorting.

thanks
 
M

Mike Wahler

engartte said:
Dear Neo,

The main aim is to store the integers in 2-D array

He was asking if the integers stored must have particular
values, or are they arbitrary?
and it is same as
sorting.

No, storing is not at all the same as sorting. In order
to sort data, first you must store that data. Two separate
steps.

-Mike
 
M

Mike Wahler

engartte said:
Dear Dietmar Schindler,
Thanks a lot for solution.but,If possible,please tell me more about
"println"
and the operation of this program.as for me, the use of the
term"println" is
new because I am new with C and array.

I'll reproduce his function here (but I've changed
the formatting to better accomdate my comments):

void println(int a[10])

/* Indicates that this function does not return a value,
and has one parameters which is a pointer to a type 'int'
object. */

{
int i = 10;
/* will be used as a counter (counts down rather than up) */

do
/* means to repeatedly execute the instructions between { and },
until the condition specified for 'while' evaluates to true */

{
printf("%d", *a++);

/* prints the integer at the memory address indicated by the
pointer object 'a', then increments 'a' to point to the
next integer in memory */

} while (--i && printf(" "));
/* Decrements (subtracts one from) 'i', then tests whether 'i'
is true (nonzero) or false (zero). If true, prints a blank
space and tests the return value of 'printf()' for true/false
('printf()' returns the number of characters output). Then
combines both 'true/false' results using '&&' (logical 'and'
operator), yielding a 'combined' true/false value. If true,
the loop repeats. If false, the loop terminates. */

printf("\n");
/* prints a blank line */
}

-Mike
 
G

giambi

int L1, C1;

cout<< "Matrix A" << endl;
cout<< "Lines:" << endl;
cin >>L1;
cout<< "Colums:" << endl;
cin>> C1;
int mx1 [L1][C1];
cout<<endl;


for (int m=0; m<L1; m++){
for (int n=0; n<C1; n++){
cout<< " Matrix element " << m+1 << " , " << n+1 <<endl;
cin>> mx1 [m][n];
cout<< endl;}}

for (int m=0; m<L1; m++){
for (int n=0; n<C1; n++){
cout<< mx1 [m][n]; }
cout<<endl;}
 
E

engartte

Dear Mike Wahler ,
Thanks for nice information on.It was too useful to me.
regards,
engartte
 
D

Dietmar Schindler

Mike said:
I'll reproduce his function here (but I've changed
the formatting to better accomdate my comments):
...

You did this more beautiful and accurate than I would have ever done it!

Best regards,
Dietmar
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top