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

Data Structures

Uploaded by

shwethasweeti37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data Structures

Uploaded by

shwethasweeti37
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Data structures

Name of the Assignment


1. operations on Program to perform operations on 1
dimensional arrays
2. Program to perform matrics

Group Members
1. Mohith Gowda
2. Vishwas S
3. Shwetha R
Submitted to
Ashwini Diwakar
Program to perform operations on 1
dimensional arrays
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int
a[10][10],b[10][10],r[10][10],r1,c1,r2,c2,i,flag=1,mu[10][10],j
,k;
//Matrix Addition
void matadd()
{
printf("\n Enter the rows and columns for matrix A:");
scanf("%d%d",&r1,&c1);
printf("\n Enter the rows and columns for matrix B:");
scanf("%d%d",&r2,&c2);
if(r1!=r2||c1!=c2)
{
printf("\n Matrix are not compertible for addition");
return;
}
printf("\n Enter the elements for matrix A:");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the elements for matrix B:");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
r[i][j]=a[i][j]+b[i][j];
printf("\n Matrix A\n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d",a[i][j]);
printf("\n");
}
printf("\n Matrix B\n ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d",b[i][j]);
printf("\n");
}
printf("\n Matrix sum \n ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d",r[i][j]);
printf("\n");
}
}
//Matrix Subtraction
void matsub()
{
printf("\n Enter the rows and columns for matrix A:");
scanf("%d%d",&r1,&c1);
printf("\n Enter the rows and columns for matrix B:");
scanf("%d%d",&r2,&c2);
if(r1!=r2||c1!=c2)
{
printf("\n Addition is not possible");
return;
}
printf("\n Enter the elements for matrix A:");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the elements for matrix B:");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
r[i][j]=a[i][j]-b[i][j];
printf("\n Matrix A\n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d",a[i][j]);
printf("\n");
}
printf("\n Matrix B\n ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d",b[i][j]);
printf("\n");
}
printf("\n Matrix substraction \n ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d",r[i][j]);
printf("\n");
}
}
void matmulti()
{
//Matrix multiplication
printf("\n Enter the rows and columns for matrix A:");
scanf("%d%d",&r1,&c1);
printf("\n Enter the rows and columns for matrix B:");
scanf("%d%d",&r2,&c2);
if(r1!=c2)
{
printf("\n Multiplication is not possible");
return;
}
printf("\n Enter the elements for matrix A");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the elements for matrix B");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
for(k=0;k<3;k++)
{
mu[i][j]+=a[i][k]*b[k][j];
}
}
printf("\n Matrix A\n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d",a[i][j]);
printf("\n");
}
printf("\n Matrix B\n ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d",b[i][j]);
printf("\n");
}
printf("\n Matrix multiplication \n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d",mu[i][j]);
printf("\n");
}
}
//Matrix Transpose
void mattrans()
{
printf("Enter rows and columns for matirx A:");
scanf("%d %d",&r1,&c1);
printf("Enter elements for matrix A:");
for( i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
for( i=0;i<r1;i++)
for( j=0;j<c1;j++)
r[j][i]=a[i][j];
printf(" \nMatrix transpose \n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d",r[i][j]);
printf("\n");
}
}
Output

One dimensional arrays


A one-dimensional array (or single dimension array) is a type of
linear array. Accessing its elements involves a single subscript
which can either represent a row or column index.
As an example consider the C declaration int
anArrayName[10]; which declares a one-dimensional array
of ten integers. Here, the array can store ten elements of
type int . This array has indices starting from zero through
nine. For example, the
expressions anArrayName[0] and anArrayName[9] are
the first and last elements respectively.

Addition
Addition (usually signified by the plus symbol + ) is one of the four
basic operations of arithmetic, the other three
being subtraction, multiplication and division.[2] The addition of two whole
numbers results in the total amount or sum of those values combined.
Besides counting items, addition can also be defined and executed without referring
to concrete objects, using abstractions called numbers instead, such
as integers, real numbers and complex numbers. Addition belongs to arithmetic, a
branch of mathematics. In algebra, another area of mathematics, addition can also
be performed on abstract objects such
as vectors, matrices, subspaces and subgroups.

Subtraction
Subtraction (which is signified by the minus sign − ) is one of the four arithmetic
operations along with addition, multiplication and division. Subtraction is an operation
that represents removal of objects from a collection. [1] For example, in the adjacent
picture, there are 5 − 2 peaches—meaning 5 peaches with 2 taken away, resulting in
a total of 3 peaches. Therefore, the difference of 5 and 2 is 3; that is, 5 − 2 = 3. While
primarily associated with natural numbers in arithmetic, subtraction can also
represent removing or decreasing physical and abstract quantities using different
kinds of objects including negative numbers, fractions, irrational numbers, vectors,
decimals, functions, and matrices.[2]

Multiplication
multiplication (often denoted by the cross symbol × , by the mid-line dot operator ⋅ ,
by juxtaposition, or, on computers, by an asterisk * ) is one of the
four elementary mathematical operations of arithmetic, with the other ones
being addition, subtraction, and division. The result of a multiplication operation is
called a product.
The multiplication of whole numbers may be thought of as repeated addition; that is,
the multiplication of two numbers is equivalent to adding as many copies of one of
them, the multiplicand, as the quantity of the other one, the multiplier; both
numbers can be referred to as factors.

Transpose
In linear algebra, the transpose of a matrix is an operator which flips a matrix over
its diagonal; that is, it switches the row and column indices of the matrix A by
producing another matrix, often denoted by AT (among other notations).[1]
The transpose of a matrix was introduced in 1858 by the British
mathematician Arthur Cayley.[2] In the case of a logical matrix representing a binary
relation R, the transpose corresponds to the converse relation RT.
Program to perform operations on
matrics
#include<stdio.h>
#include<conio.h>
void display(int a[],int n);
void main()
{
int a[10],choice,ele,pos,i,n;
clrscr();
printf("enter the size of array:");
scanf("%d",&n);
printf("Enter the elements of array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
do
{
printf("\n array operation menu:\n");
printf("1.insert at beginning\n");
printf("2.insert at middle\n");
printf("3.insert at end\n");
printf("4.delete at beginning\n");
printf("5.delete at middle\n");
printf("6.delete at end\n");
printf("7.exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter element to insert at the beginning:");
scanf("%d",&ele);
for(i=n;i>=0;i--)
{
a[i+1]=a[i];
}
a[0]=ele;
n++;
display(a,n);
break;
case 2:
printf("Enter element to insert:");
scanf("%d",&ele);
printf("Enter position to insert:");
scanf("%d",&pos);
if(pos<=0 || pos>n)
{
printf("Invalid position \n");
break;
}
else
{
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos]=ele;
n=n+1;
}
display(a,n);
break;
case 3:
printf("Enter element to be inserted at end:");
scanf("%d",&ele);
a[n]=ele;
n=n+1;
display(a,n);
break;
case 4:
if(n==0)
{
printf("Array is empty\n");
break;
}
for(i=0;i<n-1;i++)
{
a[i]=a[i+1];
}
n--;
display(a,n);
break;
case 5:
printf("Enter position to delete:");
scanf("%d",&pos);
if(n==0)
{
printf("array is empty\n");
break;
}
if(pos<=0 || pos>n-1)
{
printf("invalid position\n");
break;
}
else
{
for(i=pos-1;i<n-1;i++)
{
a[i]=a[i+1];
}
n--;
}
display(a,n);
break;
case 6:
n--;
display(a,n);
break;
case 7:
printf("Exit program\n");
exit();
default:
printf("Invalid choice!\n ");
}
}
{
while(1);
getch();
}
void display(int a[],int n)
{
int i;
printf("update array elements are:");
for(i=0;i<n;i++)

printf("\t%d",a[i]);
}
printf("\n");
}

Output
Matrix Operations Program

Overview
The provided program is written in C and performs various operations on
matrices. These operations include addition, subtraction, multiplication,
transpose, and identification of an identity matrix. The user is prompted to
choose an operation from a menu, and the program executes the chosen
operation accordingly. The program continues to operate until the user
chooses to exit.

Code Structure
The program begins with including necessary header files and defining
function prototypes.

Global variables are declared to store matrices and other parameters


required for operations.

The main () function controls the flow of the program.

Inside main (), a menu is displayed, and the user's choice is processed
using a switch-case statement.

Depending on the user's choice, corresponding functions (read (), read1(),


dis ()) are called to perform operations on matrices.

The program loops until the user chooses to exit.


Functions
void read (): Reads the sizes and elements of two matrices required for
addition, subtraction, or multiplication.

void read1(): Reads the size and elements of a single matrix required for
transpose or identity check.

void dis (): Displays the elements of the resultant matrix after an operation.

The main () function handles the menu and calls appropriate functions
based on user input.

Functionality
The program allows users to perform basic matrix operations such as
addition, subtraction, multiplication, transpose, and identity check.

It checks for valid matrix dimensions before performing addition,


subtraction, and multiplication.

For the identity check, it verifies if the provided matrix is an identity matrix
or not.

Improvements
Error handling can be improved to handle invalid user inputs.

Matrix multiplication logic needs correction as it currently performs


element-wise multiplication instead of matrix multiplication.

Additional functionalities like matrix inversion or determinant calculation


could be added.

Clearer user prompts and error messages would enhance usability.


Conclusion
Overall, the program provides a functional implementation of basic matrix
operations in C. With some improvements and additions, it could serve as
a useful tool for performing matrix calculations.

Thank you

You might also like