0

Hi could you please tell me how to print array elements with spaces. Please provide an example. I would like to see how a 2 D array is printed.Thanks a lot

4
Contributors
3
Replies
12
Views
9 Years
Discussion Span
Last Post by Aia
0

Hi could you please tell me how to print array elements with spaces. Please provide an example. I would like to see how a 2 D array is printed.Thanks a lot

For 1D arrays : use a single "for" loop to go through the array and print the current array element.
This code for example will print all the elements of a 1D array.

int array[5]={9,8,7,6,5};
for(int i=0;i<5;printf("%d ",array[i]), i++);

For 2D arrays : use two nested "for" loops, and follow my steps from above.
Bonus if you can squeeze it in one line =)

0

For 1D arrays : use a single "for" loop to go through the array and print the current array element.
This code for example will print all the elements of a 1D array.

int array[5]={9,8,7,6,5};
for(int i=0;i<5;printf("%d ",array[i]), i++);

I can only guess why you took all this trouble, even going outside C89 syntax, to just show the OP how to do a for loop, that doesn't answer at all the question in the post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.