Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By Junky
  • 1 Post By Junky
  • 1 Post By sunde887

Thread: Question about some lines of code arrays

  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default Question about some lines of code arrays

    Hi, I have a question about some code in this program, it deals with arrays. I have commented the questions on the lines.
    Any help greatly appreciated. Thank you.


    Java Code:
    //********************************************************************
    //  SodaSurvey.java       Author: Lewis/Loftus
    //
    //  Demonstrates the use of a two-dimensional array.
    //********************************************************************
    
    import java.text.DecimalFormat;
    
    public class SodaSurvey
    {
       //-----------------------------------------------------------------
       //  Determines and prints the average of each row (soda) and each
       //  column (respondent) of the survey scores.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          int[][] scores = { {3, 4, 5, 2, 1, 4, 3, 2, 4, 4},
                             {2, 4, 3, 4, 3, 3, 2, 1, 2, 2},
                             {3, 5, 4, 5, 5, 3, 2, 5, 5, 5},
                             {1, 1, 1, 3, 1, 2, 1, 3, 2, 4} };
    
          final int SODAS = scores.length;
          final int PEOPLE = scores[0].length;
    
          int[] sodaSum = new int[SODAS];[COLOR="green"] //I don't understand this line[/COLOR]
          int[] personSum = new int[PEOPLE];[COLOR="green"]//I don't understand this line either[/COLOR]
    
          for (int soda=0; soda < SODAS; soda++)
             for (int person=0; person < PEOPLE; person++)
             {
                sodaSum[soda] += scores[soda][person];[COLOR="green"]//I don't understand this line[/COLOR]
                personSum[person] += scores[soda][person];[COLOR="green"]//Dont understand this line either[/COLOR]
             }
    
          DecimalFormat fmt = new DecimalFormat ("0.#");
          System.out.println ("Averages:\n");
    
          for (int soda=0; soda < SODAS; soda++)
             System.out.println ("Soda #" + (soda+1) + ": " + 
                        fmt.format ((float)sodaSum[soda]/PEOPLE));
    
          System.out.println ();
          for (int person =0; person < PEOPLE; person++)
             System.out.println ("Person #" + (person+1) + ": " + 
                        fmt.format ((float)personSum[person]/SODAS));
       }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    int[] sodaSum = new int[SODAS];
    int[] personSum = new int[PEOPLE];
    What don't you understand? Both lines declare and initialise an int array with a length of whatever value is stored in the variables: SODAS and PEOPLE. Perhaps you are more used to seeing
    Java Code:
    int[] arr = new int[10];
    which is basically the same except the length is hard coded.
    Java Code:
    sodaSum[soda] += scores[soda][person];
    personSum[person] += scores[soda][person];
    Same for these lines. Rather having a hardcoded number for the index of the array it is using a variable. Or are you not understanding the 2D array part?
    silverglade likes this.

  3. #3
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Sorry I didnt specify what I didn't understand,It just looked like greek. But now I understand more of what I don't understand lol. I thought the above code should be int [] sodasum = SODAS; instead of into [] sodasum = new int [SODAS]. , because new int [SODAS] looks like we are creating an array. and sodaSum[soda] += scores[soda][person], how can we make the value of a one dimensional array equal to a 2D array? Sorry for the confusion, thank you for helping.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by silverglade View Post
    I thought the above code should be int [] sodasum = SODAS;
    No, that is attempting to assign a single int to an int array.
    instead of into [] sodasum = new int [SODAS]. , because new int [SODAS] looks like we are creating an array.
    Yes it is creating an array. sodasum is a variable that references a 1D array of ints.
    sodaSum[soda] += scores[soda][person], how can we make the value of a one dimensional array equal to a 2D array?
    It isn't. It is accessing a single value in the 2D array at location [soda][person] and assigning it to location [soda] in the 1D array
    silverglade likes this.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    To help clarify, the 2 d array also contains ints. It is simply an array of arrays where each array is filled with ints, so if you index into the first dimension you are looking at an array of ints. If you index into the first and second you are looking at an int. So it's safe to add the item at index [x][y] to a element of the one d array.

    I hope this clarifies it somewhat, if not, let me know and I will try to do a better job explaining.
    silverglade likes this.

  6. #6
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Great thank you Junky and Sunde887. I now understand it after writing out the loops and putting in the values like you would a math problem. That is the best way I think to figure out code. Treat it as a math problem. It is nice to see you Sunde887, I was away for a while. But now I'm learning java again every day or I try to. I am starting to see the art of computer programming, especially in these "elegant" little for loop applications that I have to stretch my non programmer brain to understand. The more compact the code, the more ingenious it is, and kind of an art form. Like looking at someone take a huge engine (bad programmer or non programmer) and redoing it the size of a lunch box (good programmer or experienced). I hope I can write an app that good as the one I just showed you. Since I don't have the mindset for it yet. The previous code I showed you is very ingenius to me. Thanks both again. Another hurdle jumped. Derek

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to see you again and hope all is well. How's the book coming? How far in have you gotten? The more you read and practice, the easier things get.

  8. #8
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Thank you. Yes I still am reading "Java Software Solutions" after I had to take a long break due to other life things. But now I am back, and I am on page 405 out of about 700. Then I go back to "Head First Java" and finish that. Then I go to "Thinking in Java" and finish that. Then I go to effective java. I am not sure whether to do effective java or thinking in java first, but I will have to take a closer look at effective java. Also, the "Java Software Solutions" program exercises would have too many future-chapter-dependent requirements, so I decided to read the whole book then tackle the chapter program exercises. Because I just got frustrated by the first few chapter exercises when they would require something not covered yet. But it is great to be doing the book again and being on the forum, great people on this forum.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I know how the life interference stuff goes. I've been busy with some stuff lately and haven't been reading as much as I'd like. I'd say read head first java before effective java. I am just finishing effective java and I am really enjoying it. It isn't necessarily an intro book though. It gives you a lot of great things to keep in mind when designing and writing code.

    If you are feeling good, you could consider trying effective java and thinking in java at the same time and back off whichever is too challenging.

  10. #10
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Good idea. I will try effective java and thinking in java at the same time and like you said, back off on the harder one. thank you.

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2012, 07:46 PM
  2. Please explain these 2 lines of code to me..
    By murphaph in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 03:11 PM
  3. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 03:29 AM
  4. Removing empty lines from code using Eclipse
    By javaplus in forum Eclipse
    Replies: 1
    Last Post: 12-14-2007, 10:21 PM
  5. Unify these 3 lines of code
    By ulykidjoe in forum Advanced Java
    Replies: 4
    Last Post: 07-13-2007, 02:15 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
  •