You are on page 1of 21

//Course: CSC191

//Project: P0118a
//Date: Jan. 18, 2018
//Author:
//Purpose:

class Point {
double x, y;

Point(double x1, double y1) {


x = x1;
y = y1;
}

void setPoint(double x1, double y1) {


x = x1;
y = y1;
}
}

class Line {
Point p1, p2;

Line(Point p11, Point p22) {


p1 = p11;
p2 = p22;
}

Line(double p1x, double p1y, double p2x, double p2y) {


//p1.x = p1x;
//p1.y = p1y;
//p2.x = p2x;
//p2.y = p2y;
p1 = new Point(p1x, p1y);
p2 = new Point(p2x, p2y);
}

void setLine(double p1x, double p1y, double p2x, double p2y) {


p1.x = p1x;
p1.y = p1y;
p2.x = p2x;
p2.y = p2y;
}
}

class LineSeg {
Point p1, p2;

LineSeg(Point p11, Point p22) {


p1 = p11;
p2 = p22;
}

LineSeg(double p1x, double p1y, double p2x, double p2y) {


//p1.x = p1x;
//p1.y = p1y;
//p2.x = p2x;
//p2.y = p2y;
p1 = new Point(p1x, p1y);
p2 = new Point(p2x, p2y);
}

void setLineSeg(double p1x, double p1y, double p2x, double p2y) {


p1.x = p1x;
p1.y = p1y;
p2.x = p2x;
p2.y = p2y;
}
}

class Circle {

class Rectangle {

class Triangle {

public class P0118a {


//point to point
static boolean pointPoint(Point p1, Point p2) {
return (p1.x == p2.x) && (p1.y == p2.y);
}

//given 3 points p1, p2, p3,


//decide if you need left, right, or straight at p2 to reach p3.
//return 1(left), 0(stright), -1(right)
static int ccw(Point p1, Point p2, Point p3) {
//find the area of triangle made from p1, p2, p3
//if area = 0, staight
//if area > 0, left turn
//if area < 0, right turn

//p2.x - p1.x, p2.y - p1.y


//p3.x - p1.x, p3.y - p1.y
//area = (p2.x - p1.x)*(p3.y - p1.y) - (p3.x - p1.x)*(p2.y - p1.y)

return (int)Math.signum((p2.x - p1.x)*(p3.y - p1.y) - (p3.x - p1.x)*(p2.y -


p1.y));
}

static void lineLine(Line l1, Line l2) {


//s1 = (l1.p2.y-l1.p1.y)/(l1.p2.x-l1.p1.x)
//s2 = (l2.p2.y-l2.p1.y)/(l2.p2.x-l2.p1.x)
//-1/s2 = -(l2.p2.x-l2.p1.x)/(l2.p2.y-l2.p1.y)
if ((l1.p2.y-l1.p1.y)*(l2.p2.x-l2.p1.x) == (l2.p2.y-l2.p1.y)*(l1.p2.x-
l1.p1.x))
if (ccw(l1.p1, l1.p2, l2.p1) == 0)
System.out.println("overlapping");
else
System.out.println("parallel");
else
if ((l1.p2.y-l1.p1.y)*(l2.p2.y-l2.p1.y) == -(l1.p2.x-l1.p1.x)*(l2.p2.x-
l2.p1.x))
System.out.println("perpendicular");
else
System.out.println("intersecting");
}

static boolean pointLineSeg(LineSeg l, Point p) {


if (ccw(l.p1, l.p2, p) != 0)
return false;
return (p.x >= Math.min(l.p1.x, l.p2.x) && p.x <= Math.max(l.p1.x, l.p2.x)
&&
p.y >= Math.min(l.p1.y, l.p2.y) && p.y <= Math.max(l.p1.y,
l.p2.y));
}

static boolean lineSegLineSeg(LineSeg l1, LineSeg l2) {


int p11 = ccw(l1.p1, l1.p2, l2.p1);
int p12 = ccw(l1.p1, l1.p2, l2.p2);
int p21 = ccw(l2.p1, l2.p2, l1.p1);
int p22 = ccw(l2.p1, l2.p2, l1.p2);

if (p11 == 0 && p12 == 0 && p21 == 0 && p22 == 0 &&


!pointLineSeg(l1, l2.p1) && !pointLineSeg(l1, l2.p2))
return false;

return (p11*p12 <= 0) && (p21*p22 <= 0);


}

static boolean pointTriangle(Point p1, Point p2, Point p3, Point p) {


int n12 = ccw(p1, p2, p);
int n23 = ccw(p2, p3, p);
int n31 = ccw(p3, p1, p);

if (n12*n23*n31 == 0)
return true;
return (n12 == n23 && n23 == n31);
}

public static void main(String[] args) {


System.out.println(ccw(new Point(1,1), new Point(2,2), new Point(3,3)));
System.out.println(ccw(new Point(1,1), new Point(2,2), new Point(3,4)));
System.out.println(ccw(new Point(1,1), new Point(2,2), new Point(3,2)));
}
}

//Course: CSC191
//Project: P0125a
//Date: Jan. 25, 2018
//Author:
//Purpose:

import java.util.Scanner;
import java.util.Random;
public class P0125a {
static void loopCount(int n) {
int cnt = 0;
for (int i = 1; i <= n; i++)
cnt++;
System.out.println(cnt); //100
cnt = 0;
for (int i = 1; i <= n; i*=2)
cnt++;
System.out.println(cnt); //7
//i = 1, 2, 4, 8, 16, 32, 64

cnt = 0;
for (int i = 2; i <= n*n; i*=i)
cnt++;
System.out.println(cnt); //4
//i = 2, 4, 16, 256

cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cnt++;
System.out.println(cnt); //10000

cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
cnt++;
System.out.println(cnt); //10000
//i j reps
//1 1 - 1 1
//2 1 - 2 2
//3 1 - 3 3
//
//100 1 - 100 100

cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j*=2)
cnt++;
System.out.println(cnt); //10000
//i j reps
//1 1 1
//2 1, 2 2
//3 1, 2 2
//4 1,2,4 3
//5 1,2,4 3
//6
//7
//8 1,2,4,8 4
//100 1 - 100 100

cnt = 0;
for (int i = 1; i <= n; i*=2)
for (int j = 1; j <= i; j++)
cnt++;
System.out.println(cnt); //10000
//i j reps
//1 1 1
//2 1, 2 2
//4 1-4 4
//8 1-8 8
//16 1-16 16
//32 32
//64 64
}

//Fibonacci numbers
//1,1,2,3,5,8,13,21,34,50,...
static int getFibo(int n){
int f1 = 1, f2 = 1, f3 = 1;
for (int i = 3; i <= n; i++) {
f3 = f1+f2;
f1 = f2;
f2 = f3;
}
return f3;
}

//given n, test if n is a Fibo


static boolean isFibo(int n) {
int f1 = 1, f2 = 1, f3 = 1;
while (f3 < n) {
f3 = f1+f2;
f1 = f2;
f2 = f3;
}
return (f3 == n);
}

//given n, find the largest Fibo less than n


//if n = 60, return 55. If n = 55, return 34.
static int getFiboLargest(int n) { //n > 1
int f1 = 1, f2 = 1, f3 = 1;
while (f3 < n) {
f3 = f1+f2;
f1 = f2;
f2 = f3;
}
return f1;
}

//check if n is a power of 2
static boolean isPow(int n) {
while (n > 1) {
if (n%2 == 1)
return false;
n /= 2;
}
return true;
}

//given n, find the smallest 2's pow larger than or equal to n


//if n = 100, return 128. if n = 128, return 128
static int getPowSmallest(int n) {
int p = 1;
while (p < n)
p *= 2;
return p;
}

//check if n is a prime
static boolean isPrime(int n) {
int sq = (int)Math.sqrt(n);
for (int p = 2; p <= sq; p++)
if (n%p == 0)
return false;

return true;
}

//check if n is a prime
static boolean isPalin(String s) {
int n = s.length();
for (int i = 0; i < n/2; i++)
if (s.charAt(i) != s.charAt(n-1-i))
return false;

return true;
}

//remove all duplicates from string s


static String removeDup(String s) {
String r = "";
for (int i = 0; i < s.length(); i++)
if (r.indexOf(s.charAt(i)) == -1)
r += s.charAt(i);
return r;
}

//decimal to binary
static String dToB(int n) {
String r = "";
while (n > 0) {
r = n%2 + r; //r += n%2 won't work
n /= 2;
}
return r;
}

//binary to decimal
static int bToD(String b) {
int r = 0;
for (int i = 0; i < b.length(); i++)
r = r*2 + (b.charAt(i)-'0');
return r;
}

//decimal(10) binary (2) octal(8) hexadecimal(16)


//1 1 1 1
//2 10 2 2
//7 111 7 7
//8 1000 10 8
//10 1010 12 A
//15 1111 17 F
//16 10000 20 10
//decimal to octal
static String dToO(int n) {
String r = "";
while (n > 0) {
r = n%8 + r;
n /= 8;
}
return r;
}

//octal to decimal
static int oToD(String b) {
int r = 0;
for (int i = 0; i < b.length(); i++)
r = r*8 + (b.charAt(i)-'0');
return r;
}

//decimal to Hexa
static String dToH(int n) {
String r = "";
while (n > 0) {
//10 -> A
//11 -> B
//12 -> C
//13 -> D
//14 -> E
//15 -> F
int t = n%16;
if (t < 10)
r = t + r;
else
r = (char)(t-10+'A') + r;
n /= 16;
}
return r;
}

//factorize n, if n = 600, beak into 2*2*2*3*5*5


//factorize n, if n = 600, beak into 2^3*3*5^2
static String fact1(int n) {
String r = "";
int f = 2;
while (n > 1)
if (n%f == 0) {
r += f+"*";
n /= f;
}
else
f++;
return r.substring(0, r.length()-1);
}

static String fact2(int n) {


String r = "";
int f = 2;
int cnt = 0;
while (n > 1) {
cnt = 0;
while (n%f == 0) {
cnt++;
n /= f;
}
if (cnt == 1)
r += f+"*";
else if (cnt >= 2)
r += f+"^"+cnt+"*";
f++;
}

return r.substring(0, r.length()-1);


}

//Euclid's GCD - gcd(105, 60) = gcd(60, 45) = gcd(45, 15) = gcd(15, 0)


static int getGcd(int n1, int n2) {
while (n2 > 0) {
int r = n1%n2;
n1 = n2;
n2 = r;
}
return n1;
}

//add two hexadecimal strings


static String addH(String h1, String h2) {
String h3 = "";
int i1 = h1.length()-1, i2 = h2.length()-1;
int c = 0;
while (i1 >= 0 && i2 >= 0) {
int n1 = (h1.charAt(i1) <= '9'?
h1.charAt(i1)-'0':h1.charAt(i1)-'A'+10);//ternary operator
int n2 = (h2.charAt(i2) <= '9'?h2.charAt(i2)-'0':h2.charAt(i2)-'A'+10);
int sum = n1+n2+c;
c = sum/16;

char nc = (sum%16 <= 9? (char)(sum%16+'0') : (char)(sum%16-10+'A'));


h3 = nc + h3;
i1--;
i2--;
}
while (i1 >= 0) {
int n1 = (h1.charAt(i1) <= '9'?
h1.charAt(i1)-'0':h1.charAt(i1)-'A'+10);//ternary operator
int sum = n1+c;
c = sum/16;
char nc = (sum%16 <= 9? (char)(sum%16+'0') : (char)(sum%16-10+'A'));
h3 = nc + h3;
i1--;
}
while (i2 >= 0) {
int n2 = (h2.charAt(i2) <= '9'?
h2.charAt(i2)-'0':h2.charAt(i2)-'A'+10);//ternary operator
int sum = n2+c;
c = sum/16;
char nc = (sum%16 <= 9? (char)(sum%16+'0') : (char)(sum%16-10+'A'));
h3 = nc + h3;
i2--;
}

if (c > 0)
h3 = c+h3;
return h3;
}

//multiply two hexadecimal strings


static String mulH(String h1, String h2) {
String h3 = "";
int c = 0;
String pad = "";
for (int i2 = h2.length()-1; i2 >= 0; i2--) {
String p = "";
int n2 = (h2.charAt(i2) <= '9'?h2.charAt(i2)-'0':h2.charAt(i2)-'A'+10);
for (int i1 = h1.length()-1; i1 >= 0; i1--) {
int n1 = (h1.charAt(i1) <= '9'?
h1.charAt(i1)-'0':h1.charAt(i1)-'A'+10);
int sum = n1*n2+c;
c = sum/16;
char nc = (sum%16 <= 9? (char)(sum%16+'0') : (char)(sum%16-
10+'A'));
p = nc + p;
}
if (c > 0)
p = (c <= 9? (char)(c+'0') : (char)(c-10+'A')) + p;
//System.out.println(p);
h3 = addH(h3, p+pad);
pad += "0";
}
return h3;
}

//run-length coding
//if s = "abbbbbbbbccd", r = "ab8ccd"
static String compress(String s) {
String r= "";
char ch = s.charAt(0);
int cnt = 1;
for (int i = 1; i < s.length(); i++)
if (s.charAt(i) == ch)
cnt++;
else {
if (cnt == 1)
r += ch;
else if (cnt == 2)
r = r + ch + ch;
else
r = r + ch + cnt;
ch = s.charAt(i);
cnt = 1;
}
if (cnt == 1) //last char
r += ch;
else if (cnt == 2)
r = r + ch + ch;
else
r = r + ch + cnt;
return r;
}

static String decompress(String s) {


String r = "";
String n = "";
for (int i = s.length()-1; i >= 0; i--) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9')
n = s.charAt(i)+n;
else
if (n.length() == 0)
r = s.charAt(i)+r;
else {
for (int j = 1; j <= Integer.parseInt(n); j++)
r = s.charAt(i)+r;
n = "";
}
}
return r;
}

//encode
//p = "abc xyz" and key = "bbc" changed to
//c = "abc xyz"
//k = "bbcbbcb"
//c = "cdfbzaa"

String encode(String s, String k) { //no blank in key


String c = "";
int ind;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ')
ind = 0;
else
ind = s.charAt(i)-'a'+1;

ind = (ind + (k.charAt(i%k.length()))-'a'+1)%27;


if (ind == 0)
c += ' ';
else
c += (char)(ind +'a'-1);
}
return c;
}

String decode(String s, String k) { //no blank in key


String c = "";
int ind;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ')
ind = 0;
else
ind = s.charAt(i)-'a'+1;

ind = (ind + (27-(k.charAt(i%k.length()))-'a'+1))%27;


if (ind == 0)
c += ' ';
else
c += (char)(ind +'a'-1);
}
return c;
}

//evalAS
//123-45+6+78-31
//1-2-3
static int evalAS(String e) {
int r = 0, n = 0;
char op = '+';
for (int i = 0; i < e.length(); i++) {
if (e.charAt(i) >= '0' && e.charAt(i) <= '9')
n = n*10 + (e.charAt(i)-'0');
else {
if (op == '+')
r += n;
else
r -= n;
n = 0;
op = e.charAt(i);
}
}
if (op == '+')
r += n;
else
r -= n;
return r;
}

//evalAS
//123/45*6*78/31
//12/2/3
static int evalMD(String e) {
int r = 1, n = 0;
char op = '*';
for (int i = 0; i < e.length(); i++) {
if (e.charAt(i) >= '0' && e.charAt(i) <= '9')
n = n*10 + (e.charAt(i)-'0');
else {
if (op == '*')
r *= n;
else
r /= n;
n = 0;
op = e.charAt(i);
}
}
if (op == '*')
r *= n;
else
r /= n;
return r;
}

//+, -, *, / but no (, )
//12*4/10-5*60+25/6-20
static int evalE(String e) {
int r = 0;
String n = "";
char op = '+';
for (int i = 0; i < e.length(); i++) {
if (e.charAt(i) == '+' || e.charAt(i) == '-') {
if (op == '+')
r += evalMD(n);
else
r -= evalMD(n);
n = "";
op = e.charAt(i);
}
else
n += e.charAt(i);
}
if (op == '+')
r += evalMD(n);
else
r -= evalMD(n);
return r;
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
Random rnd = new Random();

/*
//loopCount(100);
System.out.println(fact1(600));
System.out.println(fact2(600));

System.out.println(getGcd(105, 60));
System.out.println(getGcd(60, 105));

System.out.println(dToH(15));
System.out.println(dToH(16));
*/
//System.out.println(addH("567", "99"));
//System.out.println(addH("", "12"));
//System.out.println(addH("FFFF", "FF"));
System.out.println(mulH("567", "99"));
System.out.println(mulH("1AF", "1"));
System.out.println(mulH("FFFF", "FF"));
}
}

/*
course: CSC190
project: P0213a
date: 2/13/18
author: (your name)
purpose: arrays
*/

import java.util.Scanner;
import java.util.Random;
class MyArray {
final int MAXSIZE = 10;
int a[];
int n;

MyArray() {
a = new int[MAXSIZE];
n = 0;
}

void readA() {
Scanner in = new Scanner(System.in);
n = 0;
System.out.print("Enter a positive integer(-1 to stop): ");
int key = in.nextInt();
while (key > 0) {
a[n++] = key;
if (n >= MAXSIZE) {
System.out.println("array full...");
break;
}
System.out.print("Enter a positive integer(-1 to stop): ");
key = in.nextInt();
}
}

void generateA() {
Random rnd = new Random();

n = rnd.nextInt(MAXSIZE)+1;
for (int i = 0; i < n; i++)
a[i] = rnd.nextInt(100);
}

void printA() {
for (int i = 0; i < n; i++)
System.out.print(a[i]+" ");
System.out.println();
}

int maxA() {
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max)
max = a[i];

return max;
}
//how to find the 2nd largest number

void reverseA() {
//a[0], a[n-1]
//a[1], a[n-2]
//.
//a[n/2-1], a[ ]
for (int i = 0; i < n/2; i++) {
int t = a[i];
a[i] = a[n-1-i];
a[n-1-i] = t;
}
}

void rotateA() {
Scanner in = new Scanner(System.in);

String res;

do {
System.out.println("l. to left: ");
System.out.println("r. to right: ");

System.out.println("m. back to main: ");


System.out.println("select: ");

res = in.next();

switch (res.charAt(0)) {
case 'l': case 'L':
rotateL();
break;
case 'r': case 'R':
rotateR();
break;

case 'm': case 'M':


break;
default:
System.out.println("invalid? Try it again");

}
} while (res.toLowerCase().charAt(0) != 'm');
}

//rotate left
//a = 1,2,3,4,5
//a = 2,3,4,5,1 after rotate left
void rotateL() {
int t = a[0];
for (int i = 1; i < n; i++)
a[i-1] = a[i];
a[n-1] = t;
}

//rotate right
//a = 1,2,3,4,5
//a = 5,1,2,3,4 after rotate right
void rotateR() {
int t = a[n-1];
for (int i = n-2; i <= 0; i--)
a[i+1] = a[i];
a[0] = t;
}

void bubble() {
for (int stage = n-2; stage >= 0; stage--) {
boolean isSwap = false;
for (int i = 0; i <= stage; i++)
if (a[i] > a[i+1]) {
isSwap = true;
int t = a[i];
a[i] = a[i+1];
a[i+1] = t;
}
if (!isSwap)
break;
}
}

//2 5 4 1 6 3
//2 5 4 1 6 3 after inserting 5
//2 4 5 1 6 3 after inserting 4
//1 2 4 5 6 3 after inserting 1
//1 2 4 5 6 3 after inserting 6
//1 2 3 4 5 6 after inserting 3

//1 2 3 4 5 6 best case

//6 5 4 3 2 1 worst case


//5 6 4 3 2 1
void insertion() {
for (int i = 1; i < n; i++) {
int t = a[i];
int j;
for (j = i-1; j >= 0; j--)
if (a[j] > t)
a[j+1] = a[j];
else
break;
a[j+1] = t;
}
}

//2 5 4 1 6 3
//2 5 4 1 3 6
//2 3 4 1 5 6
//2 3 1 4 5 6
//2 1 3 4 5 6
//1 2 3 4 5 6
void selection() {
for (int stage = n-1; stage >= 1; stage--) {
int maxInd = 0;
for (int i = 1; i <= stage; i++)
if (a[i] > a[maxInd])
maxInd = i;
//swap a[maxInd] and a[n-1]
int t = a[maxInd];
a[maxInd] = a[stage];
a[stage] = t;
}
}

boolean isSorted() {
for (int i = 0; i <= n-2; i++)
if (a[i] > a[i+1])
return false;
return true;
}

void sortA() {
Scanner in = new Scanner(System.in);

String res;

do {
System.out.println("b. bubble sort: ");
System.out.println("i. insertion sort: ");
System.out.println("s. selection sort: ");

System.out.println("m. back to main: ");


System.out.println("select: ");

res = in.next();

switch (res.charAt(0)) {
case 'b': case 'B':
bubble();
break;
case 'i': case 'I':
insertion();
break;
case 's': case 'S':
//selection();
break;

case 'm': case 'M':


break;
default:
System.out.println("invalid? Try it again");

}
} while (res.toLowerCase().charAt(0) != 'm');
}

int sequential(int key) {


for (int i = 0; i < n; i++)
if (a[i] == key)
return i;

return -1;
}

//how to find all locations where key can be found?

//array must be pre-sorted


//1,3,3,5,6,8,9 with key = 6
//pull out middle number 5 in range (0, 6) and compare with 6
//since 6 > 5, you reduce the range to (4, 6)
//pull out the middle number 8 in range (4, 6) and compare with 6
//since 6 < 8, you reduce the range to (4, 4)
//pull out the middle number 6 in range (4, 4) and compare with 6, matched
//1,3,3,5,6,8,9 with key = 7
//(0,6) initially, (4, 6) after 1st loop, (4, 4) after 2nd round, (5, 4) no key
//in NCAA game, how many rounds with 512 teams?
//2^X = 512, x = 9, you need 10 rounds, so, the number of rounds needed is
//log2(n) + 1
int binary(int key) {
int left = 0, right = n-1;
while (left <= right) {
int middle = (left+right)/2;
if (a[middle] == key)
return middle;
if (a[middle] < key)
left = middle+1;
else
right = middle -1;
}
return -1;
}
void searchA() {
Scanner in = new Scanner(System.in);

String res;

do {
System.out.println("s. sequential search: ");
System.out.println("b. binary search: ");

System.out.println("m. back to main: ");

System.out.println("select: ");

res = in.next();

switch (res.charAt(0)) {
case 's': case 'S':
System.out.print("Enter a key: ");
int key = in.nextInt();
int p = sequential(key);
if (p == -1)
System.out.println(key+" not found!");
else
System.out.println(key+" found at "+p);
break;
case 'b': case 'B':
if (!isSorted()) {
System.out.println("array not presorted!!!");
break;
}
System.out.print("Enter a key: ");
key = in.nextInt();
p = binary(key);
if (p == -1)
System.out.println(key+" not found!");
else
System.out.println(key+" found at "+p);
break;

case 'm': case 'M':


break;
default:
System.out.println("invalid? Try it again");

}
} while (res.toLowerCase().charAt(0) != 'm');
}
}

public class P0213a {


public static void main(String[] args) {
Scanner in = new Scanner(System.in);
MyArray ma = new MyArray();

int n;
String res;

do {
System.out.println("1. read array: ");
System.out.println("2. generate array: ");
System.out.println("3. print array: ");
System.out.println("4. max: ");
System.out.println("5. reverse array: ");
System.out.println("6. rotate array: ");
System.out.println("7. sort array: ");
System.out.println("8. search array: ");

System.out.println("q. stop: ");

System.out.println("select: ");

res = in.next();

switch (res.charAt(0)) {
case '1':
ma.readA();
break;
case '2':
ma.generateA();
break;
case '3':
ma.printA();
break;
case '4':
System.out.println("max = "+ma.maxA());
break;
case '5':
ma.reverseA();
break;
case '6':
ma.rotateA();
break;
case '7':
ma.sortA();
break;
case '8':
ma.searchA();
break;

case 'q': case 'Q':


System.out.println("Thank you for using my program");
break;
default:
System.out.println("invalid? Try it again");

}
} while (res.toLowerCase().charAt(0) != 'q');
}
}

/*
course: CSC191
project: P0213b
date: 2/13/18
author: (your name)
purpose: 2d arrays
*/
import java.util.Scanner;
import java.util.Random;
class My2DArray {
final int MAXROW = 10;
final int MAXCOL = 10;
int a[][];
int nR, nC;

My2DArray() {
a = new int[MAXROW][MAXCOL];
nR = nC = 0;
}

void readA() {
Scanner in = new Scanner(System.in);
System.out.print("number of rows: ");
nR = in.nextInt();
System.out.print("number of cols: ");
nC = in.nextInt();

for (int r = 0; r < nR; r++)


for (int c = 0; c < nC; c++) {
System.out.print("Enter a number for ["+r+"]["+c+"]: ");
a[r][c] = in.nextInt();

void generateA() {
Random rnd = new Random();

nR = rnd.nextInt(MAXROW)+1;
nC = rnd.nextInt(MAXCOL)+1;
for (int r = 0; r < nR; r++)
for (int c = 0; c < nC; c++)
a[r][c] = rnd.nextInt(100);

void printA() {
for (int r = 0; r < nR; r++) {
for (int c = 0; c < nC; c++)
System.out.printf("%4d", a[r][c]);
System.out.println();
}
}

//a
//1 2 3
//4 5 6
//7 8 9

//after transpose assuming nR = nC


//1 4 7
//2 5 8
//3 6 9
//rotate rowwise
//4 5 6
//7 8 9
//1 2 3

//matrix multiplication

//magic square

void rotateR() {
for (int c = 0; c < nC; c++) {
int t = a[0][c];
for (int r = 1; r < nR; r++)
a[r-1][c] = a[r][c];
a[nR-1][c] = t;
}
}

void transpose() {
if (nR != nC)
return;
for (int r = 0; r < nR; r++)
for (int c = r+1; c < nC; c++) {
//switch a[r][c] with a[c][r]
int t = a[r][c];
a[r][c] = a[c][r];
a[c][r] = t;
}
}

// 1 2 3 4 1 2 50 60
// 5 6 7 8 * 3 4 = ?? ??
// 9 10 11 12 5 6 ?? ??
// 7 8

My2DArray multiply(My2DArray m1, My2DArray m2) {


if (m1.nC != m2.nR)
return null;
My2DArray m3 = new My2DArray();
m3.nR = m1.nR;
m3.nC = m2.nC;

//m3.a[0][0] = m1.a[0][0]*m2.a[0][0] + m1.a[0][1]*m2.a[1][0] +


// m1.a[0][2]*m2.a[2][0] + m1.a[0][3]*m2.a[3][0];
//m3.a[0][1] = m1.a[0][0]*m2.a[0][1] + m1.a[0][1]*m2.a[1][1] +
// m1.a[0][2]*m2.a[2][1] + m1.a[0][3]*m2.a[3][1];
for (int r = 0; r < m3.nR; r++)
for (int c = 0; c < m3.nC; c++) {
m3.a[r][c] = 0;
for (int i = 0; i < m1.nC; i++)
m3.a[r][c] += m1.a[r][i]*m2.a[i][c];

}
return m3;
}

//magic square nR = nC and odd


//nR = 3
//put 1,2,3,...9
// 8 1 6
// 3 5 7
// 4 9 2

void getMagicSquare(int n) {
if (n%2 == 0)
return;
nR = nC = n;
int r = 0, c = n/2;
a[r][c] = 1;
for (int i = 2; i <= n*n; i++) {

//steps for getting the next spot

a[r][c] = i;
}
}

public class P0213b {


public static void main(String[] args) {
My2DArray myA = new My2DArray();
My2DArray myA1 = new My2DArray();

myA.readA();
myA.printA();
myA1.readA();
myA1.printA();
//myA.transpose();
myA.multiply(myA, myA1).printA();

//myA.printA();

}
}

You might also like