0% found this document useful (0 votes)
5 views

Array

Uploaded by

manavjpatel2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Array

Uploaded by

manavjpatel2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Array

Introduction
• Array is a collection of variables of same data type known by a single
name.
• The array is an important concept and helps the programmer in managing
many variables of the same type, because all the elements of an array
share a single name.
• Date Types:
• Fundamental data types : int, float, char
• Derived data types: arrays, pointers
• User defined data types: structures, unions
• Arrays can be divided into three categories
1. Single dimensional array
2. Two-dimensional array
3. Multi-dimensional array
Single Dimensional Array
 The syntax for declaring a single dimensional array is :
datatype arrayname[size];

 Data type can be int, char, float , long int etc. The arrayname is the
name of a variable which represents the array, while size which is
represented in [ ] symbols represents the size of array.

For example, int a[10];

 In ‘C’ language array index or subscript starts from 0. where a[0] is


the first element of an array, while a[9] is the last element.
Program – sum and average of numbers
using array
#include <stdio.h>
#include <conio.h>
main()
{
int a[10]; /* array of size 10 defined */
int i,n;
float avg,sum =0; /* sum initialized to 0 */
clrscr();
printf(“Give value of n (not more than 10)\n”);
scanf(“%d”,&n); /* actual array size in n */
for(i=0;i<n;i++)
{
printf(“Give number\n”);
scanf(“%d”,&a[i]); /* store array elements in array a */
sum = sum + a[i]; /* go on adding array element to sum */
}
avg = sum/n; /* sum over. Calculate average */
printf(“Array elements are :\n”);
for (i=0; i<n;i++) /* print array elements */
printf(“%4d”,a[i]);
printf(“\nSum = %f Average = %6.2f\n”,sum,avg); /* print answer */
}
Program – to find smallest and largest
using array
#include <stdio.h>
#include <conio.h>
main()
{
int a[10]; /* array of size 10 defined */
int i,n;
int max,min;
clrscr();
printf(“Give value of n (not more than 10)\n”);
scanf(“%d”,&n); /* actual array size in n */
for(i=0;i<n;i++)
{
printf(“Give number\n”);
scanf(“%d”,&a[i]);
}
Program – to find smallest and largest
using array (cont)
max = a[0]; /* initialize min and max */
min = a[0];
for (i=1; i<n;i++)
{
if (max < a[i])
max = a[i];
if (min > a[i])
min = a[i];
}
printf(“Array elements are :\n”);
for (i=0; i<n;i++)
printf(“%4d”,a[i]);
printf(“\nLargest = %d Smallest = %d\n”,max,min);
}
Program to Insert an element at a specific position in
an Array
#include <stdio.h>

int main()
{
int arr[100];
int i, x, pos, n;
scanf("%d ", &n);
for (i = 0; i < n; i++)
scanf("%d ", &arr[i]);
scanf("%d ", &x);
scanf("%d ", &pos);
n++;
for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Two dimensional array
Sometimes we need to store data where more than one
dimensions are involved, like sales information of a company, or
for mathematical calculations we need to use matrix.
The syntax for two-dimensional array is :
datatype variablename[rowsize][colsize];
where, variablename represents the name of an array, rowsize
indicate the number of rows in table, colsize indicates number of
columns in an array.
Program – to add two matrices
#include <stdio.h>
#include <conio.h>
main()
{
int a[4][4], b[4][4],c[4][4]; /* matrix c stores result i.e c = a +b */
int m,n,p,q; /* m no.of rows in a, n no. of cols in a*/
int i,j; /* p no.of rows in b, q no. of cols in b*/
clrscr();
printf(“Give number of rows in first matrix\n”);
scanf(“%d”,&m);
printf(“Give number of columns in first matrix\n”);
scanf(“%d”,&n);
printf(“Give number of rows in second matrix\n”);
scanf(“%d”,&p);
printf(“Give number of columns in second matrix\n”);
scanf(“%d”,&q);
Program – to add two matrices (cont)
if( m!= p || n!= q) /* check size match or not */
{
printf(“Size do not match. Addition not possible\n”);
return 0;
}
printf(“Enter matrix A row-wise\n”);
for(i=0;i<m;i++) /* Get first matrix data */
{
for(j=0;j<n;j++)
{
printf(“a[%d][%d]= “,i,j);
scanf(“%d”,&a[i][j]);
}
printf(“\n”);
}
Program – to add two matrices (cont)
printf(“Enter matrix B row-wise\n”);
for(i=0;i<p;i++) /* Get second matrix data */
{
for(j=0;j<q;j++)
{
printf("b[%d][%d]= ",i,j);
scanf("%d“,&b[i][j]);
}
printf(“\n”);
}
printf(“Matrix A \n”);
for(i=0;i<m;i++) /* display first matrix row-wise */
{
for(j=0;j<n;j++)
printf(“%4d”,a[i][j]);
printf(“\n”);
}
Program – to add two matrices (cont)
printf(“Matrix B \n”);
for(i=0;i<p;i++) /* display second matrix row-wise */
{
for(j=0;j<q;j++)
printf("%4d“,b[i][j]);
printf("\n“);
}
printf(“\nSum Matrix C \n”);
for(i=0;i<m;i++) /* calculate sum and display result matrix */
{
for(j=0;j<n;j++)
{
c[i][j] = a[i][j] + b[i][j];
printf("%4d“,c[i][j]);
}
printf(“\n”);
}
}

You might also like