You are on page 1of 13

C Array

C Array is a collection of variables belongings to the same data type. You can store
group of data of same data type in an array.

Array might be belonging to any of the data types

Array size must be a constant value.

Always, Contiguous (adjacent) memory locations are used to store array


elements in memory.
It is a best practice to initialize an array to zero or null while declaring, if we

dont assign any values to array.


EXAMPLE FOR C ARRAYS:
int a[10];

// integer array

char b[10]; // character array i.e. string


TYPES OF C ARRAYS:

There are 2 types of C arrays. They are,


1.

One dimensional array

2.

Multi dimensional array


1.

Two dimensional array

2.
Three dimensional array, four dimensional array etc
1. ONE DIMENSIONAL ARRAY IN C:

Syntax : data-type arr_name[array_size];


Array declaration

Array initialization

Accessing array

data_type arr_name
Syntax: data_type

[arr_size]=(value1,

arr_name [arr_size];

value2, value3,.);

arr_name[index];
age[0]; /* 0 is_accessed */

int age [5];

int age[5]={0, 1, 2, 3,

age[1]; /* 1 is accessed */

4};

age[2]; /*2 is accessed */

char str[10]={H,a,i};
(or)char str[0] = H;
char str[1] = a;
char str[10];

char str[2] = i;

str[0]; /* H is accessed */
str[1]; /* a is accessed */
str[2]; /* i is accessed*/

EXAMPLE PROGRAM FOR ONE DIMENSIONAL ARRAY IN C:


#include<stdio.h>
int main()
{
int i;
int arr[5] = {10,20,30,40,50};
// declaring and Initializing array in C
//To initialize all array elements to 0, use int arr[5]={0};
/* Above array can be initialized as below also
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50; */
for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}
}

OUTPUT:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50

2. TWO DIMENSIONAL ARRAY IN C:

Two dimensional array is nothing but array of array.

syntax : data_type array_name[num_of_rows][num_of_column]


S.n

Accessing

Array declaration

Array initialization

array

data_type

Syntax: data_type arr_name

arr_name[2][2] =

[num_of_rows]

{{0,0},{0,1},{1,0},

arr_name[index

[num_of_column];

{1,1}};

];
arr [0] [0] = 1;
arr [0] ]1] = 2;

Example:int arr[2][2];

int arr[2][2] = {1,2,

arr [1][0] = 3;

3, 4};

arr [1] [1] = 4;

EXAMPLE PROGRAM FOR TWO DIMENSIONAL ARRAY IN C:


#include<stdio.h>int main(){
int i,j;
// declaring and Initializing array
int arr[2][2] = {10,20,30,40};
/* Above array can be initialized as below also
arr[0][0] = 10; // Initializing array
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40; */
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
// Accessing variables
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}

Output:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40

C PROGRAM TO SORT THE ARRAY IN AN ASCENDING ORDER


/*
* C program to accept N numbers and arrange them in an ascending order
*/
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
}

SORT GIVEN STRING IN ASCENDING ORDER IN C PROGRAMMING


#include<stdio.h>
#include<conio.h>
int main()
{
char str[100],temp;
int i,j;
clrscr();
printf("Enter the string :");
gets(str);
printf("%s in ascending order is -> ",str);
for(i=0;str[i];i++)
{
for(j=i+1;str[j];j++)
{
if(str[j]<str[i])
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
}
}
}
printf("%s\n",str);
getch();
return 0;
}

Output:
Enter the string : syntax
syntax in ascending order is -> anstxy

C PROGRAM TO SEARCH AN ELEMENT IN ARRAY


#include<stdio.h>
int main() {
int a[30], ele, num, i;
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter the values :");
for (i = 0; i < num; i++) {
scanf("%d", &a[i]);
}
//Read the element to be searched
printf("\nEnter the elements to be searched :");
scanf("%d", &ele);
//Search starts from the zeroth location
i = 0;
while (i < num && ele != a[i]) {
i++;
}
//If i < num then Match found
if (i < num) {
printf("Number found at the location = %d", i + 1);
} else {
printf("Number not found");
}

return (0);

Output
Enter no of elements : 5
11 22 33 44 55
Enter the elements to be searched : 44
Number found at the location = 4

CHECK GIVEN STRING IS PALINDROME NUMBER OR NOT ?


#include <stdio.h>
#include <conio.h>
void main() {
char *a;
int i,len,flag=0;
clrscr();
printf("\nENTER A STRING: ");
gets(a);
len=strlen(a);
for (i=0;i<len;i++) {
if(a[i]==a[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("\nTHE STRING IS PALINDROM"); else
printf("\nTHE STRING IS NOT PALINDROM");
getch();
}

C PROGRAM TO DISPLAY FIBONACCI SEQUENCE

The Fibonacci sequence is a series where the next term is the sum of
pervious two terms. The first two terms of the Fibonacci sequence is 0
followed by 1.
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;
printf("Enter the number of terms: ");
scanf("%d",&n);
// displays the first two terms which is always 0 and 1
printf("Fibonacci Series: %d, %d, ", t1, t2);
// i = 3 because the first two terms are already displayed
for (i=3; i <= n; ++i)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ",nextTerm);
}
return 0;
}

C String
1. C Strings are nothing but array of characters ended with null character (\0).
2. This null character indicates the end of the string.
3. Strings are always enclosed by double quotes. Whereas, character is enclosed by
single quotes in C.
EXAMPLE FOR C STRING:
char string[20] = { f , r , e , s , h , 2 , r , e , f , r , e , s , h , \0}; (or)
char string[20] = fresh2refresh; (or)
char string []

= fresh2refresh;

Difference between above declarations are, when we declare char as string[20], 20


bytes of memory space is allocated for holding the string value.
When we declare char as string[], memory space will be allocated as per the
requirement during execution of the program.
EXAMPLE PROGRAM FOR C STRING:
#include <stdio.h>
int main ()
{
char string[20] = "fresh2refresh.com";
printf("The string is : %s \n", string );
return 0;
}
OUTPUT:

The string is : fresh2refresh.com

C STRING FUNCTIONS:
String.h header file supports all the string functions in C language. some the string
functions are given below.

S.n

String

functions

Description

Concatenates str2 at the end of


1

strcat ( )

str1.

strcpy ( )

Copies str2 into str1

strlen ( )

gives the length of str1.

Returns 0 if str1 is same as


str2. Returns

<0

if

strl

strcmp ( )

str2. Returns >0 if str1 > str2.

13

strlwr ( )

converts string to lowercase

14

strupr ( )

converts string to uppercase

15

strrev ( )

reverses the given string

<

STRCAT( )
#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = " fresh2refresh" ;
char target[ ]= " C tutorial" ;
printf ( "\nSource string = %s", source ) ;
printf ( "\nTarget string = %s", target ) ;
strcat ( target, source ) ;
printf ( "\nTarget string after strcat( ) = %s", target ) ;
}

C STRCPY()
#include <stdio.h>
#include <string.h>
int main( )
{
char source[ ] = "fresh2refresh" ;
char target[20]= "" ;
printf ( "\nsource string = %s", source ) ;
printf ( "\ntarget string = %s", target ) ;
strcpy ( target, source ) ;
printf ( "\ntarget string after strcpy( ) = %s", target ) ;
return 0;
}
C STRLEN()
#include <stdio.h>
#include <string.h>
int main( )
{
int len;
char array[20]="fresh2refresh.com" ;
len = strlen(array) ;
printf ( "\string length = %d \n" , len ) ;
return 0;
}
C STRCMP()
#include <stdio.h>
#include <string.h>
int main( )
{
char str1[ ] = "fresh" ;
char str2[ ] = "refresh" ;
int i, j, k ;
i = strcmp ( str1, "fresh" ) ;

j = strcmp ( str1, str2 ) ;


k = strcmp ( str1, "f" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
return 0;
}
C STRREV()
#include<stdio.h>
#include<string.h>
int main()
{
char name[30] = "Hello";
printf("String before strrev( ) : %s\n",name);
printf("String after strrev( ) : %s",strrev(name));
return 0;
}

WRITE A C PROGRAM TO PRINT THE MULTIPLICATION OF TWO MATRICES

#include<stdio.h>
#include<conio.h>
void main()
{
int Matrix A[9][9] , MatrixB[9][9] , Matrixsproduct [9][9] ;
int n , i , j , k;

/* 'i' used for rows and 'j' used for columns */

int Row1 , Row2 , Column1 , Column2;


clrscr();
printf(" Enter the order of Matrix A\n");
scanf("%d * %d " , &Row1 , &Column1);
printf(" Enter the order of Matrix B\n");
scanf("%d * %d " , &Row2 , &Column2);
if(Column1 == Row2)
{
printf(" Enter the elements of Matrix A\n");
for(i=0 ; i<Row1 ; i++)
{
for(j=0 ; j<Column1 ; j++)
{
scanf("%d" , &Matrix A[i][j] );
}
}
printf(" Enter the elements of Matrix B\n");

for(i=0 ; i<Row2 ; i++)


{
for(j=0 ; j<Column2 ; j++)
{
scanf("%d" , &Matrix B[i][j] );
}
}
/* Product of two Matrices */
for(i=0 ; i<Row1 ; i++)
{
for(j=0 ; j<Column2 ; j++)
{
Matrixproduct[i][j] = 0 ;
for(k=0 ; k<Row2 ; k++)
{
Matrixproduct[i][j] = Matrixproduct[i][j] + ( Matrix A[i][k] * Matrix B[k][j] );
}
}
}
printf(" Product Matrix\n");
for(i=0 ; i< Row1 ; i++)
{
for(j=0 ;j< Column2;j++)
{
printf("%d" , Matrixproduct[i][j] );
}
printf("\n");
}
}

/* End of if */

else
{
printf(" Invalid order so Multiplication not possible\n");
}
/* End of main() */

WRITE A C PROGRAM TO PRINT THE SUM OF TWO MATRICES OF SAME


ORDER

#include<stdio.h>
#include<conio.h>
void main()
{
int Matrix1 [2][2] , Matrix2 [2][2] , Matrixsum[2][2] ;
int i, j;
/* 'i' used for rows and 'j' used for columns */
clrscr();

printf(" Enter the elements of Matrix1\n");


for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
scanf("%d" , &Matrix1[i][j] );
}
}
/* Elements of Matrix Read (Input) with the help of scanf() Function */

printf(" Enter the elements of Matrix2\n");


for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
scanf("%d" , &Matrix2[i][j] );
}
}

/ * Sum of two Matrices */


for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
Matrixsum[i][j] = Matrix1[i][j] + Matrix2[i][j] ;
}
}
printf(" Sum Matrix \n");
for(i=0 ; i<2 ; i++)
{
for(j=0 ; j<2 ; j++)
{
printf("%d" , Matrixsum[i][j] );
}
printf("\n");
}
getch();
}

/* End of main */

You might also like