0% found this document useful (0 votes)
6 views16 pages

Array

Uploaded by

0112cs221035
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

Array

Uploaded by

0112cs221035
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structured Data type: ARRAY

Objectives:
• What is Array?
• What is subscript?
• Types of Array
• 1-D Array
• Memory Representation of 1-D Array
• Program to Linear Search an element in an Array.
• String as an Array
• 2-D Array
• Processing 2-D Array
contd…
• Memory Representation of 2-D Array
• Program to check equality of two matrices.
• Array of Strings
What is Array?
• In C, all arrays consist of contiguous memory locations.
• The lowest address corresponds to the first element and the highest
address to the last element.
• Arrays are the way to group a number of items into a larger unit.
• Array can have data items of simple types like int or float, or even of
user-defined types like structures.
What is subscript?
• The element number in [] are called subscripts or indices.
• The subscript, or index of an element designates its position in the
array’s ordering.
• The subscript of the lowest position always starts from 0 and the
highest position is always size of the array – 1.
Types of Array
• There are 2 types of Array:
• one-dimensional arrays, comprised of finite homogeneous elements.
• two-dimensional arrays, comprised of elements, each of which is
itself an array.
1-D Array
• A simplest form of an array.
• syntax:
• type array-name[size];
• example:
• int marks[50];
• In order to initialize particular element in an array use subscript, i.e.
• marks[0]=89; //the first element of an array marks is 89
contd…
• In order to take input user-input in bulk use for loop, i.e.
• for(i=0; i<50; ++i)
• scanf(“%d”, &marks[i]);
• //where i in the loop starts from the subscript 0 and ends at 50-1
• Similarly, you can print the value using for loop.
Memory Representation of 1-D Array
• example:
• char grade[8];
• The memory of the above would look like:
g[0] g[1] g[2] g[3]

2001 2002 2003 2004 grade

addresses

Base Address
contd…
• int age[5];
age[0] age[1] age[2] age[3] age[4]

5000 5002 5004 5006 5008


• Because of the size of the int beign 2 bytes the address will also skip.
• In order to calculate the total size of an array:
• total bytes=sizeof(type)*size of array
• bn=sizeof(int)*5; //bn=10 bytes
Program to Linear Search an element in an
Array.
• You’ll require:
• an array arr[50] to store the data of integer type of size 50.
• item that need to be searched in the given array.
• N to give the actual number of elements that need to be stored
in an array.
• an i for the loop
• and an index to get the position of the item that has been found.
contd…
9. void main()
1. int Lsearch(int a[], int size, int
item) 10. { int arr[50], N, item, index,i;
2. { int i; 11. printf(“enter the desired
size of elements:\n”);
3. for(i=0; i<size; i++)
12. scanf(“%d”,&N);
4. { if(a[i]==item)
13. printf(“Enter Array
5. return i; elements:\n”);
6. } 14. for(i=0;i<N;++i)
7. return -1; 15. { scanf(“%d”,&arr[i]);
8. } 16. }
contd…
17. printf(“Enter element to be
searched for:\n”);
18. scanf(“%d”,&item);
19. index=Lsearch(arr, N, item);
20. if(index==-1)
21. printf(“could not found
the item”);
22. else
23. printf(“Found in
position: %d ”, index+1);
24. }
String as an Array
• In C, string is defined as a character array that terminated by null(‘\0’)
character.
• For this reason, the character arrays are declared one character
longer than the largest string they can hold.
• For instance, to declare an array strng that holds a 10-character
string, you would write:
• char strng[11];

note*- “Stream of characters is known as String”


contd…
• In C, in order to take use-input either you can use for loop.
• But, more better option would be to use “%s”.
• char name[30];
• scanf(“%s”,&name);
Write a C program to reverse the string.

You might also like