Arrays and Strings.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Chapter 7: Arrays In this chapter, you will learn about
Chapter 8: Arrays.
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
C Language.
One Dimensional Arrays
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Kernighan/Ritchie: Kelley/Pohl:
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
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 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays.
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
A First Book of ANSI C Fourth Edition
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
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.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Pointers *, &, array similarities, functions, sizeof.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 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.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Programming for Engineers
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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?
Objectives You should be able to describe: One-Dimensional Arrays
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Array in C# Array in C# RIHS Arshad Khan
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays I Handling lists of data.
Arrays in Java.
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Arrays and Strings

Speciality Conventional variables also called scalar variables can only store only one value. The stored value is atomic type.(It can not be further sub-devided into legitimate data types. Ex : integer, floating point, double precision data.

Specialty Atomic data types can be represented as char ch; int n; float price; Arrays are Aggregate type of data. Also known as structured data type or as Data structure. The values can be decomposed and operations are available to retrieve and update individual values. Hence these are further called as Derived data types.

Why Array? Can be used to perform certain operations like To print the numbers in the reverse order. Ex: Suppose problem is to print three numbers in reverse order. The program can be written as

Why Array? void main() { int n1,n2,n3; printf(“\n enter the values”); scanf(“%d %d %d”,&n1,n2,n3); printf(“\nThe numbers in the reverse oreder are %d,%d,%d”,n3,n2,n1); }

Why Array? But problem would be severe if number of inputs are more . Also it is required that the number of inputs must match with that the user expects.

Why Array? To find out the average of numbers and to find out the number which is larger than the average. This problem requires To read all the numbers and to calculate the average. To read again to compare the entered numbers against the calculated average.(difficult as it involves entering values twice and accuratly with no mistakes).

Definition A data structure consisting of an ordered set of data values of homogeneous(same) type. The individual data elements are Ordered Fixed in size homogeneous

One-dimensional Array Have a single subscript or index whose value refers to the individual array element ranging from 0 to (n-1), where n is the total number of elements in the array. Declaration (Requirements) The type of data it can hold (int,char, float etc). The number of values it can hold i.e the maximum number of elements it can hold A name.

One-dimensional Array Syntax : Data_type array_name [size]; Ex : int a[100]; An array element can be referenced as <array name>[<index>] In the above example the array index starts at 0, and the elements are referred as a[0], a[1],a[2], … ………., a[99].

One-dimensional Array Each array element is called an indexed variable or subscripted variable. Internally the index is used as an offset from the array’s starting position. a[0] a[1] a[2] a[3] a[4]

One-dimensional Array It is mandatory to give the size of the array. We can not write int a[]; Here the compiler will generate error. Variable size declaration is also not allowed. Ex : int n; double x[n], y[n]; This kind of declaration is illegal in c and will generate error.

One-dimensional Array However declaration is allowed in terms of symbolic constant. Ex : #define n 20 void main() { double x[n], y[n]; }

One-dimensional Array Since the number of array elements can also be given by an expression int x[n+1]; is also allowed. But there must be no unknowns Note : C never checks the array index neither during compilation nor during runtime. wrong indexed elements are easily accepted but gives unpredictable results.

One-dimensional Array Initialization : Initializing the array elements int arr[ ] ={2,5,4,7,9}; Here the compiler will deduce the size of the array from the initialization element. This is known as Automating sizing.

Accessing Array Elements Single operations involving entire arrays are not permitted. Ex : if a[] and b[] are arrays of int type then we can not write a=0; or b=a; Opeartions like assignment, comparision can be carried out on element-by-element basis using loops. Ex: a[3]=10; a[3]=2*(a[0]-4);

Accessing Array Elements Using integer expressions the array elements can be accessed sequentially by the counter in a for loop. Ex : for (i=0;i<5;i++) total=total + a[i]; Here i has been used both as a counter and as an subscript. For adding the array elements the same procedure can also be used. Ex : for(i=0;<5;i++) a[i]=0;

Other allowed operations If int a[10];.Then the allowed operations on this array are To increament the i’th element. a[i]++; To add n to the i’th element. A[i]+=n; To copy the contents of i’th element to the k’th element. A[k]=a[i]; To copy the contents from array a[] to array b[]. for(i=0;i<10;i++) b[i]=a[i]; To exchange the values in a[i] and a[k] . Using third variable temp. int temp; temp=a[i]; a[i]=a[k]; a[k]=temp;

Storing and Printing Storing values in an array : Reading the input into an array can be done like int a[10],I; For(i=0;i<10;i++) Scanf(“%d”,&a[i]); Printing an array: Printf(“%d”,a[i]);

Internal representation of Arrays in c An array is implemented as a single block of memory. The element 0 occupies the first slot in the block. Thus the subscripts are closely related to actual memory addresses and to the notation of pointers. For an array of length ‘len’ of int type the compiler will allocate len*sizeof(int) bytes. Thus for an array int a[10] it allocates 20 bytes(10*sizeof(int)) suppose say starting from 64789. Then the compiler accesses the value stored in a[5] at memory location starting with byte 64789+(5*2) .

References to elements outside the array element If an element outside the boundary is accessed the following events may happen. Produce some sort of error. Program may crash. Produce incorrect output. Even produce correct output. It all depends on what information is being stored in the memory locations surrounding the block of memory reserved for the array.

Dynamic Memory allocation A program is allocated a small area of memory called the stack. This memory is used by the variables in the program. Disadvantages A variable looses it’s scope when the program goes to the next closing curly bracket it is destroyed and the allocated memory is freed up. So it is needed to be careful when getting functions to fill in arrays. The size of memory allocated from the stack must be fixed at compile time. It is not possible to declare a variable for the size as the compiler does not know how big the array will be. Ex : int size; scanf(“%d”,&size); int a[size]; This code will not work.