Case Study 2 – Marking a Multiple-choice Test

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Advertisements

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
Computer Programming Lab(4).
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 10 Title: Arrays, Part 2 Multidimensional Arrays Reference: MalikFarrell, chap 1,
Java Programming: From the Ground Up
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.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 8 Multidimensional Arrays Lecture.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Classes - Intermediate
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 11 Lists for Multi-dimensional Data.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
CS 201 Tarik Booker California State University, Los Angeles.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Multidimensional Arrays.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Array 1 ARRAY. array 2 Learn about arrays. Explore how to declare and manipulate data into arrays. Understand the meaning of “array index out of bounds.”
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 Chapter 7 Multidimensional Arrays. 2 Motivations You can use a two-dimensional array to represent a matrix or a table.
Chapter 8 Multidimensional Arrays
Two-Dimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 6 Arrays DDC 2133 Programming II.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
Data Structures Array - Code.
Chapter 8 Multidimensional Arrays
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
Exam 2 Review 1.
Chapter 8 Multidimensional Arrays
Chapter 8 Multi-Dimensional Arrays
Java Language Basics.
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Data Structures Array - Code.
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Multidimensional Arrays
Multidimensional array
Week 4 Lecture-2 Chapter 6 (Methods).
Chapter 8 Multidimensional Arrays
Arrays in Java.
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Building Java Programs
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Chapter 7 Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Presentation transcript:

Case Study 2 – Marking a Multiple-choice Test In this case study, we learn: How to design a solution for a given problem How to use a two-dimensional array to represent a collection of multiple-choice test answers for a list of students How to process the two-dimensional array How to write, compile, run and test a program to implement the designed solution. 1 1 1

Problem Specification The problem is to represent a collection of test answers for eight students, with each student having answers to ten questions , compare each set of test answers with the set of key answers, and count and print the number of correct answers. 2 2 2 2 2 2 2 2

How Would We Do it - Design? Top-level design 1. Declare, create and initialise an array, answers[8] [10] to represent the test answers 2. Declare, create and initialise an array, keys[10], to represent the key answers 3. Count and print out the number of correct answers of each student 3 3 3 3 3 3 3

How Would We Do it - Design? Final design in pseudo code 1. Declare, create and initialise an array, answers[8][10], to represent the test answers 2. Declare, create and initialise an array, keys[10], to represent the correct answers 3.1. For i = 0 to number of rows 3.2. Set count to 0 3.3. For j = 0 to number of answers - 1 3.4. If answer[i][j] = key[j] then 3.5. Increment count by 1 3.6. Print out student j + count 4 4 4 4 4 4 4 4

Implementation public class GradeExam { public static void main(String[] args) { // Declare, create and initialise an array to represent answers char[][] answers = { {'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'}, {'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'}, {'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'}, {'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}, {'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}}; // Declare, create and initialise an array to represent correct answers char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'}; // Count correct answers of each students for (int i = 0; i < answers.length; i++) { // Count correct answers of an individual student int correctCount = 0; for (int j = 0; j < answers[i].length; j++) { if (answers[i][j] == keys[j]) correctCount++; } System.out.println("Student " + i + "'s correct count is " + correctCount); 5 5 5 5 5 5 5 5 5 5 5 5 5

Two-dimensional Array Basics Two subscripts are used in a two-dimensional array, one for the row and the other for the column, with the index for each subscript of int type starting from 0. 6 6 6 6 6 6 6 6 6

Processing Two-dimensional Arrays Suppose an array, matrix, is created as follows: int[][] matrix = new int[4][4]; Obtaining the lengths of two dimensional array: A two-dimensional array is a one-dimensional array in which each element is also a one-dimensional array. The matrix is a one-dimensional array of 4 elements, matrix[0], matrix[1], matrix[2], matrix[3], each of which is a one-dimensional array of 4 integers. The method length returns the length of a one-dimensional array. For example matrix.length returns 4, and each of matrix[0].length, matrix[1].length, matrix[2].length, matrix[3].length returns 4. 7 7 7 7 7 7 7 7 7 7 7

Processing Two-dimensional Arrays Initializing arrays with user input values: java.util.Scanner input = new Scanner(System.in); System.out.println("Enter " + matrix.length + ” rows and " + matrix[0].length + " columns: "); for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { matrix[row][column] = input.nextInt(); } 8 8 8 8 8 8 8 8 8 8 8

Processing Two-dimensional Arrays Printing arrays: for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { System.out.print(matrix[row][column] + " "); } System.out.println(); 9 9 9 9 9 9 9 9 9 9 9 9

Processing Two-dimensional Arrays Summing all elements: int total = 0; for (int row = 0; row < matrix.length; row++) { for (int column = 0; column < matrix[row].length; column++) { total += matrix[row][column]; } 10 10 10 10 10 10 10 10 10 10 10 10 10

Processing Two-dimensional Arrays Finding the row with the largest sum: int maxRow = 0; int indexOfMaxRow = 0; // Set sum of the first row to maxRow for (int column = 0; column < matrix[0].length; column++) { maxRow += matrix[0][column]; } for (int row = 1; row < matrix.length; row++) { int totalOfThisRow = 0; for (int column = 0; column < matrix[row].length; column++) totalOfThisRow += matrix[row][column]; if (totalOfThisRow > maxRow) { maxRow = totalOfThisRow; indexOfMaxRow = row; System.out.println("Row " + indexOfMaxRow + " has the maximum sum of " + maxRow); 11 11 11 11 11 11 11 11 11 11 11 11 11 11

Processing Two-dimensional Arrays Summing elements by column: for (int column = 0; column < matrix[0].length; column++) { int total = 0; for (int row = 0; row < matrix.length; row++) total += matrix[row][column]; System.out.println("Sum for column " + column + " is “ + total); } How to sum elements by row? 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12