Upcoming SlideShare
×

# C-11

194
-1

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
194
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
13
0
Likes
0
Embeds 0
No embeds

No notes for slide

### C-11

1. 1. Lecture 11Lecture 11 Version 1.0Version 1.0 Arrays: One DimensionalArrays: One Dimensional
2. 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh ArraysArrays  one of the C’s most essential data structuresone of the C’s most essential data structures  Arrays are data structures consisting of relatedArrays are data structures consisting of related data items of the same typedata items of the same type  it is a group of memory locations related by theit is a group of memory locations related by the fact that they all have the same name and samefact that they all have the same name and same typetype
3. 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh Why ArraysWhy Arrays  No doubt, this program will print the value ofNo doubt, this program will print the value of xx as 10as 10  Because when a value 10 is assigned toBecause when a value 10 is assigned to xx, the earlier, the earlier value ofvalue of xx, i.e. 5, is lost, i.e. 5, is lost  ordinary variables (the ones which we have used so far)ordinary variables (the ones which we have used so far) are capable of holding only one value at a timeare capable of holding only one value at a time
4. 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh Why ArraysWhy Arrays  However, there are situations in which we would wantHowever, there are situations in which we would want to store more than one value at a time in a singleto store more than one value at a time in a single variablevariable  suppose we wish to arrange the percentage markssuppose we wish to arrange the percentage marks obtained by 100 students in ascending order. In such aobtained by 100 students in ascending order. In such a case we have two options to store these marks incase we have two options to store these marks in memory:memory: 1.1. Construct 100 variables, each variable containing oneConstruct 100 variables, each variable containing one student’s marks.student’s marks. 2.2. Construct one variable capable of storing or holdingConstruct one variable capable of storing or holding all the hundred values.all the hundred values.
5. 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh Why ArraysWhy Arrays  the second alternative is betterthe second alternative is better  it would be much easier to handle one variableit would be much easier to handle one variable than handling 100 different variablesthan handling 100 different variables  Moreover, there are certain logics that cannotMoreover, there are certain logics that cannot be dealt with, without the use of an arraybe dealt with, without the use of an array
6. 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  a one dimensional array is a list of variables thata one dimensional array is a list of variables that are all of the same type and accessed through aare all of the same type and accessed through a common namecommon name  An individual element in an array is calledAn individual element in an array is called arrayarray elementelement  Array is helpful to handle a group of similarArray is helpful to handle a group of similar types of datatypes of data
7. 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  To declare a one dimensional array, we use-To declare a one dimensional array, we 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 andarray_name is the name of that array and  size specifies the number of elements in thesize specifies the number of elements in the arrayarray
8. 8. 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 type integerDeclares an array name my_array of type integer that contains 20 elementsthat contains 20 elements
9. 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  An array element is accessed by indexing theAn array element is accessed by indexing the array using the number of elementarray using the number of element  all arrays begin at zeroall arrays begin at zero  if you want to access the first element in anif you want to access the first element in an array, use zero for the indexarray, use zero for the index  To index an array, specify the index of theTo index an array, specify the index of the element you want inside square bracketselement you want inside square brackets
10. 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  The second element of my_array will be-The second element of my_array will be- my_array [1]my_array [1]
11. 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  C stores one dimensionalC stores one dimensional array in one contiguousarray in one contiguous memory location withmemory location with first element at the lowerfirst element at the lower addressaddress  an array namedan array named aa of 10of 10 elements can occupy theelements can occupy the memory as follows-memory as follows-
12. 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  a program that declares an array of 10 elements anda program that declares an array of 10 elements and initializes every element of that array with 0initializes every element of that array with 0
13. 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  an array can also be initialized by following thean array can also be initialized by following the declaration with an equal sign and a comma separateddeclaration with an equal sign and a comma separated list of values within a pair of curly bracelist of values within a pair of curly brace
14. 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  If there are fewer values than elements in array, theIf there are fewer values than elements in array, the remaining elements are initialized automatically withremaining elements are initialized automatically with zerozero
15. 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  If you put more values than the array can hold, thereIf you put more values than the array can hold, there will be a syntax errorwill be a syntax error
16. 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  If array size is omitted during declaration, the numberIf array size is omitted during declaration, the number of values during array initialization will be the numberof values during array initialization will be the number of elements the array can holdof elements the array can hold
17. 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh Simple program using ArraySimple program using Array
18. 18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  Character arrays have several unique featuresCharacter arrays have several unique features  A character array can be initialized using aA character array can be initialized using a stringstring literalliteral char string1[ ] = “first”;char string1[ ] = “first”;
19. 19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  ““first” string literal contains five characters plusfirst” string literal contains five characters plus a special string termination character called nulla special string termination character called null character (0)character (0)  string1 array actually has 6 elements-f, i, r, s, tstring1 array actually has 6 elements-f, i, r, s, t and 0and 0
20. 20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  Character arrays can be initialized as follows asCharacter arrays can be initialized as follows as well-well- char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘0’};char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘0’};  We can access individual characters in a stringWe can access individual characters in a string directly using array subscript notation. Fordirectly using array subscript notation. For example, string1 [3] is the character ‘s’example, string1 [3] is the character ‘s’
21. 21. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  We can also input a string directly from theWe can also input a string directly from the keyboard using scanf () function and %skeyboard using scanf () function and %s specifier.specifier. char string1 [20];char string1 [20]; scanf (“%s”, string1);scanf (“%s”, string1);
22. 22. 22Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  the name of the array is passed to scanf ()the name of the array is passed to scanf () without the preceding & used with otherwithout the preceding & used with other variablesvariables  The & is normally used to provide scanf () withThe & is normally used to provide scanf () with a variable’s location in a memory so a value cana variable’s location in a memory so a value can be stored therebe stored there  Array name is the address of the start of theArray name is the address of the start of the array, therefore, & is not necessaryarray, therefore, & is not necessary
23. 23. 23Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  scanf () reads characters from the keyboard untilscanf () reads characters from the keyboard until the first whitespace character is encounteredthe first whitespace character is encountered  scanf () takes upto the first whitespacescanf () takes upto the first whitespace
24. 24. 24Rushdi Shams, Dept of CSE, KUET, Bangladesh
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.