Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

UNIT-II

Arrays & Strings


Arrays in C :
 Collection of more than one data item.
 All the data items should be of some type.
Syntax :
Data type name of array[size of array];
eg: int a[5];
Declaration :
int a[ ];  it should not be empty
int a[-s]; it should contain only positive number
int a[2+2];
int a[3*2];  all are correct
int a[10/2];
eg: int a[5];

1000 1002 1004 1006 1008

Macros can be used :


eg) # define N 5
main();
{ int n;
int a[N]; int a[n];
}
Initialization :
At compile time :
1. int a[5] = {0,-1,11,10,2};
2. int a[ ] = {0,1,2,0,-1,6,7};
3. int a[5]= {0,1,-1} 0 1 -1 0 0
4. int a[5]; it store garbage value
5. int a[5] = {1,2,3,4,5,6,7}  error

6. int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
7. int a[5]= {0 }
8. int a[5] ={ } ;error
char b[ ]={‘c’, ‘s’,’E’}
At runtime :
int i ;
int a[5];
eg 1:
printf(“Enter the elements of array”};
for(i=0;i<5;i++)
{
Scanf(“%d”,&a[i]);
}
eg 2:
int a[100],i;
for(i=0;i<=100;i++)
{
if (i<30)
a[i]=1;
else
a[i]=0;
}
Memory Representation & accessing of array:
int a[5]={1,10,0,-1,2}
1 10 0 -1 2
………. ……

In array data will be stored in continuous allocation of memory.


0 1 2 3 4

1 10 0 -1 2
base address 2000 2002 2004 2006 2008

Accessing :
To access an element of an array we have to use index value and name of the array.
0 1 2 3 4

1 10 0 -1 2
eg: if we want to take 1 element
a[0]=1 1st element
a[3]= -1  4th element
To calculate the address of the element :
B+(index * size of int)
2000+(3*2)
2000+6=2006
Note :
 Array is a collection of more than one data item of same type.
 All data items are stored in contiguous memory location.
 Number of data items array hold is size of array.
 Once size has declared it can’t be changed at run time ( fixed size).
 Index starts from zero /0.
 It is known as derived data type.
 Accessing of any element is faster using index of array.
 It allows to store data in multi dimensional form.
 Inserting & Deleting is tough.
 No boundary checking in C.
eg :
#include<stdio.h>
#include<conio.h>
void main()
int i,a[5];
for(i=0;i<5;i++)
{
scanf(“%d”,&a);
}
for(i=0;i<5;i++) // for(i=4;i>=0;i--)
{
printf(“%d\n”,a[i]);
}
getch();
}
Read marks of 5 students ,calculate sum & avg using array.
#include<stdio.h>
#include<conio.h>
void main()
int marks[5];
int sum=0,avg;
for(i=0;i<5;i++)
{
scanf(“%d”,&marks[i]);
}
for( i=0;i<5,i++)
{
sum=sum+marks[i];
}
avg=sum/5;
printf(“%d”,sum);
printf(“%d”,avg);
}
To read an array of 10 integers & count total no.of even and odd elements.
#include<stdio.h>
void main()
{
int a[10],i;
int even=0,odd=0;
printf(“enter array elements:”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
if(a[i]%2==0)
even++;
else
odd++;
}
printf(“total no.of even no are :%d”,even);
printf(“total odd no are :%d”,odd);
}
Two dimensional array:
 Collection of 1d array
 Also known as array of array.
Syntax:
Datatype arrayname [row][column];
eg: int arr[3][3];
 It is also called matrix.
Memory allocation:
int a[4][3];
C1 C2 C3

R1
a[0][0] a[0][1] a[0][2]

R2
a[1][0] a[1][1] a[1][2]

R3
a[2][0] a[2][1] a[2][2]

R4
a[3][0] a[3][1] a[3][2]

Program : compilation time


#include<stdio.h>
#include<conio.h>
void main()
{
int a[4][3] ={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int i,j;
clrscr();
printf(“The values are \n”);
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
getch();
}
Program : Run time
#include<stdio.h>
#include<conio.h>
void main()
{
int a[4][3];
int i,j;
clrscr();
printf(“Enter the values”);
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
printf(“The values in matrix form:”);
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
getch();
}
Program to print 2D array & calculate sum.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][3];
int i,j,sum=0;
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,a[i][j]);
sum=sum+a[i][j];
}
printf(“\n”);
}
printf(“sum=%d”,sum);
}
Program to print addition of 2 matrices:
#include<stdio.h>
Int a[2][3],b[2][3],c[2][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
Program to print multiplication of two matrix:
1 5 6 1 2 3
2 2 5 * 0 1 5
1 7 5 1 0 2

1*1+5*0+6*1 1*2+5*1+6*0 1*3+5*5+6*2

2+0+5=(7) 4+2+0=6 6+10+10=26

1+0+5=6 2+7+0=9 3+35+10=48

7 7 40
7 6 26 Product
6 9 48
**column of 1st matrix=row of 2nd matrix we can do multiply**
#include<stdio.h>
#include<conio.h>
#define N 50
int main()
int a[N][N],b[N][N],c[N][N],I,j,k,sum,m,n,p,q;
printf(“Enter rows and columns for 1st matrix:\n”);
scanf(“%d %d”,&m,&n);
printf(‘Enter first matrix;\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,a[i][j]);
}
}
printf(“Enter rows and columns for 2nd matrix:\n”);
scanf(“%d %d”,&p,&q);
printf(‘Enter second matrix;\n”);
for(i=0;i<pi++)
{
for(j=0;j<q;j++)
{
scanf(“%d”,b[i][j]);
}
}
Printf(“\n first matrix is :\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
Printf(“\n second matrix is :\n”);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,b[i][j]);
}
printf(“\n”);
}
if (n!=p)
{
printf(“can not multiply “);
}
else
{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
sum=0;
for (k=0;k<m;k++)
{
sum=sum+(a[i][j]+b[i][j]);
}
c[i][j]=sum;
}
}
printf(“multiplication is :\n”);
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}}

You might also like