Confusion on output

Discussion in 'C Programming' started by engartte, Jan 3, 2005.

1. engartteGuest

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

engartte, Jan 3, 2005

2. NeoGuest

"engartte" <> wrote in message
news:...
> 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

Neo, Jan 4, 2005

3. Dietmar SchindlerGuest

engartte wrote:
> 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. */

Dietmar Schindler, Jan 4, 2005
4. engartteGuest

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

engartte, Jan 4, 2005
5. engartteGuest

Dear Neo,

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

thanks

engartte, Jan 4, 2005
6. Mike WahlerGuest

"engartte" <> wrote in message
news:...
> 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

Mike Wahler, Jan 4, 2005
7. Mike WahlerGuest

"engartte" <> wrote in message
news:...
> 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

Mike Wahler, Jan 4, 2005
8. giambiGuest

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;}

giambi, Jan 4, 2005
9. Keith ThompsonGuest

"giambi" <> writes:
> int L1, C1;
>
> cout<< "Matrix A" << endl;

[snip]

comp.lang.c++ is around the corner. This is comp.lang.c.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Jan 4, 2005
10. engartteGuest

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

engartte, Jan 5, 2005
11. Dietmar SchindlerGuest

Mike Wahler wrote:
> 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

Dietmar Schindler, Jan 5, 2005

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.