Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
January 28th, 2013, 08:13 PM
 13patterc
Registered User

Join Date: Jan 2013
Posts: 3
Time spent in forums: 1 h 15 sec
Reputation Power: 0
[Homework] Array Help

I have a couple array problems that I can't for the life of me figure out. I've spent countless hours just getting to the point I'm at and even more hours trying to figure out why these two programs still don't work. I am very new to Java so bear with me...

General guidelines: In order to accomplish this work as quickly as possible, we will not use keyboard or file input. Instead, you may assign the 10 values to your initial arrays in the programs. Using initializer lists to load them with the data types expected for each exercise. Please use methods to do each task. Pass your arrays to the method to complete each task.

1. Write a program that takes 10 integers as input. The program places the even integers into an array called evenList, the odd integers into an array called oddList, and the negative integers into an array called negativeList.The program displays the contents of the three arrays after all of the integers have been entered.

Here is my code:
Code:
```import java.util.*;
import java.io.*;

public class arraysproject1
{
private static int i = 0;
private static int number = 0;

public static void main(String[] args) throws IOException
{
int[]  intArray = {13, -2, 10, 7, -15, 20, 51, 87, 22, -90};
sortEvens(intArray);
sortOdds(intArray);
sortNegatives(intArray);
}

public static void sortEvens(int[] intArray)
{
System.out.println("The even numbers are: ");
int[]  evenList = new int[5];
int even = 0;
for(i = 0; i < 9; i++){
if(intArray[i] % 2 == 0){
intArray[i] = evenList[even];
}
}
System.out.println(evenList[even]);
}

public static void sortOdds(int[] intArray)
{
System.out.println("The odd numbers are: ");
int[]  oddList = new int[5];
int odd = 0;
for(i = 0; i < 9; i++){
if(intArray[i] % 2 != 0){
intArray[i] = oddList[odd];
}
}
System.out.println(oddList[odd]);
}

public static void sortNegatives(int[] intArray)
{
System.out.println("The negative numbers are: ");
int[]  negativeList = new int[3];
int negative = 0;
for(i = 0; i < 9; i++){
if(intArray[i] < 0){
intArray[i] = negativeList[negative];
}
}
System.out.println(negativeList[negative]);
}
}```

And my output:
The even numbers are: 0
The odd numbers are: 0
The negative numbers are: 0

2.Write a program that randomly inputs 20 numbers from 1 to 10 into a 2-D array of 5 rows and 4 columns. The program should output the 2-D array, the sum of each row storing numbers in a parallel array and the sum of each column storing number in a parallel array.

Code:
Code:
```import java.util.*;
import java.io.*;
import java.util.Random;

public class arraysproject3
{
private static int i = 0;
private static int j = 0;

public static void main(String[]args)
{
int[][] randArray = new int[5][4];
Random randomizer = new Random();
for (i = 0; i < randArray.length; i++){
for (j = 0; j < randArray[i].length; j++){
randArray[i][j] = randomizer.nextInt(11);
}
}
print(randArray);
sumRows(randArray);
sumCols(randArray);
}
public static void print(int[][] randArray)
{
System.out.println("Elements in 2D Array:");
for (i = 0; i < randArray.length; i++){
for (j = 0; j < randArray[i].length; j++){
System.out.println(randArray[i][j]);
}
}
}

public static void sumRows(int[][] randArray)
{
int[] sumOfRows = new int[randArray.length];
int sumOfR = 0;
for (int row = 0; row < randArray.length; row++){
for (int col = 0; col < randArray[row].length; col++){
sumOfR += randArray[row][col];
}
sumOfRows[row] = sumOfR;
}
System.out.println("Sum of rows: " + sumOfR);
}

public static void sumCols(int[][] randArray)
{
int[] sumOfCols = new int[randArray[i].length];
int sumOfC = 0;
for (int col = 0; col < randArray.length; col++){
for (int row = 0; row < randArray[col].length; row++){
sumOfC += randArray[col][row];
}
sumOfCols[col] = sumOfC;
}
System.out.println("Sum of columns: " + sumOfC);
}

}```

And output:
Elements in 2D Array:
9
9
6
1
3
1
6
1
4
10
7
0
4
4
5
6
5
5
10
3
Sum of rows: 99
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at arraysproject3.sumCols(arraysproject3.java:55)
at arraysproject3.main(arraysproject3.java:28)

Any guidance on what I'm doing wrong would be great! Thanks

#2
January 28th, 2013, 09:36 PM
 JonthnC
Contributing User

Join Date: Sep 2010
Posts: 68
Time spent in forums: 2 Days 2 h 35 m
Reputation Power: 4
You only have a few issues, which is good.

In the first program, the sortEvens method is iterating through a 10 element array, but the array you set up for storing the even numbers is only 5 elements long. What happens if you have 6 even numbers? Secondly, the index where you storing data into evenList is never increased. This means when your program does find an even number it stores it into the same position as the previous ones it found. Fortunately, all three of your methods are similar. Hopefully, you should be able to make these same 2 changes to all of them.

In your second program, for both methods you're not printing the array storing the totals. You're only printing the last total you calculated. Also, in the sumCols method, you make an array with a length of randArray[i].length, then you try to access it up to an index of randArray.length.

Try fixing the errors I pointed out, and see if you can get it working from there.

#3
January 29th, 2013, 07:53 AM
 13patterc
Registered User

Join Date: Jan 2013
Posts: 3
Time spent in forums: 1 h 15 sec
Reputation Power: 0
Quote:
 Originally Posted by JonthnC Try fixing the errors I pointed out, and see if you can get it working from there.

Thanks so much for the help. I will work on it and check back if I have any more questions.

#4
January 29th, 2013, 08:17 PM
 13patterc
Registered User

Join Date: Jan 2013
Posts: 3
Time spent in forums: 1 h 15 sec
Reputation Power: 0
Ok I've been able to fix the second one, but I'm still struggling with the odd/even/negative one. I made the changes you said and still get the same output.

#5
January 29th, 2013, 10:42 PM
 JonthnC
Contributing User

Join Date: Sep 2010
Posts: 68
Time spent in forums: 2 Days 2 h 35 m
Reputation Power: 4
Code:
```  public static void sortEvens(int[] intArray)
{
System.out.println("The even numbers are: ");
int[]  evenList = new int[10];
int even = 0;
for(i = 0; i < 10; i++){
if(intArray[i] % 2 == 0){
evenList[even] = intArray[i];
even++;
}
}
System.out.println(evenList);
}```

I made a few changes to get this working. (I did overlook a few errors and failed to mention them in the last post)
I set the length of evenList to 10. The for loop is set to run 10 times. I added an increment operator to even, and I reversed the line in the if statement so the new value is put into the evenList (you had it backwards). I also print the entire array rather than the last element.

I didn't check this with a compiler, but it should be right.
--- One more thing, we use the number 10 in there twice. You should change this to the length of the array in both instances (10 becomes intArray.length). This way, if you change the number of elements in your initial array, your code will still run fine.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > [Homework] Array Help

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |