Array C Language
Array C Language
ARRAY
• Array is a collection of elements of same data type.
• The elements are stored sequentially one after the other in memory.
• Any element can be accessed by using
→ name of the array
→ position of element in the array
• Arrays are of 2 types:
1) Single dimensional array
2) Multi dimensional array
SINGLE DIMENSIONAL ARRAY
• A single dimensional array is a linear list consisting of related elements of same type.
• In memory, all the elements are stored in continuous memory-location one after the other.
• After an array is declared, its elements must be initialized. Otherwise they will contain the garbage
values.
• Array initialization can be done in 2 ways.
1) Initialization at compile time
2) Initialization at run time
Initialization of 1D-array at compile time
The general form of initializing an array at the compile time is as follows,
Type array_name[size]={list of values};
• Example, int number[3]={1,2,3};
• The values in the list are separated by commas.
• Character arrays may be declared in the same manner
char name[]={‘J’,’O’,’H’,”N’,’\0’};
• String arrays may be declared as char
name[]=”JOHN”;
• A two dimensional array is used when elements are arranged in a tabular fashion.
• Here, to identify a particular element, we have to specify 2 indices:
• → First index identifies the row number of the element and
• → Second index identifies the column number of the element
Declaration of Two Dimensional Arrays
• The syntax is shown below:
data_typearray_name[row_size][column_size];
• For ex:
int matrix[2][3];
• The above code can be pictorially represented as shown below:
• Can be done by separating the values of each row and column in the braces.
int table[2][3]= {{1, 23, 11}, {44, 5, 6}};
• Two dimensional array can also be initialized in the form of matrix.
int table[2][3]= { {1, 23, 11},{44, 5, 6}};
• The above code can be pictorially represented as shownbelow:
#include<stdio.h>
void main()
{
int matrix[2][3], i, j;
printf("enter elements of 2*3 matrix: \n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("elements of the matrix are: \n”); for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t", matrix[i][j]);
}
printf(“\n”);
}
}
STRINGS
• Array of character are called strings.
• A string is terminated by null character/0.
• For ex:
"c string tutorial"
• The above string can be pictorially represented as shown below:
STRING VARIABLE
• There is no separate data type for strings in C.
• They are treated as arrays of type “char‟.
• So, a variable which is used to store an array of characters is called a string variable.
Declaration of String
• Strings are declared in C in similar manner as arrays. Only difference is that, strings are of “char” type.
• The general form of declaring string variable is as follows,
char string_name[size];
• For ex:
chars[5]; //string variable name can hold maximum of 5 characters including NULL character
• The above code can be pictorially represented as shown below:
Initialization of String
• C permits initialization of character array in two ways
• For ex:
char s[5]={'r', 'a', 'm', 'a',’\0’};
char s[5]="rama";
• The above code can be pictorially represented as shown below:
•This function joins 2 strings. It takes two arguments, i.e., 2 strings and resultant string is stored in
the first string specified in the argument.
• The syntax is shown below:
strcat(first_string,second_string);
strcmp()
• This function compares 2 string and returns value 0, if the 2 strings are equal. It takes 2 arguments,
i.e., name of two string to compare.
• The syntax is shown below:
temp_varaible=strcmp(string1,string2);
FUNCTIONS:
• A function is a block of code to perform a specific task.
Function Call
• Control of the program cannot be transferred to user-defined function unless it is called/invoked.
• The syntax is shown below:
function_name(argument(1),. ..argument(n));
Example:
#include<stdio.h>
void main()
{
display(); // function call
}
#include <stdio.h>
void add();
void main()
{
add();
}
void add()
{
int a, b, sum;
printf("Enters two number to add : ");
scanf("%d %d", &a, &b);
sum=a+b;
printf("\n sum=%d", sum);
return;
}
#include <stdio.h>
void add(int a, int b);
void main()
{
int a, b, sum;
printf("Enters two number to add \n");
scanf("%d %d", &a, &b);
add(a, b);
}
void add(int a, int b)
{
sum= a+ b;
printf("\n sum=%d", sum);
return;
}
RECURSION
• A function that calls itself is known as recursive function.
#include <stdio.h>
int fib(int n) Output:
{ Enter the series size: 10
The Fibonacci series are
if(n==0||n==1) 0 1 1 2 3 5 8 13 21 34
return n;
else
return (fib(n-1)+fib(n-2)); /*self call to function
}
void main()
{
int series_size,i;
printf("Enter the series size: ");
scanf("%d",&series_size);
printf(“the Fibonacci series are\n”);
for(i=0;i<series_size;i++)
printf(“%d\t”,fib(i));
return 0;
}
Example: To find the factorial of a number using recursion
#include <stdio.h>
float fact(int n) Output:
{ Enter positive integer 3
if(n==0) factorial of 3=6.000000
return 1;
else
return n*fact(n-1); /*self call to function
}
void main()
{
int num;
float res;
printf("Enter a positive integer: ");
scanf("%d",&num);
res=fact(num);
printf("factorial of %d=%f", num,res);
}