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

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

CS110 Programming Language I
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Introduction to Computer Programming Decisions If/Else Booleans.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Java vs. You.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.
LAB 10.
Computer Programming Lab(4).
Computer Programming Lab(5).
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Lecture 10 Recursion. public class recursionDemo { public static void main(String[] args) { System.out.println("TriCount for n = 5 is... " + triCount(5));
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
1 Traversal algorithms. 2 Array traversal traversal: An examination of each element of an array. Traversal algorithms often takes the following form:
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Computer Programming1 Computer Science 1 Computer Programming.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Chapter 2 Clarifications
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Software Development Packages
Data types, Expressions and assignment, Input from User
Case Study 2 – Marking a Multiple-choice Test
TK1114 Computer Programming
Building Java Programs
Building Java Programs
CSC 142 Computer Science II
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Java Language Basics.
Building Java Programs
Building Java Programs
python.reset() Also moving to a more reasonable room (CSE 403)
Self study.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Building Java Programs
Dr. Sampath Jayarathna Cal Poly Pomona
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Building Java Programs
Array Review Selection Sort
Computer Science Club 1st November 2019.
Presentation transcript:

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

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

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

Problem Description What is output by the following programs?

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.

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();

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 }

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:

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);

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 }

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: