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.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Chapter 10.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
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.,
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Array What it is. How to use it How to declare it How to initialize it.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
 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.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
Program Input and the Software Design Process ROBERT REAVES.
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.
Chapter 8 Arrays and Strings
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Data Types Storage Size Domain of all possible values Operations 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Strings, Slide Fundamental Programming Strings.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Lecture 21: Arrays. 2 Lecture Contents: t Declaring and referencing arrays t Array subscripts t Using for/while loops for sequential access t Demo programs.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Arrays Kingdom of Saudi Arabia
CPS120: Introduction to Computer Science
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Dr. Khizar Hayat Associate Prof. of Computer Science
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

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 elements of the array are referenced by sub_scripting the group name. – The index type is integer –In C++ the index range must be 0... n-1 where n is a programmer-defined constant expression. –Parameter passing style Always call by reference (no indication necessary)

Arrays Hold Multiple Values Unlike regular variables, arrays can hold multiple values. Arrays is a collection of homogeneous data type.

18 ShowDiff.cpp #include using namespace std; int main() { const int MAX_ITEMS = 8; float x[MAX_ITEMS]; float average,sum; int i; cout << "Enter " << MAX_ITEMS << " numbers: "; for (i = 0; i < MAX_ITEMS; i++) cin >> x[i]; // Compute the average value. sum = 0.0; for (i = 0; i < MAX_ITEMS; i++) sum += x[i]; average = sum / MAX_ITEMS; cout << "The average value is " << average <<endl <<endl; // Display the difference between each item and the average. cout << "Table of differences between x[i] and the average.\n"; cout << setw (4) << "i" << setw (8) << "x[i]" << setw (14) << "difference" << endl; for (i = 0; i < MAX_ITEMS; i++) cout << setw (4) << i << setw (8) << x[i] << setw (14) << (x[i] - average) << endl; return 0; }

18 ShowDiff.cpp #include void input_array ( int x[], int ); float sum_array( int x[], int ); using namespace std; int main() { const int MAX_ITEMS = 8; float x[MAX_ITEMS],average, sum = 0.0; int i; input_array(x, MAX_ITEMS); // Compute the average value. sum = sum_array(x, MAX_ITEMS); average = sum / MAX_ITEMS; cout << "The average value is " << average <<endl <<endl; // Display the difference between each item and the average. cout << "Table of differences between x[i] and the average.\n"; cout << setw (4) << "i" << setw (8) << "x[i]" << setw (14) << "difference" << endl; for (i = 0; i < MAX_ITEMS; i++) cout << setw (4) << i << setw (8) << x[i] << setw (14) << (x[i] - average) << endl; return 0; }

// // input_array: input data into array // void input_array ( int x[], int size ) { cout << "Enter " << MAX_ITEMS << " numbers: "; for (i = 0; i < MAX_ITEMS; i++) cin >> x[i]; } // // sum_array: sum of array // float sum_array( int x[], int size ) { float sum = 0.0; for (i = 0; i < MAX_ITEMS; i++) sum += x[i]; return sum; }

22 ShowDiff.cpp Program Output Enter 8 numbers: The average value is 2.0 Table of differences between x[i] and the average Ix[I]difference etc

Parallel Arrays Parallel Arrays can be used when array variables are related to each other by position, or subscript. Tip: It is necessary that the data in each array be loaded in the correct order. If we have arrays ‘NumberGrade’ and ‘LetterGrade’ below, each element of NumberGrade is related to an element in LetterGrade by the position in the array. –NumberGrade: –LetterGrade: “A” ”B” ”C” ”D” ”F”

String and Character Array C++ does not have a data type specifically for strings. Many C++ programmers work with strings by manipulating groups of character called character arrays. Then, string class was developed for most updated compiler package which allow you to create string object.

// stringex.cpp #include #include // library for string class using namespace std; int main() { string MyString;// MyString is a string object MyString = "Hello World!"; cout << MyString << '\n'; return 0; }

// char.cpp #include using namespace std; int main() { char myStr[30] = "Hello World!"; // myString is a character array cout << myStr << '\n'; return 0; }

String object and its associated function 1.You can assign the contents of one string object to another string object. 2.You can assign a string literal to a string object. 3.You can assign a character literal to a string object. string MyString1, MyString2 = “ABCDEF”; MyString1 = MyString2; MyString1 = “string literal”; MyString1 = ‘A’; If ( MyString1 == MyString2 ) // string comparison cout << MyString1;// display the content of MyString1 cout << MyString.length();// return the size of the string MyString1 = MyString1 + ‘ ‘;// string concatenation MyString1 += MyString2; getline(cin,MyString1);

Character array and its associated function 1.You can assign the contents of one string object to another string object unless you use strcpy function. 2.You cannot assign a character literal to a string object. 3.You cannot compare the two string with ==, instead you use strcmp or stricmp. 4.You cannot use + to perform string concatenation, instead you use strcat function. char myString1[30], myString2[30] = “ABCDEF”; strcpy(myString1,myString2);// #include myString1 = myString2; Strcpy(mString1. “string literal”); myString1[0] = ‘A’; If ( strcmp(myString1,myString2) )//string.h or cstring //return 0 if equal, //return > 0 if myString1 > myString2, //return < 0 if myString1 < myString2 cout << myString1;// display the content of MyString1 cout << myString.length();// return the size of the string strcat(myString1,” “); // #include strcat(myString1,myString2); cin.getline(myString1,31);

// str_end.cpp #include using namespace std; int main() { string instring; ifstream infile; // Declare file stream named infile. infile.open("strings.txt"); // Open file for input. if (infile) // If no error occurred while opening file input the data from the file. { cout << "The strings in the data file are:\n\n"; while(getline(infile,instring)) { cout << instring << endl; } else // If error occurred, display message. { cout << "An error occurred while opening the file.\n"; } infile.close(); // Close the input file. return 0; }