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.

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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 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:
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.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
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.
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.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous memory allocation.
© 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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
Array in C# Array in C# RIHS Arshad Khan
INC 161 , CPE 100 Computer Programming
User-Written Functions
Chapter 7 Pointers and C-Strings
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computer Programming BCT 1113
CHP-2 ARRAYS.
© 2016 Pearson Education, Ltd. All rights reserved.
C Scope Rules and Arrays
EKT120 : Computer Programming
Arrays Declarations CSCI N305
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
5. Arrays, Pointers and Strings
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Pointers, Dynamic Data, and Reference Types
EKT150 : Computer Programming
Declaration, assignment & accessing
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
EKT120: Computer Programming
Chapter 2 Array and String Visit to more Learning Resources.
UNIT - 3 Noornilo Nafees.
7 Arrays.
To refer to an element, specify
Arrays Arrays A few types Structures of related data items
Arrays.
CS31 Discussion 1H Fall18: week 6
Pointers, Dynamic Data, and Reference Types
Dr. Khizar Hayat Associate Prof. of Computer Science
Visit for more Learning Resources
Presentation transcript:

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 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]); 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 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 H e l o ‘\0’

#include <stdio.h> 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 < stdio.h > 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 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 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: 1 2 3 4 5 6 0 0 0 0

9

Output 6 5 4 3 2

10

}

string

Output ccccccccccccccccccc

Output 0000000000

Output No output

24