0% found this document useful (0 votes)
108 views7 pages

Arrays in C: Example Where Arrays Are Used

The document discusses arrays in C language. It explains what arrays are, how to declare and initialize arrays, how to access array elements, and how to perform operations like traversing, sorting and searching on arrays. It also provides examples of one dimensional and two dimensional arrays and operations like addition and multiplication of matrices.

Uploaded by

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

Arrays in C: Example Where Arrays Are Used

The document discusses arrays in C language. It explains what arrays are, how to declare and initialize arrays, how to access array elements, and how to perform operations like traversing, sorting and searching on arrays. It also provides examples of one dimensional and two dimensional arrays and operations like addition and multiplication of matrices.

Uploaded by

Shivam Saxena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Arrays in C

In C language, arrays are reffered to as structured data types. An array is defined as finite
ordered collection of homogenous data, stored in contiguous memory locations.
Here the words,
 finite means data range must be defined.
 ordered means data must be stored in continuous memory addresses.
 homogenous means data must be of similar data type.
Example where arrays are used,
 to store list of Employee or Student names,
 to store marks of students,
 or to store list of numbers or characters etc.
Since arrays provide an easy way to represent data, it is classified amongst the data structures in
C. Other data structures in c are structure, lists, queues, trees etc. Array can be used to
represent not only simple list of data but also table of data in two or three dimensions.
Declaring an Array
Like any other variable, arrays must be declared before they are used. General form of array
declaration is,
data-type variable-name[size];
/* Example of array declaration */
int arr[10];
Here int is the data type, arr is the name of the array and 10 is the size of array. It means array
arr can only contain 10 elements of int type.
Index of an array starts from 0 to size-1 i.e first element of arr array will be stored at arr[0]
address and the last element will occupy arr[9].
Initialization of an Array
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any
random value). An array can be initialized at either compile time or at runtime.
Compile time Array initialization
Compile time initialization of array elements is same as ordinary variable initialization. The
general form of initialization of array is,
data-type array-name[size] = { list of values };
/* Here are a few examples */
int marks[4]={ 67, 87, 56, 77 }; // integer array initialization
float area[5]={ 23.4, 6.8, 5.5 }; // float array initialization
int marks[4]={ 67, 87, 56, 77, 59 }; // Compile time error
One important thing to remember is that when you will give more initializer(array elements) than
the declared array size than the compiler will give an error.
#include<stdio.h>
void main()
{
int i;
int arr[] = {2, 3, 4}; // Compile time array initialization
for(i = 0 ; i < 3 ; i++)
{
printf("%d\t",arr[i]);
}
}
234
Runtime Array initialization
An array can also be initialized at runtime using scanf() function. This approach is usually used
for initializing large arrays, or to initialize arrays with user specified values. Example,
#include<stdio.h>
void main()
{
int arr[4];
int i, j;
printf("Enter array element");
for(i = 0; i < 4; i++)
{
scanf("%d", &arr[i]); //Run time array initialization
}
for(j = 0; j < 4; j++)
{
printf("%d\n", arr[j]);
}
}
Two dimensional Arrays
C language supports multidimensional arrays also. The simplest form of a multidimensional
array is the two-dimensional array. Both the row's and column's index begins from 0.
Two-dimensional arrays are declared as follows,
data-type array-name[row-size][column-size]
/* Example */
int a[3][4];
An array can also be declared and initialized together. For example,
int arr[][3] = {
{0,0,0},
{1,1,1}
};
Note: We have not assigned any row value to our array in the above example. It means we can
initialize any number of rows. But, we must always specify number of columns, else it will give
a compile time error. Here, a 2*3 multi-dimensional matrix is created.
Runtime initialization of a two dimensional Array
#include<stdio.h>
void main()
{
int arr[3][4];
int i, j, k;
printf("Enter array element");
for(i = 0; i < 3;i++)
{
for(j = 0; j < 4; j++)
{
scanf("%d", &arr[i][j]);
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
{
printf("%d", arr[i][j]);
}
}
}
/*Program to find the average of n numbers using arrays */
#include <stdio.h>
#include <conio.h>
int main()
{
int marks[10], i, n, sum = 0 ;
float average;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
}
for(i=0; i<n; ++i)
sum += marks[i];
average = (float)sum/n;
printf("Average = %.2f", average);
getch();
return 0;
}
/* C Program to Increment every Element of the Array by one & Print
Incremented Array*/
#include <stdio.h>
#include <conio.h>
int main()
{
float A[10];
int i , n ;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%f", &A[i]);
}
for(i=0; i<n; ++i)
A[i]+= A[i]+1;
for(i=0; i<n; ++i)
printf("Element %d is: %.2f ",i+1, A[i]);
getch();
return 0;
}
/* C program for matrix addition: C=A+B*/
#include <stdio.h>
#include <conio.h>
int main()
{
int m, n, i, j, A[10][10], B[10][10], C[10][10];
clrscr();
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &A[i][j]);
printf("Enter the elements of second matrix\n");
for (i = 0; i < m; i++)
for (j = 0 ; j < n; j++)
scanf("%d", &B[i][j]);
printf("Sum of entered matrices:-\n");
for (i = 0; i < m; i++) {
for (j = 0 ; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}

/*C program to reverse an array*/

#include <stdio.h>

#include <conio.h>

int main()

int n, i, j, a[100], b[100];

clrscr();

printf("Enter the number of elements in array\n");

scanf("%d", &n);
printf("Enter array elements\n");

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

scanf("%d", &a[i]);

for (i = n - 1, j = 0; i>= 0; i--, j++)

b[j] = a[i];

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

a[i] = b[i];

printf("Reverse array is\n");

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

printf("%d\n", a[i]);

getch();

return 0;

/*Matrix multiplication in C language*/

#include <stdio.h>

#include <conio.h>

int main()

int m, n, p, q, i, j, k;

int A[3][3], B[3][3], C[3][3];


clrscr();

printf("Enter number of rows and columns of first matrix\n");

scanf("%d%d", &m, &n);

printf("Enter elements of first matrix\n");

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

for (j = 0; j < n; j++)

scanf("%d", &A[i][j]);

printf("Enter number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if (n != p)

printf("The matrices can't be multiplied with each other.\n");

else

printf("Enter elements of second matrix\n");

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

for (j = 0; j < q; j++)

scanf("%d", &B[i][j]);

for (i = 0; i < m; i++) {

for (j = 0; j < q; j++) {

C[i][j] = 0;

for (k = 0; k < p; k++) {

C[i][j]= C[i][j] + A[i][k]*B[k][j];

}
}

printf("Product of the matrices:\n");

for (i = 0; i < m; i++) {

for (j = 0; j < q; j++)

printf("%d\t", C[i][j]);

printf("\n");

getch();

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy