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

3

The document contains Java code for several programs involving arrays and matrices. It defines classes and methods to input, process, and output data for 1D and 2D arrays. The programs demonstrate summing arrays, reversing arrays, matrix addition, row/column summing, matrix multiplication, and transposing matrices.

Uploaded by

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

3

The document contains Java code for several programs involving arrays and matrices. It defines classes and methods to input, process, and output data for 1D and 2D arrays. The programs demonstrate summing arrays, reversing arrays, matrix addition, row/column summing, matrix multiplication, and transposing matrices.

Uploaded by

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

/**********************************************

NAME=AKANKSHA SHARMA

CLASS= 2ND YEAR SEC A

EN NO. = 220362

PRG NO. / NAME= 14/ SUM OF ALL ELEMENTS OF 1-D ARRAY

***********************************************/

import java.util.*;
class Arrays{
int a[];
int sum;
Arrays(){
a = new int[6];
sum = 0;
}
void input(){
System.out.println("Enter array elements");
Scanner sc = new Scanner(System.in);
for(int i=0;i<6;i++){
a[i] = sc.nextInt();
}
System.out.println();
}
void proc(){
for(int i=0;i<6;i++){
sum = sum + a[i];
}
}
void output(){
System.out.println(sum);
}
}
public class sum1dArray {
public static void main(String args[]){
Arrays a1 = new Arrays();
a1.input();
a1.proc();
a1.output();
}
}
/*******************************************************

NAME= AKANKSHA SHARMA

CLASS= 2ND YEAR SEC A

EN NO. = 220362

PRG NO. / NAME= 15/ REVERSE OF 1-D ARRAY USING SECOND ARRAY

******************************************************/

import java.util.*;
class Reverse{
int a[] = new int[5];
int b[] = new int[5];
int j = a.length;

void inpt(){
System.out.println("Enter array elements");
Scanner sc = new Scanner(System.in);
for(int i=0;i<a.length;i++){
a[i] = sc.nextInt();
}
}
void rev(){
for(int i=0;i<a.length;i++){
b[j-1] = a[i];
j=j-1;
}
}
void outpt(){
for(int i=0;i<a.length;i++){
System.out.print(b[i] + " ");
}

}
public class reverse1dArray {
public static void main(String[] args) {
Reverse r = new Reverse();
r.inpt();
r.rev();
r.outpt();
}

}
/*******************************************************

NAME=AKANKSHA SHARMA

CLASS= 2ND YEAR SEC A

EN NO. = 220362

PRG NO. / NAME= 16/ SUM OF TWO MATRICES OF SIZE 3X3

******************************************************/

import java.util.*;
class Matrix{
int a[][] = new int[3][3];
int b[][] = new int[3][3];
int c[][] = new int[3][3];

void input(){
System.out.println("Enter first array");
Scanner sc = new Scanner(System.in);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[i][j] = sc.nextInt();
}}
System.out.println("Enter second array");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
b[i][j] = sc.nextInt();}}
System.out.println();}

void sum(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j] = a[i][j]+b[i][j];}}}

void output(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(c[i][j] + " ");}
System.out.println();}}}

public class MatrixSum {


public static void main(String[] args) {
Matrix m1 = new Matrix();
m1.input();
m1.sum();
m1.output();
}
}
/*******************************************************

NAME=AKANKSHA SHARMA

CLASS= 2ND YEAR SEC A

EN NO. = 220362

PRG NO. / NAME= 17/ CLASS FOR 2-D ARRAY WITH FOLLOWING METHODS
1. input method
2. do the sum of each row and print it
3. do the sum of each column and print it
4. do the sum of diagonal elements.

******************************************************/

class Sums{
int a[][] = new int[3][3];
int rows = a.length;
int cols = a[0].length;
void input(){
System.out.println("Enter array elements");
Scanner sc = new Scanner(System.in);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[i][j] = sc.nextInt();
}
}
}
void rowsum(){
for(int i=0;i<rows;i++){
int sumrows = 0;
for(int j=0;j<cols;j++){
sumrows = sumrows + a[i][j];
}
System.out.println("Sum of " + (i+1) +" row: " + sumrows);
}
System.out.println();
}
void colsum(){
for(int j=0;j<cols;j++){
int sumcols = 0;
for(int i=0;i<rows;i++){
sumcols = sumcols + a[i][j];
}
System.out.println("Sum of " + (j+1) +" column: " + sumcols);
}
System.out.println();
}
void diagsum(){
int sum = 0;
for (int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(i == j){
sum = sum + a[i][j];
}}}
System.out.println("Sum of diagonal elements is: " + sum);
}
}
public class Array2dSum {
public static void main(String[] args) {
Sums s1 = new Sums();
s1.input();
s1.rowsum();
s1.colsum();
s1.diagsum();
}
}
/*******************************************************

NAME=AKANKSHA SHARMA

CLASS= 2ND YEAR SEC A

EN NO. = 220362

PRG NO. / NAME= 18/ MATRIX MULTIPLICATION

*******************************************************/

import java.util.*;
class Mul{
int a[][] = new int [3][3];
int b[][] = new int[3][3];
int c[][] = new int[3][3];
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter first array Elements");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter second array Elements");
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
b[i][j] = sc.nextInt();}
}
}
void multiply(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j] = 0;
for(int k=0;k<3;k++){
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);}}
System.out.println();
}}
void output(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(c[i][j] + " ");}
System.out.println();}
}}
public class MatrixMul {
public static void main(String[] args) {
Mul m1 = new Mul();
m1.input();
m1.multiply();
m1.output();
}}
/*******************************************************

NAME=AKANKSHA SHARMA

CLASS= 2ND YEAR SEC A

EN NO. = 220362

PRG NO. / NAME= 19/ TRANSPOSE OF GIVEN MATRIX

******************************************************/
import java.util.*;
class Matrix{
int a[][] = new int[3][3];
int b[][] = new int[3][3];
void input(){
System.out.println("Enter array elements");
Scanner sc = new Scanner(System.in);
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[i][j] = sc.nextInt();
}
}
System.out.println();
}
void trans(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
b[i][j] = a[j][i];
}
}
}
void output(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}
public class Transpose {
public static void main(String[] args) {
Matrix m2 = new Matrix();
m2.input();
m2.trans();
m2.output();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy