Difference between revisions of "Arrays Solutions"

From CompSciWiki
Jump to: navigation, search
(Added solution to exercise 7)
Line 142: Line 142:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
===Exercise 7===
 +
Here is a sample program for Exercise 7.
 +
<pre>
 +
public class Exercise7{
 +
  public static void main(String[] args){
 +
      //declare our variables
 +
      int numRandomNumbers;
 +
      int num_months;
 +
      int[] randomNumbers,months;
 +
      //initialize the variables
 +
      randomNumbers = new int[numRandomNumbers];
 +
      months = new int[num_months];
 +
      num_months = 12;
 +
      numRandomNumbers = 100;
 +
 +
      //generate the random numbers and put them into the array
 +
      for(int index = 0; index < numRandomNumbers; index++){
 +
        randomNumbers[index] = (int)((Math.random()*(num_months)) + 1);
 +
      }//end for loop
 +
 +
      //increment the months array according to the random numbers in '''randomNumbers'''
 +
      for(int index = 0; index < numRandomNumbers; index++){
 +
        months[randomNumbers[index]-1]++;
 +
      }//end for loop
 +
 +
      //finally print out the contents of the months array
 +
      for(int index = 0; index < num_months; index++){
 +
        System.out.println("Month[" + index + "] = " + months[index]);
 +
      }//end for loop
 +
  }//end main method
 +
}//end class Exercise7
 +
</pre>
 +
At this time you do not need to know why I use the following piece of code:
 +
randomNumbers[index] = (int)((Math.random()*(num_months)) + 1);
 +
All you need to know is that it gives an integer between 1 and 12 inclusive.
  
 
{{Category:COMP 1010}}
 
{{Category:COMP 1010}}
 
{{Category:Java}}
 
{{Category:Java}}
 
{{Category:Solutions}}
 
{{Category:Solutions}}

Revision as of 18:17, 5 December 2007

Main_Page > Arrays


Introduction

These are the solutions for the review questions for arrays.





Overview

If you did not answer any of these questions successfully make sure to go back and re-read the material. If you have trouble understanding the material talk to your prof or a TA

Template loop detected: Template loop detected:

Review Questions

Question 1

This is called an out of bounds error


Question 2

This is called a one off error


Question 3

The second statement makes a copy of the address that is stored in array1. Now array2 gets the address.


Question 4

No. Here the == operator compares the memory address of the reference variable not the contents of the objects referenced by the variable.


Question 5

No. The array element array[10] is changed to "Hello", but the contents of i are unchanged.


Question 6

The string "Gello" is printed.


Exercises

Exercise 1

int product = 1;
int array[] = new int[10];

//have values entered into the array

//calculate the product
for(int i=0; i<array.length; i++)
{
   product *= array[i];
}


Exercise 2

int lowPos;
int lowest = INTEGER.MAX_VALUE;    //set to the highest possible value so everything is smaller
int array[] = new int[100];

//have values entered into the array

//go through the array
for(int i=0; i<array.length; i++) 
{
   if(array[i] < lowest) 
   {
      lowest = array[i];
      lowPos = i;
   }
}


Exercise 3

import java.io.*;

public class CreateArray
{

  public static void main(String []args)
  {  
    //create an array 
    int[] numbers = new int[10];
    //populating array
    for(int i=0;i<10;i++)
    {
       numbers[i]=10+i;
    }

    //displaying array elements
    for(i=0;i<10;i++)
    {
       System.out.println(numbers[i]);
    }
  }
}


Exercise 4

public class ArrayCopy
{
   public static void main(String []args)
   {
       int array1[]={1,2,3,5}
       int array2[]=new int[4];
     
       //This loop copys contents from array1 to array2 
       for(int i=0;i<array1.length();i++)
       {
           array2[i]=array1[i];
       }
   }
}


Exercise 5

public static void sortArray (int[] array) 
{
    int j = 0, k = 0, temp = 0;

    for (j = 0; j < array.length; j++) 
    {
        for (k = 0; k < array.length - 1; k++) 
        {
            if (array[k] > array[k + 1]) 
            {
                temp = array[k];
                array[k] = array[k + 1];
                array[k + 1] = temp;
            }
        }
    }
}


Exercise 6

void printArray (Object[] array) 
{
    int j = 0;

    for (j = 0; j < array.length; j++) 
    {
        System.out.println(array[j].toString);
    }
}

Exercise 7

Here is a sample program for Exercise 7.

public class Exercise7{
   public static void main(String[] args){
      //declare our variables
      int numRandomNumbers;
      int num_months;
      int[] randomNumbers,months;
      //initialize the variables
      randomNumbers = new int[numRandomNumbers];
      months = new int[num_months];
      num_months = 12;
      numRandomNumbers = 100;

      //generate the random numbers and put them into the array
      for(int index = 0; index < numRandomNumbers; index++){
         randomNumbers[index] = (int)((Math.random()*(num_months)) + 1);
      }//end for loop

      //increment the months array according to the random numbers in '''randomNumbers'''
      for(int index = 0; index < numRandomNumbers; index++){
         months[randomNumbers[index]-1]++;
      }//end for loop

      //finally print out the contents of the months array
      for(int index = 0; index < num_months; index++){
         System.out.println("Month[" + index + "] = " + months[index]);
      }//end for loop
   }//end main method
}//end class Exercise7

At this time you do not need to know why I use the following piece of code: randomNumbers[index] = (int)((Math.random()*(num_months)) + 1); All you need to know is that it gives an integer between 1 and 12 inclusive.

Template loop detected: Template loop detected: Template loop detected: