Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Passing Arrays to Functions Programming. COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters l Arrays are.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Programming Functions: Passing Parameters by Reference.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Review of C++ Basics.
C++ for Engineers and Scientists Third Edition
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 7 Single-Dimensional.
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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 6 Arrays.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 6: Arrays 数据序列, 数组 1.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Single-Dimensional Arrays and C-Strings.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Module 1: Array ITEI222 - Advance Programming Language.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Chapter 6 Arrays DDC 2133 Programming II.
Chapter 6 Arrays Lecturer: Mrs Rohani Hassan
Engineering Problem Solving with C++, Etter
Programming fundamentals 2 Chapter 1:Array
Basic Array Definition
C++ Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays Solution Opening Problem
Data type List Definition:
Counting Loops.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Chapter 7 Single-Dimensional Arrays and C-Strings
Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction.
Programming Fundamental
Programming Arrays.
Presentation transcript:

Arrays Programming

COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection of integers, collection of characters, collection of doubles).

COMP102 Prog. Fundamentals I: Arrays / Slide 3 Arrays l 1-dimensional array. l 3-dimensional array (3rd dimension is the day). Oct 15 Oct 16 Oct 14

COMP102 Prog. Fundamentals I: Arrays / Slide 4 Array Applications l Given a list of test scores, determine the maximum and minimum scores. l Read in a list of student names and rearrange them in alphabetical order (sorting). l Given the height measurements of students in a class, output the names of those students who are taller than average.

COMP102 Prog. Fundamentals I: Arrays / Slide 5 Array Declaration l Syntax: [ ] Ex. int Ar[10]; The array elements are all values of the type. The size of the array is indicated by, the number of elements in the array. must be an int constant or a constant expression. Note that an array can have multiple dimensions.

COMP102 Prog. Fundamentals I: Arrays / Slide 6 Array Declaration // array of 10 uninitialized ints int Ar[10]; -- Ar

COMP102 Prog. Fundamentals I: Arrays / Slide 7 Subscripting l Declare an array of 10 integers: int Ar[10]; // array of 10 ints To access an individual element we must apply a subscript to array named Ar. n A subscript is a bracketed expression. –The expression in the brackets is known as the index. n First element of array has index 0. Ar[0] n Second element of array has index 1, and so on. Ar[1], Ar[2], Ar[3],… n Last element has an index one less than the size of the array. Ar[9] l Incorrect indexing is a common error.

COMP102 Prog. Fundamentals I: Arrays / Slide 8 Subscripting // array of 10 uninitialized ints int Ar[10]; Ar[3] = 1; int x = Ar[3]; -- 1 Ar Ar[4]Ar[5]Ar[6]Ar[3]Ar[0]Ar[2]Ar[8]Ar[9]Ar[7]Ar[1] 1 --

COMP102 Prog. Fundamentals I: Arrays / Slide 9 Subscripting Example 1 //For loop to fill & print a 10-int array #include using namespace std; int main ( ) { int index, ar[10]; // array for 10 integers // Read in 10 elements. cout << "Enter 10 integers: "; for(index = 0; index < 10; index ++) cin >> ar[index]; cout << endl; cout << "The integers are "; for(index = 0; index < 10; index ++) cout << ar[index] << " "; cout << endl; return 0; }

COMP102 Prog. Fundamentals I: Arrays / Slide 10 Sorting with Arrays: Ex. 2 // Compare and sort three integers void swap (int&, int&); int main ( ) { int ar[3]; // input integers // Read in three elements. cout << "Enter three integers: "; cin >> ar[0] >> ar[1] >> ar[2]; if (ar[0] > ar[1]) swap (ar[0], ar[1]); if (ar[1] > ar[2]) swap (ar[1], ar[2]); if (ar[0] > ar[1]) swap (ar[0], ar[1]); cout << "The sorted integers are " << ar[0] <<", " << ar[1] << ", " << ar[2] << endl; return 0; }

COMP102 Prog. Fundamentals I: Arrays / Slide 11 Swapping Function: Ex. 2 // Function for swapping two integers void swap (int& first, int& second) { int temp; temp = first; first = second; second = temp; }

COMP102 Prog. Fundamentals I: Arrays / Slide 12 Array Element Manipulation Ex. 3 l Consider int Ar[10], i = 7, j = 2, k = 4; Ar[0] = 1; Ar[i] = 5; Ar[j] = Ar[i] + 3; Ar[j+1] = Ar[i] + Ar[0]; Ar[Ar[j]] = 12; cin >> Ar[k]; // where the next input value is Ar Ar[4]Ar[5]Ar[6]Ar[3]Ar[0]Ar[2]Ar[8]Ar[9]Ar[7]Ar[1]

COMP102 Prog. Fundamentals I: Arrays / Slide 13 Array Initialization Ex. 4 int Ar[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; Ar[3] = -1; Ar Ar

COMP102 Prog. Fundamentals I: Arrays / Slide 14 Initializing arrays with random values The following loop initializes the array myList with random values between 0 and 99: for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = rand() % 100; }

COMP102 Prog. Fundamentals I: Arrays / Slide 15 Program with Arrays int main() { int values[5]= {11,1,3,6,10}; for (int i = 1; i < 5; i++) { values[i] = values[i] + values[i-1]; } values[0] = values[1] + values[4]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 16 Printing arrays To print an array, you have to print each element in the array using a loop like the following: for (int i = 0; i < ARRAY_SIZE; i++) { cout << myList[i] << " "; }

COMP102 Prog. Fundamentals I: Arrays / Slide 17 Copying Arrays Can you copy array using a syntax like this? list = myList; This is not allowed in C++. You have to copy individual elements from one array to the other as follows: for (int i = 0; i < ARRAY_SIZE; i++) { list[i] = myList[i]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 18 Summing All Elements Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this: double total = 0; for (int i = 0; i < ARRAY_SIZE; i++) { total += myList[i]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 19 Finding the Largest Element Use a variable named max to store the largest element. Initially max is myList[0]. To find the largest element in the array myList, compare each element in myList with max, update max if the element is greater than max. double max = myList[0]; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) max = myList[i]; }

COMP102 Prog. Fundamentals I: Arrays / Slide 20 Finding the smallest index of the largest element double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; }

COMP102 Prog. Fundamentals I: Arrays / Slide 21 Shifting Elements double temp = myList[0]; // Retain the first element // Shift elements left for (int i = 1; i < myList.length; i++) { myList[i - 1] = myList[i]; } // Move the first element to fill in the last position myList[myList.length - 1] = temp;

#include // Random Shuffling #include using namespace std; const int ARRAY_SIZE = 10; int main(){ int myList[ARRAY_SIZE]= {1,2,3,4,5,6,7,8,9,10}; srand(time(0)); for ( int i = 0; i < ARRAY_SIZE; i++) { // Generate an index randomly int index = rand() % ARRAY_SIZE; int temp = myList[i]; myList[i] = myList[index]; myList[index] = temp; } for ( int i = 0; i < ARRAY_SIZE; i++) cout << myList[i] <<' '; cout << endl; return 0;}

#include // Create 6 random numbers for Mark 6 #include using namespace std; const int ARRAY_SIZE = 6; int main(){ int used[50] ={0};//indicate if a random number is used. int Mark[6], temp; srand(time(0)); int i = 0; while (i < 6){ temp = rand()%49 + 1; if (used[temp] == 0) {//temp is not used Mark[i] = temp; i++; used[temp] = 1; } for ( int i = 0; i< 6; i++) cout << Mark[i] << endl; return 0; }

#include //Print numbers in reversed order #include using namespace std; int main(){ srand(time(0)); int numbers[500]; int num_num; cout<< "How many numbers (up to 500)?"; cin >> num_num; for ( int i= 0; i < num_num; i++) numbers[i] = rand()%100; cout << "\n Your numbers reversed are:\n"; for ( int j= num_num-1 ; j >= 0; j--) cout << setw(3) << numbers[j]; cout << endl; return 0;}

#include //print histogram #include using namespace std; int main(){ ifstream ins; int count[26]={0}; char next; ifstream input_stream; char input_file_name[16]; // declare the input file names cout << "Enter the input file name (max 15 characters):\n"; cin >> input_file_name; ins.open(input_file_name); // open the file ins >> next; // get the first char while(!ins.eof()){ // loop to read each line next = tolower(next); if (isalpha(next)) count[ next - 'a']++; ins >> next; } ins.close(); for (char i = 'a'; i <='z'; i++) // print_histogram(count); cout << i << ": " << count[i-'a'] << endl; return 0; }

void print_histogram( int list[]){ int max = list[0]; for (int i = 1; i < 26; i++){ if (max < list[i]) max = list[i]; } double ratio= 70.0 /max ; for ( i = 0; i < 26; i++){ cout << char ('a'+i) <<": “ << setw(5) <<list[i] <<‘ ‘; int count = list[i]* ratio; for ( int j = 0; j < count; j++) cout <<'*'; cout << endl; } cout << endl; }