Unit-6 ArrayandStrings
Unit-6 ArrayandStrings
CHAPTER - 6
Array and Strings
1. Introduction to Array
An array is the collective name given to a group of similar quantities. Or an array is a set of similar data elements
which are stored in consecutive memory locations under a common variable name. The individual values in an
array are called elements. Array is sets of values of the same type, which have single name followed by an index.
The similar element of an array would be all integers or all floats or all characters. Usually, the array of
character is called string.
The memory space taken by various array elements are shown below:
int → 2 byte memory space
float → 4 byte memory space
char → 1 byte memory space
Limitations of array:
Conventional arrays are static in nature i.e. memory is allocated in the beginning of the programand it cannot be
changed dynamically. If n is the size of an array and only m elements are needed then n - m locations are
unnecessarily wasted.
2. Array Declaration
Declaring the name and type of an array and setting the number of elements in the array is known as
dimensioning(declaring) the array. It must be declared before it is used like other variables. In the array
declaration, we must define the type of the array, name of the array and number of subscripts in the array. In
general, one-dimensional array may be expressed as:
data_type array_name[size];
Example:
int marks[300];
where, int = Data type or Type of variable
marks = Name of array variable
300 = Dimension(size) of array
[ ] = Tells the compiler that we are dealing with an array
LECTURE NOTES BY PRITHVI RAJ PANERU(B.Sc.CSIT First Semester)
3. Dimension of Array
The dimension denotes the size of array that is classified as one or twoor more dimension according to the number of
subscript using on it.
For example:
int values[3]={10,11,12}; or int values[]={10,11,13};
where,
values[0]=10
values[1]=11
values[2]=12
for(i=9;i>=0;i--)
printf("%d \t", arr[i]);
getch();
}
Example3: Write a program to read n numbers in an array and find the largest and smallest number.
#include<stdio.h>
#include<conio.h>
#define n 50
void main( )
{
int x[n], i, large,small;
for(i=0;i<=n-1;i++)
{
scanf("%d", &x[i]);
}
large=x[0];
small=x[0];
for(i=1;i<=n-1;i++)
{
if(x[i]>large)
{
large=x[i];
}
if(x[i]<small)
{
small=x[i];
}
}
printf("\n the largest number is %d \n the smallest value is %d", large,small);
getch();
}
Example4: Write a program to read 5 integer numbers in an array and sort it in ascending order.(Exam 2010fall,2012
spring)
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={10,2,13,8,7},i,j,temp;
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n the array after sort is:");
LECTURE NOTES BY PRITHVI RAJ PANERU(B.Sc.CSIT First Semester)
for(i=0;i<=4;i++)
{
printf("%4d",arr[i]);
}
getch();
}
Example7: WAP to find sum of all prime numbers in a given array. (Exam 2013 fall)
#include<stdio.h>
#include<conio.h>
#define n 10
void main()
{
int arr[n],i,j,sum=0;
//to read n array elements
for(i=0;i<=n-1;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<=n-1;i++)
{
for(j=2;j<=arr[i]-1;j++)
{
if(arr[i]%j==0)
{
break;
}
}
if(j==arr[i])
{
sum=sum+arr[i];
}
}
printf("\n the sum of all the prime numbers in an array =%d",sum);
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d", &x[i][j]);
}
}
//to display array elements
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t", x[i][j]);
}
printf("\n");
}
getch();
}
Example2: Write a program to read two 3x3 matrices and add them.
#include<stdio.h>
#include<conio.h>
void main( )
{
int x[3][3]={{1,2,3},{4,5,6},{7,8,9}}, y[3][3]={{1,1,1},{2,2,2},{3,3,3}},z[3][3], i, j;
//to add two 3*3 matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
z[i][j] = x[i][j]+y[i][j];
}
}
printf("\n the matrix after addition is\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t", z[i][j]);
}
printf("\n");
}
getch();
}
{
for(j=0;j<=2;j++)
{
scanf("%d",&x[i][j]);
}
}
printf("enter 9 elements of second matrix");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&y[i][j]);
}
}
//to multiply two matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
z[i][j] = 0;
for(k=0;k<=2;k++)
{
z[i][j]=z[i][j]+x[i][k]*y[k][j];
}
}
}
printf("\n the resultant matrix is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t", z[i][j]);
}
printf("\n");
}
getch();
}
Example4: Write a program to find the norm of a matrix. (Norm of a matrix is the square root of sum of
square of individual elements.)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int x[3][3], i, j, sum=0, norm;
printf("enter 9 elements");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d", &x[i][j]);
LECTURE NOTES BY PRITHVI RAJ PANERU(B.Sc.CSIT First Semester)
}
}
for(i=0;i<=2;i++) /*find sum of square*/
{
for(j=0;j<=2;j++)
{
sum=sum+x[i][j]*x[i][j];
}
}
norm=sqrt(sum); /*find norm*/
printf("\n the norm of the matrix is %d", norm);
getch();
}
}
Example5: Write a program to find the transpose of a 4*5 matrix. (Exam 2010 fall)
#include<stdio.h>
#include<conio.h>
void main( )
{
int x[4][5], i, j;
printf("enter 20 elements");
for(i=0;i<=3;i++)
{
for(j=0;j<=4;j++)
{
scanf("%d", &x[i][j]);
}
}
for(i=0;i<=4;i++) /*transpose*/
{
for(j=0;j<=3;j++)
{
printf("%d \t", x[j][i]);
}
printf("\n");
}
getch();
}
Example6: WAP to find sum of even elements of 3*3 matrix.(Exam 2011 spring)
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[3][3]={{1,2,3},{4,5,6},{7,8,9}}, i,j,sum=0;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(arr[i][j]%2==0)
LECTURE NOTES BY PRITHVI RAJ PANERU(B.Sc.CSIT First Semester)
{
sum=sum+arr[i][j];
}
}
}
printf("\n the sum of even elements of 3*3 matrix is:%d",sum);
getch();
}
Example7: WAP to find the average of all the elements of m*n matrix(Exam 2010 spring)
#include<stdio.h>
#include<conio.h>
#define m 3
#define n 4
void main()
{
int arr[m][n],i,j,sum=0;
float avg;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
sum=sum+arr[i][j];
}
}
avg=sum/12;
printf("the sum=%d and average=%f",sum,avg);
getch();
}
Example8: WAP to input m*n matrix and convert it into upper triangular matrix and display it. (Exam 2012 spring)
#include<stdio.h>
#include<conio.h>
#define m 4
#define n 4
void main()
{
int arr[m][n],i,j;
printf("enter the elemnets in the matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
LECTURE NOTES BY PRITHVI RAJ PANERU(B.Sc.CSIT First Semester)
}
for(i=1;i<m;i++)
{
for(j=0;j<i;j++)
{
arr[i][j]=0;
}
}
printf("\n the upper triangular matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
clrscr();
}
Example 9:WAP to find the sum of diagonal elements from the left (the trace of a matrix).(Exam 2008 fall)
#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[3][3],sum=0,i,j;
printf("enter the 3*3 matrix:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==j)
{
sum=sum+matrix[i][j];
}
}
}
printf("\n the sum of diagonal element of 3*3 matrix is:%d",sum);
getch();
clrscr();
}
OR
LECTURE NOTES BY PRITHVI RAJ PANERU(B.Sc.CSIT First Semester)
#include<stdio.h>
#include<conio.h>
void main( )
{
int matrix[3][3], i, j, sum=0;
printf("enter the matrix elements\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d", &matrix[i][j]);
}
}
for(i=0;i<=2;i++)
{
sum = sum + matrix[i][i];
}
printf("the trace of the matrix is %d", sum);
getch();
}
The elements of the string are stored in contiguous memory locations. When we initialize the string, the compiler
automatically puts a null character („\0‟) at the end of last character of the array. The '\0' represents the end of the
string. We can declare it as one or multi-dimensional array.
char A[20];
printf("Emter any string\n");
scanf("%s", A);
printf("%s", A);
getch();
}
Example1. WAP to reverse a string entered from keyboard using in build string function.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char A[10];
printf("Enter a string:");
gets(A);
puts(strrev(A));
getch();
clrscr();
}
Example2. WAP to reverse a string entered from keyboard without using strrev() function
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i, len;
char A[10];
printf("Enter a string:");
scanf("%s",A);
len=strlen(A);
for (i=len-1;i>=0;i--)
{
printf("%c", A[i]);
}
getch();
clrscr();
}
Example3. WAP to copy string from one array onto another using in build string function
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char A[10],B[10];
printf("Enter a string:");
scanf(" %s",A);
LECTURE NOTES BY PRITHVI RAJ PANERU(B.Sc.CSIT First Semester)
strcpy(B,A);
printf("the content of B is: %s", B);
getch();
clrscr();
}
Example4. A program to calculate the length of the string entered from keyboard.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int len;
char A[10];
printf("Enter a string:");
gets(A);
len=strlen(A);
printf("%d", len);
getch();
clrscr();
}
Example5. WAP to concatenate two strings inputted through keyboard. The result of concatenation should
be displayed after copying on third variable.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i=0, l=0;
char A[10],B[10],C[20];
printf("Enter first string:");
gets(A);
printf("Enter second string:");
gets(B);
strcpy(C, strcat(A,B));
puts(C);
getch();
clrscr();
}
ENGINEE
ENGINEER
ENGINEERI
ENGINEERIN
ENGINEERING
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j;
char n[20]="ENGINEERING";
for(i=0;i<strlen(n);i++)
{
for(j=0;j<=i;j++)
{
printf("%c",n[j]);
}
printf("\n");
}
getch();
}