In C we can have arrays of two or more dimensions. The two dimensional array is also known as a matrix. A two dimensional array is a grid having rows and columns in which each element is specified by two subscripts. It is the simplest of multidimensional arrays. For example, An array a[m][n] is an m by n table having m rows and n columns containing m x n elements. The size of the array (total number of elements) is obtained by calculating m x n.
Declaration
Datatypearr_name[rows][cols]; Example: inta[3][5];
The array will be stored in the form given below:
The number of rows or columns is called the range of the dimension.
a to w – dimensional array is a logical data structure that is useful in programming and problem solving.
Implementing two dimensional array
Computer memory is usually linear (one-dimensional array).We need to transform to w dimensional reference to linear representation.Two methods to representing a to w dimensional array in memory: Row-major & array of pointers.
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
77
Row-major
Array of pointers
Calculatetheaddressofanelementin2Dimensional-array(using row-major method):
Suppose we have: intar[r1][r2]; r1=number of rows, r2=number of columns.
base(ar) : the address of first element in array esize : the size of each element in ar. Now we want to calculate the address of the element ar[i1][i2]: The address of the first element in rowi1=base(ar)+i1*r2*esize. The n the address of ar[i1][i2]=base(ar)+(i1*r2+i2)*esize
Ques. WAP to print a Matrix.
#include<stdio.h> #include<conio.h> void main() { int a[3][3],b,c,i,j; clrscr();
//printf("Enter the size of Matrix(<10) i,j : "); //scanf("%d""%d",&b,&c);
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
78
{
for(j=0;j<3;j++) {
printf("Enter element (%d,%d) : ",i,j); scanf("%d",&a[i][j]); } } printf("\n\nPrintable Matrix is : \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d\t",a[i][j]); } printf("\n"); } getch(); }
Ques. WAP to add two matrixes.
#include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],d[10][10],r,c,r1,c1,i,j; clrscr();
printf("\nenter the number of row of first matrix="); scanf("%d",&r);
printf("\nenter the number of column of first matrix="); scanf("%d",&c);
printf("\nenter the number of rows of second matrix="); scanf("%d",&r1);
printf("\nenter the number of columns of second matrix="); scanf("%d",&c1); if(r==r1&&c==c1) { for(i=0;i<r;i++) { for(j=0;j<c;j++) {
printf("\nenter the %d row %d colmn of first matrix=",i,j);
scanf("%d",&a[i][j]); }
}
printf("\nthe first matrix is:"); for(i=0;i<r;i++)
{
printf("\n"); for(j=0;j<c;j++)
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
79
{ printf("\t"); printf("%d",a[i][j]); } } for(i=0;i<r1;i++) { for(j=0;j<c1;j++) {printf("\nenter the %d row %d colmn of second matrix=",i,j);
scanf("%d",&b[i][j]); }
}
printf("\n\nthe second matrix is:"); for(i=0;i<r1;i++) { printf("\n"); for(j=0;j<c1;j++) { printf("\t"); printf("%d",b[i][j]); } }
printf("\n\n\n\n\t\tthe addition of two matrix is:"); for(i=0;i<r1;i++) { printf("\n"); for(j=0;j<c1;j++) { d[i][j]=0; d[i][j]=a[i][j]+b[i][j]; printf("\t"); printf("%d",d[i][j]); } } } else { printf("\nmatrix is no possible"); } getch(); }
Ques. WAP to subtract a matrix from another matrix.
#include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],d[10][10],r,c,r1,c1,i,j; clrscr();
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
80
printf("\nenter the number of row of first matrix="); scanf("%d",&r);
printf("\nenter the number of column of first matrix="); scanf("%d",&c);
printf("\nenter the number of rows of second matrix="); scanf("%d",&r1);
printf("\nenter the number of columns of second matrix="); scanf("%d",&c1); if(r==r1&&c==c1) { for(i=0;i<r;i++) { for(j=0;j<c;j++) {
printf("\nenter the %d row %d colmn of first matrix=",i,j);
scanf("%d",&a[i][j]); }
}
printf("\nthe first matrix is:"); for(i=0;i<r;i++) { printf("\n"); for(j=0;j<c;j++) { printf("\t"); printf("%d",a[i][j]); } } for(i=0;i<r1;i++) { for(j=0;j<c1;j++) {
printf("\nenter the %d row %d colmn of second matrix=",i,j);
scanf("%d",&b[i][j]); }
}
printf("\n\nthe second matrix is:"); for(i=0;i<r1;i++) { printf("\n"); for(j=0;j<c1;j++) { printf("\t"); printf("%d",b[i][j]); } }
printf("\n\n\n\n\t\tthe subtraction of two matrix is:\n\n"); for(i=0;i<r1;i++)
{
printf("\n"); for(j=0;j<c1;j++)
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
81
{ d[i][j]=0; d[i][j]=a[i][j]-b[i][j]; printf("\t"); printf("%d",d[i][j]); } } } else { printf("\nmatrix is no possible"); } getch(); }Ques. WAP to multiply two matrices.
#include<stdio.h> #include<conio.h> void main()
{
int x[5][5], y[5][5], z[5][5], i, j, r, c, R, C,m,n,p; //variable declaration
clrscr();
start: //start : label printf("Enter Row of first Matrix : ");
scanf("%d",&r);
printf("Enter column of first Matrix : "); scanf("%d",&c);
if((r>5)||(c>5)) //case – Matrix range {
printf("\n\n\t\tMatrix out of Range! Enter Valid range..."); getch();
clrscr(); goto start; }
printf("\nEnter first Matrix \n\n");
for(i=0;i<r;i++) //loop – Mat1 scan {
for(j=0;j<c;j++) {
printf("Enter element(%d,%d) : ",i+1,j+1); scanf("%d", &x[i][j]);
} }
start2: //start2 : label clrscr();
printf("Enter Row of second Matrix : "); scanf("%d",&R);
printf("Enter column of second Matrix : "); scanf("%d",&C);
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
82
{
printf("\n\n\t\tMatrix out of Range! Enter Valid range..."); getch();
goto start2; }
if(c!=R) {
printf("\n\n\t\tCan't Multiply... Order Mismatch\nEnter Correct Order!!!");
getch(); goto start2; }
printf("\nEnter second Matrix \n\n"); m=c;
n=R;
for(i=0;i<R;i++) //loop – Mat2 scan {
for(j=0;j<C;j++) {
printf("Enter element(%d,%d) : ",i+1,j+1); scanf("%d", &y[i][j]);
} }
for(i=0;i<r;i++) //Mat3 = Mat1*Mat2 { for(j=0;j<C;j++) { z[i][j]=0; for(p=0;p<c;p++) { z[i][j]=z[i][j]+(x[i][p])*(y[p][j]); } } } clrscr(); printf("Resultant Matrix is : \n");
for(i=0;i<m;i++) //Loop – Mat3 print { for(j=0;j<n;j++) { printf("%d\t", z[i][j]); } printf("\n"); }
printf("\n\n\t\t\tPress any key to QUIT"); getch();
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
83
Array of Strings
A String is an array of char objects. An array of string can be declared and handled like a 2d(two dimensional) arrays.A String is an array of char objects. An array of string can be declared and handled like a 2d (two dimensional) arrays. You can see in the given example that we have declared a 2 dimensional character array consisting of three 'rows' and twelve 'columns'. The array is initialized with three character strings. In C, a format specifier %s is used with the printf to print the string. Declaration
charvariable_name[no. of string][length of string]; Example Program of Array of Strings :
#include <stdio.h> #include <conio.h> void main()
{
clrscr();
chararr[3][12]= { "Rose", "India", "technologies" };
printf("Array of String is = %s,%s,%s\n", arr[0], arr[1], arr[2]); getch();
}
We can also use the array of stings by using a loop.
Ques. WAP to implement print the list of student name using string.
#include"stdio.h" #include"conio.h" void main() { char name[10][10]; int i,n; clrscr();
printf("\nenter number of student:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n%dname:",i+1); scanf("%s",&name[i][0]); } printf("\nName:"); for(i=0;i<n;i++) { printf("\n%s",name[i]); } getch(); }
PROGRAMMING IN C
P
ANKAJG
ILL[email protected]
84
11. FUNCTION
A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code.
Functions serve two purposes, They allow a programmer to say: ‘this piece of code does a specific job which stands by itself and should not be mixed up with anything else',Second they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.
A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value.
You already have seen couple of built-in functions like printf() those are built in function; Similar way you can define your own functions in C language, and those were User Defined Functions.
Consider the following chunk of code
int total = 10;
printf("Hello World"); total = total + l;
To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single compound statement and write the name that you want to give it in front of the brackets:
int Demo() { int total = 10; printf("Hello World"); total = total + l; }