Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.

Slides:



Advertisements
Similar presentations
Arrays and Strings.
Advertisements

Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 7 & 8- Arrays and Strings
Arrays.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
A First Book of ANSI C Fourth Edition
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
1 Arrays Spring 2006 UVA - CS 101 Aaron Bloomfield Edited for WLCS by Paul Bui.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
1 Arrays. 2 Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional 
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Arrays Chapter 7.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Arrays.
Pascal Programming Arrays and String Type.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Arrays Low level collections.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Chapter 7 Part 1 Edited by JJ Shepherd
Arrays in C.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
Arrays An Array is an ordered collection of variables
7 Arrays.
Arrays.
CNG 140 C Programming (Lecture set 8)
Chapter 8 Arrays Objectives
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 7 The Java Array Object © Rick Mercer.
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Data Structures (CS212D) Week # 2: Arrays.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Review of Everything Arrays
Chapter 8 Spring 2006 CS 101 Aaron Bloomfield
Arrays Week 2.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
Single-Dimensional Arrays chapter6
Arrays I Handling lists of data.
7 Arrays.
Chapter 8 Arrays Objectives
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
ICS103 Programming in C Lecture 12: Arrays I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Review for Midterm 3.
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
Java Coding 6 David Davenport Computer Eng. Dept.,
Week 7 - Monday CS 121.
Presentation transcript:

Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average. in addition, the program will prompt the user for a student name, and then return that student’s grade (to double-check the grades entered). This should go on forever until “quit” is entered

Background Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional Array: A Java mechanism for storing an entire list of values of the same type in one location in memory using one variable name

All about arrays Array definition Array Initialization Element access: array subscripting using for loops Array as objects data field ‘length’ and methods (equals, clone) array of objects constant arrays Copying an array shallow copying Array of arrays (multidimensional arrays) Array algorithms reverse, search, sort

Array definition Without initialization ElementType[] variableName; Examples: int[] grades; String[] studentNames;

Array definition With initialization ElementType[] variableName = new ElementType[n]; Examples: int[] grades = new int[5]; Or, int numOfStudent = stdin.nextInt(); String[] studentNames = new String[numOfStudent];

Array Initialization int[] grades; // variable grades is un-initialized int[] grades = new int[5]; variable grades is an array of five integers, initialized as 0 by default Generally such initializations set the array size and initialize each element as: 0 for integer arrays ‘\0’ for char arrays false for boolean arrays null for object arrays

Explicit initialization Another way to initialize an array is to use a list of : ElementType[] var = {exp0, exp1, ... expn}; each exp is an expression that evaluates to type ElementType Examples: int[] grades = {90, 75, 60}; String[] studentNames = {“Joe Smith”, “Sara Smith”}; Rectangle[] r = ?

Explicit initialization Another way to initialize an array is to use a list of : ElementType[] var = {exp0, exp1, ... expn}; each exp is an expression that evaluates to type ElementType Examples: int[] grades = {90, 75, 60}; String[] studentNames = {“Joe Smith”, “Sara Smith”}; Rectangle[] r = {new Rectangle(2,4), new Rectangle(3,1)};

Explicit initialization Example String[] puppy = { "nilla“, “darby“, "galen", "panther" }; int[] unit = { 1 }; Equivalent to String[] puppy = new String[4]; puppy[0] = "nilla"; puppy[1] = “darby"; puppy[2] = "galen"; puppy[3] = "panther"; int[] unit = new int[1]; unit[0] = 1;

Element access: array subscripting The element of an array variable, say var, can be accessed by var[0], var[1], ..., var[n-1], or more precisely, var[subscript-expression] The subscript-expression has to evaluate to integer type with a value that falls between 0 and n-1, where n is the number of elements Example: int[] grades = {90, 75, 60}; int total = grades[0] + grades[1] + grades[2];

Consider Segment int[] b = new int[100]; b[-1] = 0; b[100] = 0; Causes two exceptions to be thrown -1 is not a valid index – too small 100 is not a valid index – too large IndexOutOfBoundsException int[] b = new int[100]; // b has 100 elements: b[0], … b[99] b[-1] = 0; // illegal: subscript too small b[100] = 0; // illegal: subscript too large

Element Access: Consider again the example: int[] grades = {90, 75, 60}; int total = grades[0] + grades[1] + grades[2]; What if we need to add the grades of 200 students, it will be cumbersome to add up the grade in the above manner

Using for loops with arrays Assume grades is an array of 200 elements, storing valid grades for 200 students, the following code calculates the average score int total = 0; for (int i=0; i<200; ++i) { total += grades[i]; } int average = total/200;

Using loops for initialization Suppose we want to write a program that prompts a user to enter the number of students and their grades ... System.out.print(“Enter the number of students: “); int numOfStudent = stdin.nextInt(); int[] grades; for (int i=0; i<numOfStudent; ++i) { System.out.print(“enter the grade for student “ + i + “ : ”); grades[i] = stdin.nextInt(); } Any error with the above code?

Using loops for initialization Suppose we want to write a program that prompts a user to enter the number of students and their grades ... System.out.print(“Enter the number of students: “); int numOfStudent = stdin.nextInt(); int[] grades = new int[numOfStudent]; for (int i=0; i<numOfStudent; ++i) { System.out.print(“enter the grade for student “ + i + “ : ”); grades[i] = stdin.nextInt(); } Any error with the above code?

Review Definition without initialization (e.g., int[] grades;) with initialization (e.g., int[] grades = new int[200];) Initialization explicit initialization (int[] grades = {75, 90};) using for loops Element access (syntax: var[subscript-expression]) IndexOutOfBoundsException