CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
CS 1430: Programming in C++.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Decisions Chapter 4.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
C++, OBJECT ORIENTED PROGRAMMING
Variables A piece of memory set aside to store data
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Multiple Files Revisited
Array Data Structure Chapter 6
One-Dimensional Array Introduction Lesson xx
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Arrays Kingdom of Saudi Arabia
6 Chapter Functions.
CS150 Introduction to Computer Science 1
Lecture 12 Oct 16, 02.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CISC181 Introduction to Computer Science Dr
7 Arrays.
Functions Divide and Conquer
Chapter 9: Data Structures: Arrays
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
Understanding Conditions
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
C++ winter2008(by:J.Razjouyan)
CS150 Introduction to Computer Science 1
Fundamental Programming
CS250 Introduction to Computer Science II
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 9: Pointers and String
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
各題答對人數.
Variables and Constants
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

CS150 Introduction to Computer Science 1 Chapter 9: Arrays Arrays are a way to store more than one value for a variable Example: Store 10 grades Using single variables--need 10 variables Using array--need one array of size 10 Declaration: float grade[10]; string name[2]; 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Using Arrays Declaration float temp[5]; Assign temp[0] = 32.0; temp[1] = 55.3; temp[2] = 72.1; temp[3] = 85.0; temp[4] = 20.0; for (int i = 0; i < 5; i++) temp[i] = 0.0; float temp[] = {32.0, 55.3, 72.1, 85.0, 20.0}; 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Memory Storage Single variable--one memory location. Size? Array--multiple memory locations. Size? 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Accessing Arrays After initialized, can read array Make sure don’t try to access array outside of bounds Indices should be 0 through size -1 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 General Form datatype identifier[size]; Same as regular variable declaration, but need to give size of array. Size must be constant integer expression greater than zero. Index array through [ ]. Indices must be integers (or integer expressions) in the range 0 through size - 1. There is no array[size] element!! 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Output? const int size = 5; int i,j,k; int vals[size] = {10, 9, 8, 7}; char grades[] = {'A', 'B', 'C', 'D', 'F'}; i = 2; j = 3; k = 7; cout << vals[2] << vals[j] << vals[j-i]; cout << grades[j] << grades[j%i] << grades[i%j] << grades[k/i]; cout << int (grades[i]) << grades[vals[1]%4]; cout << grades[vals[1]]; 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problems P#1: Write a C++ program segment that will switch the values in the first and third elements of the array values. P#2: Write a C++ program segment that will declare an integer array nums of size 100. Then place the first 100 positive even integers into the array nums starting with the first element and proceeding to the end of the array. P#3: A data file of grades exists with an unknown number of characters. Write a C++ program segment that will read these characters into a character array. Assume no more than 1000 characters exists in the data file. 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Passing by Reference int i,j; int arry1[] = {1,2,3,4,5}; int arry2[] = {5,4,3,2,1}; i = 2; j = 3; ... void swap (int& num1, int& num2) { int temp; temp = num1; num1 = num2; num2 = temp; } What happens? a) swap(i, arry1[1]); b) swap(arry1[2], arry1[3]); c) swap(arry1[i],arry2[i+1]); d) swap(i, arry1[1]+i); e) swap(arry1, arry2); 4/8/2019 CS150 Introduction to Computer Science 1

Passing arrays to functions Can pass individual elements If pass whole array, it is automatically passed by reference. Why do you think that is? The address of the array is passed to the function so that any element of the array can be accessed The address of an array is the memory location of the first element 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write the function definition for a ‘large’ function that stores the larger of each element in arry1 and arry2 in arry3. If the program works, arry3 should have {5,4,3,4,5}. 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Solution void large (int size, int arry1[], int arry2[], int arry3[]); void main() { const int size = 5; int arry1[] = {1,2,3,4,5}; int arry2[] = {5,4,3,2,1}; int arry3[size]; large (size, arry1, arry2, arry3); } 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Arrays used as input What happens when want to use array only as input? We can’t pass it by value… void large (int size, const int arry1[], const int arry2[], int arry3[]); We can protect array arguments by putting const in front of them in prototype and function definition 4/8/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Assume you have two arrays of floats called vals1 and vals2. They both contain maxels elements. Write a C++ bool function identical that will accept both arrays and return true if both arrays are identical; otherwise, return false. The call to your function might be by a statement of the following form: if (identical (maxels, vals1, vals2)) cout << "Arrays are identical" << endl; else cout << "Arrays are not identical" << endl; 4/8/2019 CS150 Introduction to Computer Science 1