C Chapter 06
C Chapter 06
1
• It is derived data type.
• An array is a very popular and useful data
structure used to store data elements in
successive memory locations.
• It is known as “Composite data structure” as
more than one element is stored in a sequence.
• It permits only homogenous data.
• An array can be declared of any standard or
custom data type.
• The array of character(strings) type works
somewhat differently from an array of
integers, floating numbers.
• Array elements can be accessed by its position
in the array called as index.
• Values in an array are identified using array
name with subscripts.
• It is also known as subscripted variable.
• The array can be done as under
int a[5] = {1, 2, 3, 4, 5};
Type : Type refers to data type. It decides which type of element is stored in the array. It also instructs the
compiler to reserve memory according to data type.
Base : The address of the first element(0th) is a base address. The array name itself stores address of the
first element.
Index : The array name is used to refer to the array element. For example, num[x], num is array name and x
is index. The value of x begins from 0 to onwards depending on the size of the array. The index value is
always an integer value.
Range : Index of an array i.e. value of x varies from lower bound to upper bound while writing or reading
elements from an array. For example in num[100] the range of index is 0 to 99.
Word : It indicates the space required for an element. In each memory location, computer can store a data
piece. The space occupation varies from machine to machine. If the size of element is more than word (one
byte) then it occupies two successive memory locations. The variable of data type int, float, long need more
than one byte in memory.
1. The Declaration int a[5] is the creation of five variables of integer types
in memory. Instead of declaring five variables for five values, the
programmer.
2. All the elements of an array share the same name, and they are
distinguished from one another with the help of the element number.
3. The element number in an array plays a major role for calling each
element.
4. Any particular element of an array can be modified separately without
disturbing the other elements.
int a[5] = {1, 2, 3, 4, 5}
5. Any element of an array a[] can be assigned/equated to another
ordinary variable or array variable of its type.
6. An array elements are stored in contiguous memory
0 1 2 3 4
10 20 30 40 50
1000 1002 1004 1006 1008
7. Once the array is declared, its lowest boundary cannot be changed but upper
boundary can be expanded.
8. We know that an array name itself is a pointer. Though it is a pointer, it does not
need ‘*’ operator. The brackets “[]” automatically denote that the variable is a
pointer.
9. All the elements of an array share the same near, and they are distinguished
from one another with the help of the element number.
10. The amount of memory required for an array depends upon the data type and
the number of elements. The total size in bytes for a single dimensional array is
compared as shown below.
Total bytes = sizeof(data type) x size of array
8
elements
Memory address 100 102 104 106 108 110 112 114 116
• We can initialize the elements of arrays in the same way as
the ordinary variables when they are declared.
type array_name[size] = {list of values};
• For example,
int number[3] = {5,10,15};
float total[5] ={0.0, 15.75, -10.9};
• The size may be omitted. In such cases, the compiler allocates
enough space for all initialized elements.
int counter[] = {1,1,1,1};
• The character array may be initialized in the similar manner
char name[] = {‘v’,’i’,’k’,’a’,’s’,’\0’}; or
char name[] = “vikas”;
• An array can be explicitly initialized at run time.
void main()
{
MyArray[0]= 0
int MyArray[5],i;
MyArray[1]=10
for(i=0;i<5;i++) MyArray[2]=20
{ MyArray[3]=30
MyArray[i] = i * 10; MyArray[4]=40
}
}
• An array can be explicitly declared and initialized at the first time.
int main(int argc, char *argv[]) {
char name[]="TARKAN";
6 T
int i=0;
A
while(i<sizeof(name)) R
{ K
printf("%c\n",name[i]); A
i++; N
}
return 0;
}
Two - Dimensional Arrays
What is a Two-dimensional array?
Array type Array name
Array dimension = 2
51, 52, 53 Row 1
B= Int b[2][3] = {{51, 52, 53},{54, 55, 56}};
54, 55, 56 Row 2
First Row 8 16 9 52
Second Row 3 15 27 6
Third Row 14 25 2 10
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
int c[ ][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}};
Implicitly declares the number of rows to be 4.
Input of Two-Dimensional Arrays
FindingSmall(arr);
return 0;
}
30
Question-1
return 0;
Question-2
• Write a C program to give the length of the text from the keyboard.
• Declare an array called as txt that is set the size as 50.
• Use a function called as findlen that calculates the length of
the given text.
• You must use do-while loop in the function findlen.
• The function has only one parameter and only one variable
must be used in the function.
• You must use only one variable in the function main()
• In the main program, the output is like below:
Answer for Question-2
int findlen(char []);
int main(int argc, char *argv[]) { Declaration an array
char txt[50];
scanf("%s",txt);
Input the values of the array
}
Question-3
Write a C program to sort elements of the array in
descending order.
Note That:
Declare an array called as “arr” with 100.
Read the elements of the array from the user using the Do-While
loop.
Declare and initialize a sentinel value called as “senval” with “-99”
with global.
You must NOT declare the variables more than four variables
except for “senval”.
If the user wish to exit the loop, the user has to input the sentinel
value you defined before.
You must only use For loop for both of sorting the elements of the
array and display the sorted array in descending order .
The output is like below for example:
Answer for Question-3