Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.

Similar presentations


Presentation on theme: "CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014."— Presentation transcript:

1 CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014

2 Find Errors (If Any): char[] array = new char[5]; for (int i=0 ; i <= 4 ; i++) array = ‘M’;

3 Find Errors (If Any): int[] array = new int[5]; for(int i=0 ; i < array.length() ; i++) array(i) = i;

4 Problem Description What is output by the following programs?

5 Sample Output: Problem Description (Odd/Even Index) Write a program that prompts the user to enter ten numbers and print the content of the odd index, then print the even index.

6 Code Skelton import java.util.Scanner; public class OddEvenIndex { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] numbers = new int[10]; System.out.print("Enter ten numbers: "); for (int i = 0; i < numbers.length; i++) numbers[i] = input.nextInt();

7 System.out.print("Odd index elements are: "); for (int i = 1; i < numbers.length; i += 2) System.out.print(numbers[i] + " "); System.out.println(); // new line System.out.print("Even index elements are: "); for (int i = 0; i < numbers.length; i += 2) System.out.print(numbers[i] + " "); System.out.println(); // new line } // end of main }

8 Follow-up Questions and Activities (Analyze scores) Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100. Sample Output:

9 Code Skelton import java.util.Scanner ; public class Analyze { public static void main(String[] args) { Scanner input = new Scanner(System.in); double[] scores = new double[100]; int count = 0; do { System.out.print("Enter a new score: "); scores[count] = input.nextDouble(); } while (scores[count++] != -1);

10 int numOfAbove = 0; int numOfBelow = 0; for (int i = 0; i < count - 1; i++) if (scores[i] >= 60) numOfAbove++; else numOfBelow++; System.out.println("Number of scores above or equal to the pass marks " + numOfAbove); System.out.println("Number of scores below the pass marks “ + numOfBelow); } // end of Main }

11 Evaluation (Linear Search) Write a program that prompts the user to enter the size of the array and let the user to enter the content of that array, then ask the user to enter a value to search in the array using linear search. Check if the value is found or not. In case if the values founded print the position. Sample Output:


Download ppt "CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014."

Similar presentations


Ads by Google