Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Programming and Data Structure
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Kernighan/Ritchie: Kelley/Pohl:
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
 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.
C++ for Engineers and Scientists Third Edition
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
POINTERS.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Learners Support Publications Constructors and Destructors.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
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?
Constructors and Destructors
Chapter 7: User-Defined Functions II
Constructors and Destructors
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
MSIS 655 Advanced Business Applications Programming
Presentation transcript:

Array in C++ / review

An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred to as array elements, can be addressed using a number, called index or subscript. The array is the most commonly used data storage structure. It helps to see how object-oriented programming and data structures relate to each other.

Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [ no of elements]; You can create an array from any type with the exception of some special types. An array always occupies a contiguous memory space. To know how much memory spaces an array needs, for example, if we have an array of 10 elements, this space is 10*sizeof(float) = 40 bytes.

when declaring an array, there is a possibility to assign initial values to each one of its elements. To access the values of an array; being able to both read and modify its value by using this format : name[index]

# include using namespace std; int main() { int n[10]; for( int i=0;i<10;i++) n[i] =0; cout<<“Element”<<setw(13)<<“value”<<endl; for(int j=0; j<10;j++) cout<<setw(7)<<j<<setw(13)<<n[j]<<endl; return 0; }

 How to assign initial values to a char array : char name[ ] = “welcome back"; This definition is equivalent to char name[ ] = {‘w’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’, ‘ ‘, ‘b’, ‘a’, ‘c’, ‘k’, ‘\0’};

Multidimensional arrays can be described as "arrays of arrays". Multidimensional arrays are not limited to two indices. #define WIDTH 3 #define HEIGHT 2 int table [HEIGHT][WIDTH]; int n,m; int main () { for (n=0;n<HEIGHT; n++) for (m=0; m<WIDTH; m++) { table [n][m]=(n+1)*(m+1); } return 0; }

Array Advantages: Easier to declare and use. Can be used with most of the data types. Array Disadvantages: Fixed size data. Ex: if the number of elements to be stored are more than the maximum size, the array cannot accommodate those new values.

Functions/ review

FUNCTIONS If you are looking to provide a solution for a more complex problem, it will help to divide the problem into smaller units. Functions can be defined in any order, however, the first function is normally main. Definition syntax: type name ( parameter1, parameter2,...) { statements }

Prototype and Definition In a function definition, the function header is similar in form to the prototype(declaration) of a function. State the differences between the prototype and function declaration? When do the function must be declare? The prototype provides the compiler with all the information it needs when a function is called, what are these information and what will happen after that? Can the function declaration be omitted ? When ?

■ RETURN VALUE OF FUNCTIONS When a function is called, an argument of the same type as the parameter must be passed to the function for each parameter. Note: the arguments can be also any kind of expressions. If the function is any type other than void, the return statement will cause the function to return a value to the function that called it. When the program flow reaches a return statement or the end of a function code block, where it goes?

■ PASSING ARGUMENTS Passing values to a function when the function is called is referred to as passing by value, does it access the object it self directly? However, function arguments can also be passed by reference.

16

Other topics to review Overloading function. Inline function. Recursion. Default arguments.

Any Question? Refer to chapter 1 of the book for further reading