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

Recommended Answers

All 3 Replies

Loop and print the elements.

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 =)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.