Difference between revisions of "PrintArray Method"

From CompSciWiki
Jump to: navigation, search
Line 3: Line 3:
 
|Problem= Pretend you're a computer science professor. You have a program which stores the names of all the students in each of your classes in separate arrays, based on the class they are in. How would you go about printing the contents of each of these arrays?
 
|Problem= Pretend you're a computer science professor. You have a program which stores the names of all the students in each of your classes in separate arrays, based on the class they are in. How would you go about printing the contents of each of these arrays?
  
On one hand, you could copy and paste a loop into your main method to print each of the arrays, but that would use a lot of redundant code. Instead, it would be good to write a method to print an array, and call it every time you want to print one of your class lists.
+
The most obvious way to print out your arrays would be to copy and paste a loop into your main method to print each of the arrays. Unfortunately, that would use a lot of redundant code. Think about it. You'd be writing a for loop for each array that you'd want to print out. By this method, if you've got two arrays to print, you'd need two loops to print them. If you have ten arrays, you'll need ten loops. If you've got one hundred arrays, you'll need one hundred loops. It's just not good coding.
  
  
Line 13: Line 13:
 
An image or By Students section
 
An image or By Students section
  
|Solution=The solution...
+
|Solution=Write a method that takes an array as an input and prints out its contents! It's an easy solution, and once you've got your method written you can easily print another array by just adding one more line of code (a method call!) to your program.
  
 
}}
 
}}

Revision as of 12:00, 1 April 2010

Back to the Program-A-Day homepage

Problem

Pretend you're a computer science professor. You have a program which stores the names of all the students in each of your classes in separate arrays, based on the class they are in. How would you go about printing the contents of each of these arrays?

The most obvious way to print out your arrays would be to copy and paste a loop into your main method to print each of the arrays. Unfortunately, that would use a lot of redundant code. Think about it. You'd be writing a for loop for each array that you'd want to print out. By this method, if you've got two arrays to print, you'd need two loops to print them. If you have ten arrays, you'll need ten loops. If you've got one hundred arrays, you'll need one hundred loops. It's just not good coding.

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

Write a method that takes an array as an input and prints out its contents! It's an easy solution, and once you've got your method written you can easily print another array by just adding one more line of code (a method call!) to your program.

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage