New Member FAQ | Forums | Earn Revenue | Distance Education | Help Topics | Admissions 2013
Awards & Gifts
 
Login Login    Register      

ArticlesPractice TestsAsk ExpertsQuestion PapersJobsUniversitiesCollegesCoursesSchools

Active Members
TodayLast 7 Daysmore...

Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Articles/Knowledge Sharing » Computer & Technology

Java Program-Array Manipulation


Posted Date:     Category: Computer & Technology    
Author: Member Level: Diamond    Points: 2



 

ARRAY MANIPULATION


import java.util.*;
class matrix
{
int[][] m;
int r;
int c;
public matrix(int row,int column)
{
r=row;
c=column;
m=new int[row][column];
} //end of constructor with parameter
public void input()
{
System.out.println("Enter elements of matrix: ");
Scanner sc;
sc=new Scanner(System.in);
for(int i=0;i {
for(int j=0;j {
m[i][j]=sc.nextInt();
}
}
} //end of input
public void output()
{
for(int i=0;i {
for(int j=0;j {
System.out.print(m[i][j]+" ");
}
System.out.println(" ");
}
} //end of output
public matrix add(matrix a)
{
if(r==a.r)
{
if(c==a.c)
{
System.out.println("Addition is: ");
matrix temp;
temp=new matrix(r,c);
for(int i=0;i {
for(int j=0;j {
temp.m[i][j]=m[i][j]+a.m[i][j];
}
}
return temp;
}
else //column not equal
{
matrix temp;
temp=new matrix(0,0);
System.out.println("No. of rows & colunms must be same");
return temp;
}
} //end of inner if_else
else //row not equal
{
matrix temp;
temp=new matrix(0,0);
System.out.println("No. of rows & colunms must be same");
return temp;
} //end of outer if_else
} //end of add

public matrix subtract(matrix a)
{
if(r==a.r)
{
if(c==a.c)
{
System.out.println("Substraction is: ");
matrix temp;
temp=new matrix(r,c);
for(int i=0;i {
for(int j=0;j {
temp.m[i][j]=m[i][j]-a.m[i][j];
}
}
return temp;
}
else //column not equal
{
matrix temp;
temp=new matrix(0,0);
System.out.println("No. of rows & colunms must be same");
return temp;
}
} //end of inner if_else
else //row not equal
{
matrix temp;
temp=new matrix(0,0);
System.out.println("No. of rows & colunms must be same");
return temp;
} //end of outer if_else
} //end of subtract

public matrix multiply(matrix a)
{
if(c==a.r)
{
System.out.println("Multiplication is: ");
matrix temp;
temp=new matrix(r,a.c);
for(int i=0;i {
for(int j=0;j {
temp.m[i][j]=0;
for(int k=0;k {
temp.m[i][j]+=m[i][k]*a.m[k][j];
}
}
}
return temp;
}
else //row not equal
{
matrix temp;
temp=new matrix(0,0);
System.out.println("No. of colunms of first matrix must be equal to rows of second matrix");
return temp;
} //end of if_else
} //end of multiply

} //end of class matrix

public class matrixoperations
{
public static void main(String[] args)
{
Scanner sc;
sc=new Scanner(System.in);
int row,column;
System.out.print("Enter row size: ");
row=sc.nextInt();
System.out.print("Enter column size: ");
column=sc.nextInt();
matrix a;
a=new matrix(row,column);
a.input();
System.out.println("Matrix a is:");
a.output();
int r,c;
System.out.print("Enter row size: ");
r=sc.nextInt();
System.out.print("Enter column size: ");
c=sc.nextInt();
matrix b;
b=new matrix(r,c);
b.input();
System.out.println("Matrix b is:");
b.output();
int choice;
do
{
System.out.print("1:Addition 2:Substraction 3:Multiplication 0:Exit Enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
matrix d;
d=new matrix(r,c);
d=a.add(b);
d.output();
break;
case 2:
matrix e;
e=new matrix(r,c);
e=a.subtract(b);
e.output();
break;
case 3:
matrix f;
f=new matrix(r,a.c);
f=a.multiply(b);
f.output();
break;
case 0:
break;
default:
System.out.println("Invalid choice");
} //end of switch
}while(choice!=0); //end of do_while
} //end of main
} //end of class matrixoperations





Did you like this resource? Share it with your friends and show your love!





Responses to "Java Program-Array Manipulation"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Java Program-Matrix Manipulation
    Previous Resource: Java Program-For stack
    Return to Resources
    Post New Resource
    Category: Computer & Technology


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Java Program  .  

    Subscribe to Email
  • Get Jobs by Email
  • Forum posts by Email
  • Articles by Email
  • Online MembersM.K.Dan
    Adesola Adeyeye
    stylus
    Ramprasad
    Pritan Tiwari
    ratheesh
    Gayatri Ajay Upadhyay
    sanskar
    Divya
    moses dhilip kumar
    srishty
    More...


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    AdSense Revenue Sharing sites   Advertise   Talk to Tony John
    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.