Case Study: Intersection of Sets

Similar documents
Goals for This Lecture:

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Answers to Review Questions Chapter 7

Basics of I/O Streams and File I/O

Moving from C++ to VBA

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

An Incomplete C++ Primer. University of Wyoming MA 5310

Lecture 2 Notes: Flow of Control

Introduction to Java

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

While Loop. 6. Iteration

Passing 1D arrays to functions.

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Chapter 5 Functions. Introducing Functions

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

7.7 Case Study: Calculating Depreciation

6. Control Structures

A brief introduction to C++ and Interfacing with Excel

Curriculum Map. Discipline: Computer Science Course: C++

Data Structures using OOP C++ Lecture 1

Chapter One Introduction to Programming

Member Functions of the istream Class

13 Classes & Objects with Constructors/Destructors

Chapter 8 Selection 8-1

Comp151. Definitions & Declarations

Pseudo code Tutorial and Exercises Teacher s Version

Why? A central concept in Computer Science. Algorithms are ubiquitous.

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Syllabus OBJECT ORIENTED PROGRAMMING C++

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Answers to Mini-Quizzes and Labs

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Object Oriented Software Design

Formatting Numbers with C++ Output Streams

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Object Oriented Software Design

The While Loop. Objectives. Textbook. WHILE Loops

Chapter 9 Text Files User Defined Data Types User Defined Header Files

Analysis of Binary Search algorithm and Selection Sort algorithm

Ch 7-1. Object-Oriented Programming and Classes

Visual C Tutorial

In this Chapter you ll learn:

Common Beginner C++ Programming Mistakes

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Answers to Selected Exercises

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Functions and Parameter Passing

Conditionals (with solutions)

COSC 181 Foundations of Computer Programming. Class 6

Chapter 5. Selection 5-1

C++ Input/Output: Streams

What is the consequences of no break statement in a case.

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

C++FA 5.1 PRACTICE MID-TERM EXAM

UEE1302 (1102) F10 Introduction to Computers and Programming

Sample Questions Csci 1112 A. Bellaachia

AP Computer Science Java Mr. Clausen Program 9A, 9B

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Query Processing C H A P T E R12. Practice Exercises

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

VB.NET Programming Fundamentals

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Subtopics - Functions Function Declaration Function Arguments Return Statements and values. By Hardeep Singh

Lecture 22: C Programming 4 Embedded Systems

C++ program structure Variable Declarations

Data Structures. Algorithm Performance and Big O Analysis

Appendix K Introduction to Microsoft Visual C++ 6.0

Using Two-Dimensional Arrays

The Payroll Program. Payroll

CSI33 Data Structures

Pitfalls in Embedded Software

JavaScript: Control Statements I

Pointers and Linked Lists

ALGORITHMS AND FLOWCHARTS

LOOPS CHAPTER CHAPTER GOALS

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Chapter 9 Delegates and Events

Stacks. Linear data structures

Chapter 7. Functions 7-1

Illustration 1: Diagram of program function and data flow

Section IV.1: Recursive Algorithms and Recursion Trees

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

Fondamenti di C++ - Cay Horstmann 1

C / C++ Programming Lab manual

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

EP241 Computer Programming

Transcription:

Case Study: Intersection of Sets In algebra, the intersection of two sets is defined as a new set that contains all the values common to both sets. For instance, consider the following two sets, A and B: A = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 B = 2, 4, 8, 12, 14, 20, 25, 28, 30, 32 The only values common to both sets are 2, 4, and 8. The intersection of A and B, which is denoted as A B, is A B = 2, 4, 8 This case study illustrates how array contents can be processed to perform an operation such as finding the intersection of two sets. The program will ask the user to enter two sets of values, each stored in an array. Then it will scan the two arrays looking for values common to both. The common values will be stored in a third array, whose contents are displayed on the screen. Variables Table 1 lists the variables needed. Table 1 Variable set1 set2 intersection numintvalues Description An array of 10 integers to hold the first set An array of 10 integers to hold the second set An array of 10 integers to hold the intersection of set1 and set2 An integer holding the number of intersecting values 1

2 Intersection of Sets Functions Table 2 lists the functions used by the program. Table 2 Function getarray findintersection displayintvalue Description set1 and set2 are passed into the function. It prompts the user to enter 10 values for each array. set1, set2, and intersection are passed into the function. It scans the arrays for values that appear in both. The intersecting values are stored in intersection. This function returns the number of intersecting values found. The intersection array and the numintvalues variable are passed into this function. If numintvalues contains a number greater than zero, the function displays that many elements in the intersection array. If there are no intersecting values, the function displays a a message indicating so. The findintersection Function The findintersection function uses two array parameters, first and second. The arrays set1 and set2 are passed into these parameters. The function uses nested loops to find the values that appear in both arrays. Here is the pseudocode. For each element in the first array For each element in the second array Compare the selected elements in both arrays. If they contain the same value Store the value in the intersect array. Increment the count of intersecting values. End If. End For. End For. Return the count of intersecting values. In the actual code, the outer loop cycles a counter variable (index1) through the values 0 through 9. This variable is used as the subscript for set1. The inner loop also cycles a counter variable (index2) through the values 0 through 9. This variable is used as a subscript for set2. For each iteration of the outer loop, the inner loop goes through all its iterations. An if statement in the inner loop compares first[index1] to second[index2]. Because the inner loop iterates 10 times for each iteration of the outer loop, the function will compare each individual element of the first array to every element of the second array. Here is the C++ code for the function.

Intersection of Sets 3 int findintersection(int first[], int second[], int intersect[], int size) int intcount = 0, // Number of intersecting values index3 = 0; // Subscript variable for intersect array for (int index1 = 0; index1 < size; index1++) for(int index2 = 0; index2 < size; index2++) if (first[index1] == second[index2]) intersect[index3] = first[index1]; index3++; intcount++; return intcount; // Return the number of intersecting values. shows the entire program s source code. 1 // This program allows the user to enter two sets of numbers. 2 // It finds the intersection of the two sets (which is the 3 // set of numbers contained in both sets). The intersecting 4 // values are displayed. 5 #include <iostream> 6 using namespace std; 7 8 // Function Prototypes 9 void getarrays(int [], int [], int); 10 int findintersection(int [], int [], int [], int); 11 void displayintvalues(int [], int); 12 13 int main() 14 15 const int NUM_VALUES = 10; // Number of values in each array 16 int set1[num_values], // First set 17 set2[num_values], // Second set 18 intersection[num_values], // Set containing intersection values 19 numintvalues; // number of values in intersection 20 21 // Get values for the sets. 22 getarrays(set1, set2, NUM_VALUES); 23 24 // Find the intersection of the two sets 25 numintvalues = findintersection(set1, set2, 26 intersection, NUM_VALUES); 27 (program continues)

4 Intersection of Sets (continued) 28 // Display the intersecting values 29 displayintvalues(intersection, numintvalues); 30 return 0; 31 32 33 //******************************************************** 34 // Definition of function getarrays * 35 // This function accepts two int arrays as arguments. * 36 // It prompts the user to enter 10 values for each array * 37 //******************************************************** 38 39 void getarrays(int first[], int second[], int size) 40 41 // Get values for first array. 42 cout << "Enter 10 values for the first set:\n"; 43 for (int index = 0; index < size; index++) 44 cin >> first[index]; 45 46 // Get values for second array. 47 cout << "Enter 10 values for the second set:\n"; 48 for (index = 0; index < size; index++) 49 cin >> second[index]; 50 51 52 53 //************************************************************ 54 // Definition of function findintersection * 55 // This functon accepts three arrays as arguments. * 56 // The first two arrays (first and second) are scanned, * 57 // and all values appearing in both are stored in the * 58 // third array (intersect). The number of values that appear * 59 // in both arrays is returned. * 60 //************************************************************ 61 62 int findintersection(int first[], int second[], int intersect[], int size) 63 64 int intcount = 0, // Number of intersecting values 65 index3 = 0; // Subscript variable for intersect array 66 67 for (int index1 = 0; index1 < size; index1++) 68 69 for(int index2 = 0; index2 < size; index2++) 70 (program continues)

Intersection of Sets 5 (continued) 71 if (first[index1] == second[index2]) 72 73 intersect[index3] = first[index1]; 74 index3++; 75 intcount++; 76 77 78 79 return intcount; // Return the number of intersecting values. 80 81 82 //******************************************************** 83 // Definition of function displayintvalues * 84 // This function accepts two arguments: an array of ints * 85 // and an int. The second argument is the number of * 86 // valid elements contained in the array. * 87 // These values are displayed, if there are any. * 88 //******************************************************** 89 90 void displayintvalues(int intersect[], int num) 91 92 if (!num) 93 cout << "There are no intersecting values.\n"; 94 else 95 96 cout << "Here is a list of the intersecting values:\n"; 97 for (int index = 0; index < num; index++) 98 cout << intersect[index] << " "; 99 cout << endl; 100 101 Program Output with Example Input Shown in Bold Enter 10 values for the first set: 1 2 3 4 5 6 7 8 9 10 Enter 10 values for the second set: 2 4 8 12 14 20 25 28 30 32 Here is a list of the intersecting values: 2 4 8