0% found this document useful (0 votes)
63 views9 pages

Array Concept Using C: by Ms. P.Jayalakshmi, Ap/Cse Ms. K.Ramya, AP/ CSE

The document discusses arrays in C programming language. It defines arrays as collections of similar data types stored in contiguous memory locations. It describes one-dimensional and multi-dimensional arrays. A one-dimensional array example computes the mean, median, and mode of elements in an integer array. A two-dimensional array example program demonstrates matrix operations like addition, subtraction, multiplication, determinant and transpose on integer matrices.

Uploaded by

JAYALAKSHMI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
63 views9 pages

Array Concept Using C: by Ms. P.Jayalakshmi, Ap/Cse Ms. K.Ramya, AP/ CSE

The document discusses arrays in C programming language. It defines arrays as collections of similar data types stored in contiguous memory locations. It describes one-dimensional and multi-dimensional arrays. A one-dimensional array example computes the mean, median, and mode of elements in an integer array. A two-dimensional array example program demonstrates matrix operations like addition, subtraction, multiplication, determinant and transpose on integer matrices.

Uploaded by

JAYALAKSHMI P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 9

ARRAY CONCEPT USING C

By

Ms. P.JAYALAKSHMI, AP/CSE

Ms. K.Ramya, AP/ CSE

1
INTRODUCTION TO ARRAYS
Array is the collection of similar data types stored in contiguous memory location.
 Array might be belonging to any of the data types
 Array size must be a constant value.
 Array variable can store more than one value at a time.
 Symbolic constant can also be used to specify the size of the array (#define SIZE 10;

Syntax:
data-type arr_name[array_size];
Array declaration, Example
initialization and
accessing

Array declaration syntax: Integer array example:


data_type int arr [5];
arr_name[arr_size];
arr[0] arr[1] arr[2] arr[3 arr[4]
]

First Element Last


Element

Array initialization int age[5]={0, 1, 2, 3, 4};


syntax:
data_type arr_name
[arr_size]=(value1, age[0]; /*0 is accessed*/
value2, value3,….); age[1]; /*1 is accessed*/
age[2]; /*2 is accessed*/
Array accessing syntax:
arr_name[index]; Character array example:
char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;
Types of array

 One dimensional array

 Multi dimensional array

 Two dimensional array

 Three dimensional array

 Four dimensional array etc…

ONE DIMENSIONAL ARRAY


Syntax:
Data_type array_name[size];

Example:
int marks[10];

Example Program: Computing Mean, Median and Mode


Algorithm:
1. Start
2. Declare an array a[20], sum =0,i=0,j=0,z=0
3. Read number of terms n
4. Repeat step 5 & 6 while (i<n)
5. sum=sum+a[i]
6. next i
7. mean=sum/n
8. if (n%2=0)
median=(a[n/2] +a[n/2-1])/2
else
median=a[n/2]
9. print mean and median
10. repeat step 11 &  12 when i=0 to n, j=0 to i
11. if a[i]=a[j], then b[i]++
12. if b[i]>z, then z=b[i]
13. For i=0 to n if b[i]=z, print b[i]
14. end

Program:

#include <stdio.h>

void main()
{
int i,j,x,k=0,n,a[20],z=0,b[20];
float sum=0, t,mean,medn,mod;
printf(“\n Enter the no. of elements (Max 20)\n”);
scanf(“%d”,&n);
printf(“Enter the elements\n”);
for(i=0;i<n;i++)
{
scan (“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf(“Sum: %f”,sum);
mean=sum/n;
}
for(i=0;i<n;i++)
{
for (j=i+1;j<n;j++)
  {
if (a[i]>a[j])
      {
t=a[i];
a[i]=a[j];
a[j]=t;
}
 }
}
if (n%2==0)
 medn=(a[n/2]+a[(n/2)-1])/2;
else
medn=a[n/2];
for(i=0;i<n;i++) //check
{
for ( j=0; j<i; j++ )
  {
if (a[i]==a[j])
      b[i]++;
  }
}
for(i=0;i<n;i++)
{
if (b[i]>z)
   z=b[i];
}
printf(“Mean :%f \n Median : %f \n Mean : “, mean, medn);
for ( i=0; i<n; i++)
 {
if (b[i]==z)
  printf(“%d\t”, a[i]);
}
}
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 begin from 0.

Two-dimensional arrays are declared as follows,

data-type array-name[row-size] [column-size]

Example:
int a[3][4];

EXAMPLE PROGRAM:

Matrix Operations (Addition, Scaling, Determinant and Transpose)

#include<stdio.h>
#include<conio.h>
int main()
{
int m,n;
int a[20][20],b[20][20];
int i,j;
int sum[20][20],sub[20][20];
int opt,tr[20][20],opt1,ch,e,f;
printf("Note : For Addition or Subtraction , no. of rows and columns
should be same and for transpose of matrices , your first matrices entered
should be the desired matrices .\n");
printf("Enter the no. of rows: ");
scanf("%d",&m);
printf("Enter the no. of columns: ");
scanf("%d",&n);
printf("Enter the Data Elements of first matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the no. of rows for second matrices: ");
scanf("%d",&e);
printf("Enter the no. of columns: ");
scanf("%d",&f);
printf("Enter the Data Elements of second matrices\n");
for(i=0;i<e;i++)
{
for(j=0;j<f;j++)
{
scanf("%d",&b[i][j]);
}
}
do
{
if(m==e&&n==f)
{
printf("Enter 1 for addition or subtraction of matrices\n");
if(n==e)
{
printf("Enter 2 for multiplication of matrices\n");}
printf("Enter 3 for transpose of first matrices\n");
}
else if(m!=n&&n==e)
{
printf("Enter 2 for multiplication of matrices\n");
printf("Enter 3 for transpose of first matrices\n");
}
else
{
printf("Enter 3 for transpose of first matrices\n");
}
scanf("%d",&ch);
switch(ch)
{
case 1 :
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum[i][j]=a[i][j]+b[i][j];
sub[i][j]=a[i][j]-b[i][j];
}
}
printf("Enter 1 for Addition or 2 for Subtraction: ");
scanf("%d",&opt);
switch(opt)
{
case 1 :
printf("The resultant matrices is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",sum[i][j]);
}
printf("\n");
}
break;
case 2 :
printf("The resultant matrices is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",sub[i][j]);
}
printf("\n");
}
}
break;
case 2 :
printf("The resultant matrices is : \n");
int k;
for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
{ sum[i][j]=0;
for(k=0;k<m;k++)
{
sum[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",sum[i][j]);
}
printf("\n");
}
break;
case 3 :
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
tr[j][i]=a[i][j];
}
}
printf("The resultant matrices is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%3d",tr[i][j]);
}
printf("\n");
}

break; }}
while(ch>0);
getch();
}

You might also like