Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Arrays- assignment due in 30 mins!!

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Arrays- assignment due in 30 mins!!

    This is the question:
    An array chosen contains n distinct integers arranged in no particular order.
    Another array winners contains m distinct integers arranged in ascending order.
    Write efficient code to determine how many of the numbers in chosen appear in
    winners

    package worksheet6Quest25;
     
    import java.util.*;
     
    public class ArrayCompareNumbers{
    	public static void main (String []args){
    	Scanner in =new Scanner(System.in);
    	int []chosen= {5, 8, 4, 9 ,1, 7, 0};
    	int []winner= {1, 4, 6, 8, 0, 3};
    	int m;
    	int n;
    	int count=0;
    	for (m=0;m< m-1; m++)
    	{
    	   for (n=0; n<n-1; n++)
    	   {  	
    		 if (chosen[n]==winner[m])
    		 {
    			 count++;
     
    		 }
     
    	   }
    	System.out.printf ("%d", count) ; 
    	}
    }
    }


    My output is blank.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    Your first step should be to debug the program either by using a debugger or simply adding print statements. That's going to be a lot faster than posting to a forum. But I'll give you a hint:

    Look at this for loop:

    for (m=0;m< m-1; m++)

    When will that ever be true?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    should be :
    for (i=0;i< =m; m++)

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    Okay, and what happened when you changed it to that? What happened when you stepped through a debugger or added some print statements to figure out where the program's execution differs from your expectations?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    I changed it to that and also debugged, that same line gives a problem.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    What problem does it give you?

    Similarly, when will your code enter this for loop?

    for (n=0; n<n-1; n++)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    Thread [main] (Suspended (The local variable m may not have been initialized))
    ArrayCompareNumbers.main(String[]) line: 14
    I should probably take out one of the for loops since they are consecutive.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    You should post your modified code so that we're looking at the same thing.

    I'm also not sure what you mean by taking out one of the for loops. Do you need them or do you not need them?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    I ran out of time and had to submit. However, this week miss will go through the correct solutions in class.
    Thanks for time and assistance.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Arrays- assignment due in 30 mins!!

    I suppose that's a lesson in time management, which is an important lesson to learn.

    I would suggest becoming more familiar with the process of debugging instead of trying random things. Working through a problem is the actual lesson behind your assignments, and simply looking at the correct syntax won't teach you that.

    Good luck.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Arrays- assignment due in 30 mins!!

    What is m? What is n? You are supposed to assign m and n with the length those array.
    Also, you do not need Scanner in =new Scanner(System.in);

Similar Threads

  1. While loop to perform multiple steps Please help assignment due in 12 hours!!
    By brobertson300 in forum What's Wrong With My Code?
    Replies: 28
    Last Post: March 21st, 2014, 03:17 PM
  2. Help with school assignment/arrays
    By d0mlnance in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2013, 09:00 PM
  3. Replies: 1
    Last Post: December 5th, 2012, 09:58 PM
  4. [SOLVED] PLZ HELP! Somethng wrong with code, assignment due today! Print if multiple of...
    By Rstuart970 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 16th, 2012, 02:35 AM
  5. Need help on an Assignment but its due tomorrow!
    By Mob31 in forum Paid Java Projects
    Replies: 1
    Last Post: March 2nd, 2011, 05:54 AM