Difference between revisions of "Print Array"

From CompSciWiki
Jump to: navigation, search
Line 14: Line 14:
  
 
|SideSection=
 
|SideSection=
[[Image:wiki_method01.jpg|float]]
+
Printing an array is a very common task in programming, there are
<BR>
+
many situations which require such output. It is also very useful
 +
for testing, to check that the elements in an array are what they
 +
are supposed to be.
  
  

Revision as of 12:22, 6 April 2010

Back to the Program-A-Day homepage

Problem

Write a method which will take an array of ints as a parameter. The method should print the elements of the array, separated by a comma.

Example:
int[] n = {45,32,78,45,890,45,32};
printArray(n);
Output:45,32,78,890,45,32

 

...by students

Printing an array is a very common task in programming, there are many situations which require such output. It is also very useful for testing, to check that the elements in an array are what they are supposed to be.

Solution

To solve this problem you need to use a for loop. Loop from 0 through all elements of the input array. At each iteration, print out the current array element and a comma.

Code

Solution Code

Back to the Program-A-Day homepage