Using Parallel Arrays. Parallel Array Example

Similar documents
Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

7 Arrays. 7.1 Arrays Hold Multiple Values TOPICS. CONCEPT: An array allows you to store and work with multiple values of the same data type.

Arrays. Arizona State University 1

Functions, Arrays & Structs

Functions, Arrays & Structs

ET 181. Purpose: The purpose of this lab is to become familiar with using arrays in C++.

CMPS 221 Sample Final

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lecture 23: Pointer Arithmetic

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

BITG 1113: POINTER LECTURE 12

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

BITG 1113: Function (Part 2) LECTURE 5

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CSCE Practice Midterm. Data Types

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Exam 3 Chapters 7 & 9

Week 3: Pointers (Part 2)

LAB 4.1 Relational Operators and the if Statement

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE Practice Midterm. Data Types

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Name Section: M/W or T/TH. True or False (14 Points)

BITG 1113: Array (Part 1) LECTURE 8

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

True or False (12 Points)

Programming Language. Functions. Eng. Anis Nazer First Semester

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

CMSC 202 Midterm Exam 1 Fall 2015

CS242 COMPUTER PROGRAMMING

Name SECTION: 12:45 2:20. True or False (12 Points)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Search Algorithms. Linear Search Binary Search

Arrays in Functions!

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

BITG 1113: Array (Part 2) LECTURE 9

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Chapter 17 - Notes Recursion

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Arrays. Week 4. Assylbek Jumagaliyev

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

In this chapter you will learn:

Exercise 1.1 Hello world

4. C++ functions. 1. Library Function 2. User-defined Function

Computer Programming

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Local and Global Variables

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

CS2255 HOMEWORK #1 Fall 2012

Chapter 10 - Notes Applications of Arrays

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Chapter 2: Basic Elements of C++

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

1. In C++, reserved words are the same as predefined identifiers. a. True

CSCE 206: Structured Programming in C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

A First Program - Greeting.cpp

Module 7 b. -Namespaces -Exceptions handling

CS2141 Software Development using C/C++ C++ Basics

1. Word Analysis: (Which nouns suggest a need for memory (a variable) and which verbs suggest a need for action (a function maybe).

Programming in C++: Assignment Week 8

Chapter 7 - Notes User-Defined Functions II

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

CS 240 Computer Programming 1 Arrays

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Lab Instructor : Jean Lai

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

Unit 14. Passing Arrays & C++ Strings

LAB 13: ARRAYS (ONE DIMINSION)

Topics. Functions. Functions

Constructors.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Transcription:

Using Parallel Arrays Parallel arrays: two or more arrays that contain related data A subscript is used to relate arrays: elements at same subscript are related Arrays may be of different types Parallel Array Example const int SIZE = 5; // Array size int id[size]; // student ID double average[size]; // course average char grade[size]; // course grade... for(int i = 0; i < SIZE; i++) cout << "Student ID: " << id[i] << " average: " << average[i] << " grade: " << grade[i] << endl; 1

Parallel Array Example (Program Continues) Parallel Array Example Program 7-12 (Continued) 2

Parallel Array Example The hours and payrate arrays are related through their subscripts: Arrays as Function Arguments To pass an array to a function, just use the array name: showscores(tests); To define a function that takes an array parameter, use empty [] for array argument: void showscores(int []); // function prototype void showscores(int tests[])// function header 3

Arrays as Function Arguments When passing an array to a function, it is common to pass array size so that function knows how many elements to process: showscores(tests, ARRAY_SIZE); Array size must also be reflected in prototype, header: void showscores(int [], int); // function prototype void showscores(int tests[], int size) // function header Arrays as Function Arguments - example (Program Continues) 4

Arrays as Function Arguments - example Program 7-14 (Continued) Modifying Arrays in Functions Array names in functions are like reference variables changes made to array in a function are reflected in actual array in calling function Need to exercise caution that array is not inadvertently changed by a function 5

The following program skeleton, when completed, will ask the user to enter 10 integers which are stored in an array. The function avgarray, which you must write, is to calculate and return the average of the numbers entered. #include <iostream> //Write your function prototype here int main() const int SIZE = 10; int usernums[size]; cout << "Enter 10 numbers: "; for (int count = 0; count < SIZE; count++) cout << "#" «(count + 1) << " "; cin >> usernums[count]; cout << "The average of those numbers is "; cout << avgarray(usernums, SIZE) << endl; return 0; //Write the function avgarray here. #include <iostream> using namespace std; void Test(int []); int main() int myarr [4]=3,4,5,6; for(int i=0;i<5;i++) cout<<myarr[i]<<" "; cout<<endl; Test(myArr); cout<<endl; for(int i=0;i<4;i++) cout<<myarr[i]<<" "; system("pause"); return 0; void Test(int z[]) int temp=z[3]; z[3]=z[0]; z[0]=temp; for(int j=0;j<4;j++) cout<<z[j]<<" "; 6

#include <iostream> using namespace std; void Test(int, int,int[]); int main() int x = 1; int y[3]; y[0]=1; Test(x,y[0],y); cout<<"x is: " << x<< endl; cout<<"y[0] is: " <<y[0] << endl; for(int i=0;i<3;i++) cout<<y[i]<<endl; system("pause"); return 0; void Test(int num, int num1, int z[]) num=1001; num1=290; z[1]=34; z[2]=35; What is the output of the following code? (You may need to use a calculator.). const int SIZE = 5; int time[size] = 1, 2, 3, 4, 5, speed[size] = 18, 4, 27, 52, 100, dist[size]; for (int count = 0; count < SIZE; count++) dist[count] = time[count] * speed[count]; for (int count = 0; count < SIZE; count++) cout << time[count] << " "; cout << speed[count] << " "; cout «dist[count] << endl; 7

Write a program that store the populations of 12 countries. Define 2 arrays that may be used in parallel to store the names of the countries and their populations. Write a loop that uses these arrays to print each country s name and its population. Each of the following definitions and program segments has errors. Locate as many as you can and correct the errors. a) void showvalues(int nums) for(int i = 0; i<8; i++) cout<<nums[i]; b) void showvalues(int nums [4]) for(int i = 0; i<8; i++) cout<<nums[i]; 8

Consider the following function prototypes: void funcone(int [], int); int findsum(int, int); And the declarations: int list[50]; int num; Write a C++ statements that: a) Call the function funconewith the actual parameters, list and 50 respectively. b) Print the value returned by the function funcsumwith the actual parameters, 50, and the fourth element of list respectively. c) Print the value returned by the function funcsumwith the actual parameters, the thirtieth and tenth elements of list, respectively. Write a program that has two overloaded functions that return the average of an array with the following headers: int average(int array[], int size) double average(int array[], int size Use 1,2,3,4,5,6 and 6.0,4.4,1.9,2.9,3.4,3.5 to test the functions. 9

Write a program that has a function that returns the index of the smallest element in an array of integers. If there are more than one such elements, return the smallest index. Use 1,2,4,5,10,100,2,-22 to test the function. 10