C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Arrays and Strings.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
EXERCISE IN CLASS CHAPTER 3. PART 1 IDENTIFIER SCENARIO 1 o record1 o 1record o file_3 o return o $tax o Name o name and address o name-and-address o.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Multiple-Subscripted Array
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Basic Elements of C++ Chapter 2.
# include using namespace std; int Square ( int ); int Cube ( int ); int main () { cout
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
C Tokens Identifiers Keywords Constants Operators Special symbols.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
CISC105 – General Computer Science Class 9 – 07/03/2006.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Introduction to Programming
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
CPS120: Introduction to Computer Science Variables and Constants.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
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.
Last week: We talked about: History of C Compiler for C programming
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Declarations CSCI N305
Basic Elements of C++.
Objectives Identify the built-in data types in C++
Lecture-5 Arrays.
Programming Languages and Paradigms
Basic Elements of C++ Chapter 2.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Arrays, For loop While loop Do while loop
C Arrays.
C Arrays.
7 Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
7 Arrays.
Arrays in Java.
Arrays Arrays A few types Structures of related data items
Lecture 14: Problems with Lots of Similar Data
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Variables and Constants
ㅎㅎ Fourth step for Learning C++ Programming Call by value
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

C LANGUAGE UNIT 3

UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays

The concept of array Array: a set of ordered data items You can define a variable called x, which represents not a single value, but an entire set of values. Each element of the set can then be referenced by means of a number called an index number or subscript. Mathematics: a subscripted variable, x i, refers to the ith element x in a set C programming: the equivalent notation is x[i]

Declaring an array Declaring an array variable: – Declaring the type of elements that will be contained in the array—such as int, float, char, etc. – Declaring the maximum number of elements that will be stored inside the array. The C compiler needs this information to determine how much memory space to reserve for the array.) This must be a constant integer value The range for valid index values in C: – First element is at index 0 – Last element is at index [size-1] – It is the task of the programmer to make sure that array elements are referred by indexes that are in the valid range ! The compiler cannot verify this, and it comes to severe runtime errors !

Arrays - Example Example: int values[10]; Declares an array of 10 elements of type int Using Symbolic Constants for array size: #define N 10 … int values[N]; Valid indexes: values[0]=5; values[9]=7; Invalid indexes: values[10]=3; values[-1]=6; In memory: elements of an array are stored at consecutive locations

Initializing arrays int counters[5] = { 0, 0, 0, 0, 0 }; char letters[5] = { 'a', 'b', 'c', 'd', 'e' }; float sample_data[500] = { 100.0, 300.0, }; The C language allows you to define an array without specifying the number of elements. If this is done, the size of the array is determined automatically based on the number of initialization elements: int counters[] = { 0, 0, 0, 0, 0 };

Character arrays #include int main (void) { char word[] = { 'H', 'e', 'l', 'l', 'o', '!' }; int i; for ( i = 0; i < 6; ++i ) printf ("%c", word[i]); printf ("\n"); return 0; }