Results 1 to 7 of 7

Thread: parallel arrays

  1. #1
    belfast09 is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default parallel arrays

    could somebody please tell me where im going wrong with my code to put grades beside student name i really need some help many thanks ive stuff commented out


    code

    public class arrays {





    public static void main(String[] args) {
    String[] students = new String [8];
    int[][] grades = new int[8][2];
    String tmpStr;
    int tmpInt;

    students[0] = "Smith";
    students[1] = "Jones";
    students[2] = "Thompson";
    students[3] = "Paul";
    students[4] = "Jodan";
    students[5] = "Cole";
    students[6] = "Dunn";
    students[7] = "Nolan";

    grades[0][0] = 88;
    grades[1][0] = 78;
    grades[2][0] = 81;
    grades[3][0] = 65;
    grades[4][0] = 92;
    grades[5][0] = 87;
    grades[6][0] = 80;
    grades[7][0] = 84;

    grades[0][1] = 88;
    grades[1][1] = 78;
    grades[2][1] = 81;
    grades[3][1] = 65;
    grades[4][1] = 92;
    grades[5][1] = 87;
    grades[6][1] = 80;
    grades[7][1] = 84;


    grades[0][2] = 88;
    grades[1][2] = 78;
    grades[2][2] = 81;
    grades[3][2] = 65;
    grades[4][2] = 92;
    grades[5][2] = 87;
    grades[6][2] = 80;
    grades[7][2] = 84;

    // Arrays.sort(students);
    // Arrays.sort(grades);
    // for(int t=0, t<students.length,-1 t++)
    // for(int i=0, i<students.length,-1 i++)
    // if(students[i].compareTo(students+1)>0){
    // tmpStr = students[i];
    // tmpInt = grades[i];
    // students[i] = students[i+1];
    // grades[i] = grades[i+1];
    // students [i+1] = tmpStr;
    // grades[i+1] =tmpInt;
    // }
    //
    //// print students
    int t;
    int i;
    for(i=0, i< students.length, i++){
    System.out.printf(%s - 15s %s* students[i]);
    int intSum = 0;
    double avgGrad;
    for(t=0, t<grades[i].length,t++){
    System.out.print(grades[i][t] + " ");
    intSum += grades[i][t];
    }
    int avgGrade = double / intSum;
    grades[i].length;
    System.out.println( "%5s %s \n" ",grades[i);
    }
    // }

    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Read my blog on this forum about sorting parallel arrays (using a Heapsort implementation). b.t.w. trying to sort parallel arrays is bad design from the old Fortran world ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    belfast09 is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    thanks how di find it

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    As Jos says, parallel arrays is bad design. You really should create a Student class that contains names and grades, and create just one array of Students.
    Get in the habit of using standard Java naming conventions!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by belfast09 View Post
    thanks how di find it
    In the right corner, just above my reply you see a link 'Blog entries 3'; click on the number ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    belfast09 is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    sorr jos i cant find it

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by belfast09 View Post
    sorr jos i cant find it
    Reread my previous reply; alternatively you can get to the blogs page by pressing 'Blogs' near the top of this page.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. parallel arrays
    By the beginner in forum New To Java
    Replies: 9
    Last Post: 02-22-2011, 05:35 AM
  2. Parallel Arrays
    By mwenchong in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 12:20 AM
  3. two parallel arrays
    By Adomini in forum New To Java
    Replies: 12
    Last Post: 09-07-2010, 01:45 AM
  4. How to create parallel arrays
    By Roselicious in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 12:10 PM
  5. I need examples using parallel arrays
    By dangerzone9k in forum New To Java
    Replies: 10
    Last Post: 04-04-2009, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •