Arrays C provides the option to the user to combine similar data types into a single entity 2000 2002 2004 2006 2008 It followed contiguous memory allocation.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lecture 20 Arrays and Strings
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.
Informática II Prof. Dr. Gustavo Patiño MJ
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
C ARRAYS -a collection of same type data, 1D, 2D- © 1/25.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
A First Book of ANSI C Fourth Edition
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
UNIT - 3 ARRAYS AND STRINGS. Array An Array is a collection of similar data items, that are stored under a common name. Types –One-Dimensional array –Two-Dimensional.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
26/06/ :14:35 CSC Alliance — 1 Kimera Richard Phone: INSTITUTE OF COMPUTER SCIENCE DEPARTMENT.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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.
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
© 2016 Pearson Education, Ltd. All rights reserved.
C Scope Rules and Arrays
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Module 2 Arrays and strings – example programs.
Arrays in C.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
EKT150 : Computer Programming
Declaration, assignment & accessing
Arrays Outline Introduction Arrays Declaring Arrays
UNIT - 3 Noornilo Nafees.
7 Arrays.
To refer to an element, specify
Arrays Arrays A few types Structures of related data items
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation a[0]a[1]a[2]a[3]a[4]

Main () { float avg,sum=0; Int I,a[5]; Printf(“Enter five numbers”); for(i=0;i<5;i++) Scanf(“%d”,&a[i]); for(i=0;i<5;i++) Sum=sum+a[i]; avg=sum/5; Printf(“The sum of given number is %f”,sum); Printf(“Average of given number is %f”,avg); }

Like normal variable array can also initialized during declaration example Int[5]={1,2,3,4,5}; Int[]={1,2,3,4,5}; Float[]={1.0,2.0,3.0,4.0,5.0};

Int a[5]={1,2,3,4,5}; Int a[]={1,2,3,4,5}; \\dimensional is optional\\dimensional Float b[]={1.0,2.0,3.0,4.0,5.0};

Array declaration and Initilization Datatype var_name[size]; The above term is syntax for declaring array Eg: int b[6]; Float c[6]; Int d[5][5];

initialize array in C either one by one or using a single statement double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; If you omit the size of the array, an array just big enough to hold the initialization is created. double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0}; To assign a single element in array A[4]=45;

Limitation of array a)Static data : Array is static data structure Memory is allocated during compile time Once memory is allocated during compile time, it cannot be changed during Run time.

b)Can hold the data from the same data type It cannot hold data from a different data type in a common name Int a[5]={1,2,3,4.5,6};

c)Inserting element is more difficult in array d)Deleting is not a easy task because the data is stored in contiguous memory allocation e)Bounds checking:  It doesnt show error for out of bounds value, instead it will shows the garbage value f)Shortage of memory: if we don’t know the memory size exactly.  Memory is allocated during the compile time itself

G)Wastage of memory  if array size is too large

Multi dimensional array Its called matrix Array having more than one subscript Int a[][];  One subscript value denotes “Row”  another subscript value denotes “column”

A[0][0]A[0][1]A[0][2] A[1][0]A[1][1]A[1][2] A[2][0]A[2][1]A[2][3]

Strings It is a one-dimensional array of characters which is terminated by a null character '\0'. char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; the memory presentation string Hello‘\0’

#include int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; printf("Greeting message: %s\n", greeting ); return 0; }

Output: Hello

The following example shows the use of string #include main() { char month[15]; printf (”Enter the string”); gets (month); printf (”The string entered is %s”, month); }

Reading Strings from the terminal: char address[15]; scanf(%s,address);

String operations 1. Length (number of characters in the string). 2. Concatentation (adding two are more strings) 3. Comparing two strings. 4. Substring (Extract substring from a given string) 5. Copy(copies one string over another)

strlen(string); Strcmp(string1,string2); Strcmpi(string1,string2); \\not case sens\\not strcpy(string1,string2); strlwr(string); strrev(string); strcat(string1,string2); strncmp(string1,string2,length); strnicmp(string1,stringn,length); strset(string,symbol);\\replace

String constants It is written in pair of double quotas String is declared as character array String is not a data type Single character string doesn’t have the equivalent int value

String with single char “a” String with multiple char “abg” String with numbers “12345” String with blanks “india is my country”

For example: ‘a’ is not equal to “a”

Guess the output

2

No Output

3

4

5

6

7

8

Output:

9

Output

10

}

string

0

Output ccccccccccccccccccc

Output

Output No output

24