#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Waterloo Computing Test Questions


    Hello all,

    I am taking the Waterloo Computing Test next spring and wanted to start preparing now because im a very basic programmer. I was looking at last years test and was trying to write some of the programs but struggled. I'm able to do the first program no problem. I know how to do the second but can't read in different chars from the same line. The third,fourth and fifth i'm stumped. If you can help out by posting the answers for 2 - 5 that would be great. I learn very fast from seeing the answer and it helps me understand the approach to take. Thanks for any help.

    Questions: www. cemc. uwaterloo.ca/contests/computing/2012/stage1/seniorEn.pdf



    Sorry for posting the URL and breaking the rules but im not sure how to get around that because this post is dependant on that link. LOL. If someone knows I'll edit the post. Thanks
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Please edit the post and wrap the code in code tags to preserve formatting.

    What code do have that you are having problems with? Post your code and your questions.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Please edit the post and wrap the code in code tags to preserve formatting.

    What code do have that you are having problems with? Post your code and your questions.
    Mainly 3 and 5. I don't have a program for them because it is more the logic that I am struggling with, not syntax. If you could help with the logic I can do the rest.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Start by writing the steps the program needs to take to solve the problem.
    Begin at a high level and then refine the steps by filling more details.
    Pick one and do that first, then move on to the next one.
    So far you haven't posted any specific questions or details about your problems.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Question 3:


    Code:
    class WaterlooQ3
    {
    	public static void main (String [] args)
    	{
    		int n;
    		
    		n = In.getInt();
    		int sensors [] = new int [n+1];
    		int frequency[] = new int [n+1];
    		
    		System.out.println();
    		
    		for (int i = 0; i <= (n-1); i++)
    		{
    			frequency[i] = 0;
    		}
    		
    		for (int i = 0; i <= (n-1); i++)
    		{
    			sensors[i] = In.getInt();
    		}
    	}
    }
    Im not sure how to go about getting the frequency of each number and which number has the highest frequency. Would I need a variable for each number?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you answer these questions?
    Where do the numbers come from?
    What is the range of numbers (min & max)?
    How many numbers are input to the program?

    What techniques do you know about?
    Can you use any classes?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Can you answer these questions?
    Where do the numbers come from?
    What is the range of numbers (min & max)?
    How many numbers are input to the program?

    What techniques do you know about?
    Can you use any classes?
    The numbers are input.There can be 2 billion, but the value can be 1 - 1000.

    I am not very knowledgeable with java language and don't know how to use classes in java. I got the program to work sort of. Just need a little help.

    Code:
    class WaterlooQ3
    {
    	public static void main (String [] args)
    	{
    		int n;
    		
    		n = In.getInt();
    		int sensors [] = new int [1001];
    		int frequency[] = new int [1001];
    		int counter [] = new int [1001];
    		int temp, temp1;
    		
    		System.out.println();
    		
    		for (int i = 0; i <= 1000; i++)			//initializes
    		{
    			counter[i] = 0;
    		}
    		
    		for (int i = 0; i <= 1000; i++)			//initializes
    		{
    			frequency[i] = i;
    		}
    		
    		for (int i = 0; i <= (n-1); i++)		//reads in sensor readings
    		{
    			sensors[i] = In.getInt();
    		}
    		
    		for (int i = n; i <= 1000; i++)
    		{
    			sensors[i] = 0;
    		}
    		System.out.println();
    		
    		
    		for (int i = 0; i <= 1000; i++)			//counts the frequency
    		{
    			for (int a = 0; a<= (n-1); a++)
    			{
    				if (sensors[a] == frequency[i])
    				{
    					counter[i]++;
    				}	
    			}
    		}
    		
    		for (int pass = 0; pass <= 999; pass++)
    		{
    			for (int i = 0; i <= 999; i++)			
    			{
    				if (counter[i] > counter[i + 1])
    				{
    					temp = counter[i];
    					counter[i] = counter[i + 1];
    					counter[i+1] = temp;
    					
    					temp1 = sensors[i];
    					sensors[i] = sensors[i + 1];
    					sensors[i+1] = temp1;	
    				}
    			}
    		}
    	
    		System.out.println(counter[1000] + "----" + sensors[1000]);
    		System.out.println(counter[999] + "----" + sensors[999]);
    	}
    }
    It counts right, the problem is showing the value of the second most frequent number.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    How do you find the most frequent number? Can you use the same technique (with a slight modification) to find the second most frequent?

    One thing I suggest to improve the code is to use a variable to define the size of the arrays and use the length of the array to control the looping. You should NOT hard code numbers like 1001, 1000 and 999 all over the code. That makes the code very hard to change if you want to change the size of the arrays.
    Last edited by NormR; September 27th, 2012 at 10:10 PM.

IMN logo majestic logo threadwatch logo seochat tools logo