c-11

Post on 19-Jan-2015

143 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Lecture 11Lecture 11Version 1.0Version 1.0

Arrays: One DimensionalArrays: One Dimensional

2Rushdi Shams, Dept of CSE, KUET, Bangladesh

ArraysArrays

one of the C’s most essential data one of the C’s most essential data structures structures

Arrays are data structures consisting Arrays are data structures consisting of related data items of the same of related data items of the same type type

it is a group of memory locations it is a group of memory locations related by the fact that they all have related by the fact that they all have the same name and same type the same name and same type

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

Why ArraysWhy Arrays

No doubt, this program will print the value of No doubt, this program will print the value of x x as as 10 10

Because when a value 10 is assigned to Because when a value 10 is assigned to xx, the , the earlier value of earlier value of xx, i.e. 5, is lost , i.e. 5, is lost

ordinary variables (the ones which we have used so ordinary variables (the ones which we have used so far) are capable of holding only one value at a time far) are capable of holding only one value at a time

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

Why ArraysWhy Arrays However, there are situations in which However, there are situations in which

we would want to store more than one we would want to store more than one value at a time in a single variable value at a time in a single variable

suppose we wish to arrange the suppose we wish to arrange the percentage marks obtained by 100 percentage marks obtained by 100 students in ascending order. In such a students in ascending order. In such a case we have two options to store these case we have two options to store these marks in memory: marks in memory:

1.1. Construct 100 variables, each variable Construct 100 variables, each variable containing one student’s marks. containing one student’s marks.

2.2. Construct one variable capable of storing Construct one variable capable of storing or holding all the hundred values. or holding all the hundred values.

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

Why ArraysWhy Arrays

the second alternative is better the second alternative is better it would be much easier to handle it would be much easier to handle

one variable than handling 100 one variable than handling 100 different variables different variables

Moreover, there are certain logics Moreover, there are certain logics that cannot be dealt with, without that cannot be dealt with, without the use of an array the use of an array

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

a one dimensional array is a list of a one dimensional array is a list of variables that are all of the same variables that are all of the same type and accessed through a type and accessed through a common name common name

An individual element in an array is An individual element in an array is calledcalled array element array element

Array is helpful to handle a group of Array is helpful to handle a group of similar types of data similar types of data

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array To declare a one dimensional array, we To declare a one dimensional array, we

use-use-

data_type array_name [size];data_type array_name [size];

data_type is a valid C data type, data_type is a valid C data type, array_name is the name of that array array_name is the name of that array

and and size specifies the number of elements in size specifies the number of elements in

the array the array

8Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

int my_array[20];int my_array[20];

Declares an array name my_array of Declares an array name my_array of type integer that contains 20 type integer that contains 20 elements elements

9Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

An array element is accessed by An array element is accessed by indexing the array using the number of indexing the array using the number of element element

all arrays begin at zero all arrays begin at zero if you want to access the first element if you want to access the first element

in an array, use zero for the index in an array, use zero for the index To index an array, specify the index of To index an array, specify the index of

the element you want inside square the element you want inside square brackets brackets

10Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

The second element of my_array will The second element of my_array will be- be-

my_array [1]my_array [1]

11Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

C stores one C stores one dimensional array dimensional array in one contiguous in one contiguous memory location memory location with first element at with first element at the lower address the lower address

an array named an array named aa of of 10 elements can 10 elements can occupy the memory occupy the memory as follows- as follows-

12Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

a program that declares an array of 10 a program that declares an array of 10 elements and initializes every element of elements and initializes every element of that array with 0 that array with 0

13Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

an array can also be initialized by an array can also be initialized by following the declaration with an equal following the declaration with an equal sign and a comma separated list of values sign and a comma separated list of values within a pair of curly brace within a pair of curly brace

14Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

If there are fewer values than elements in If there are fewer values than elements in array, the remaining elements are array, the remaining elements are initialized automatically with zero initialized automatically with zero

15Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

If you put more values than the array can If you put more values than the array can hold, there will be a syntax errorhold, there will be a syntax error

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

One Dimensional ArrayOne Dimensional Array

If array size is omitted during declaration, If array size is omitted during declaration, the number of values during array the number of values during array initialization will be the number of initialization will be the number of elements the array can hold elements the array can hold

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

Simple program using Simple program using ArrayArray

18Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character ArraysCharacter Arrays

Character arrays have several Character arrays have several unique features unique features

A character array can be initialized A character array can be initialized using a using a string literalstring literal

char string1[ ] = “first”;char string1[ ] = “first”;

19Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character ArraysCharacter Arrays

““first” string literal contains five first” string literal contains five characters plus a special string characters plus a special string termination character called null termination character called null character (\0) character (\0)

string1 array actually has 6 string1 array actually has 6 elements-f, i, r, s, t and \0 elements-f, i, r, s, t and \0

20Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character ArraysCharacter Arrays

Character arrays can be initialized as Character arrays can be initialized as follows as well-follows as well-

char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘\char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘\0’};0’};

We can access individual characters We can access individual characters in a string directly using array in a string directly using array subscript notation. For example, subscript notation. For example, string1 [3] is the character ‘s’ string1 [3] is the character ‘s’

21Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character ArraysCharacter Arrays

We can also input a string directly We can also input a string directly from the keyboard using scanf () from the keyboard using scanf () function and %s specifier.function and %s specifier.

char string1 [20];char string1 [20];

scanf (“%s”, string1); scanf (“%s”, string1);

22Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character ArraysCharacter Arrays

the name of the array is passed to the name of the array is passed to scanf () without the preceding & used scanf () without the preceding & used with other variables with other variables

The & is normally used to provide The & is normally used to provide scanf () with a variable’s location in a scanf () with a variable’s location in a memory so a value can be stored there memory so a value can be stored there

Array name is the address of the start Array name is the address of the start of the array, therefore, & is not of the array, therefore, & is not necessary necessary

23Rushdi Shams, Dept of CSE, KUET, Bangladesh

Character ArraysCharacter Arrays

scanf () reads characters from the scanf () reads characters from the keyboard until the first whitespace keyboard until the first whitespace character is encounteredcharacter is encountered

scanf () takes upto the first scanf () takes upto the first whitespace whitespace

24Rushdi Shams, Dept of CSE, KUET, Bangladesh

top related