PF A-03-B Amaan Ahmad

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

DECEMBER 26, 2022

PROGRAMMING FUNDAMENTALS
ASSIGNMENT 3-B

Amaan Ahmad
COMSATS UNIVERSITY ISLAMABAD
SP22/BCT/004
Q-1:
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
int r1,r2,c1,c2,i,j,k,sum;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows of matrix1: ");
r1 = sc.nextInt();
System.out.print("Enter number columns of matrix 1: ");
c1 = sc.nextInt();
System.out.print("Enter number of rows of matrix2: ");
r2 = sc.nextInt();
System.out.print("Enter number of columns of matrix 2: ");
c2 = sc.nextInt();
if(c1==r2) {
int matrix1[][] = new int[r1][c1];
int matrix2[][] = new int[r2][c2];
int product[][] = new int[r1][c2];
System.out.println("Enter the elements of matrix1");
for ( i= 0 ; i < r1 ; i++ ) {
for ( j= 0 ; j < c1 ;j++ )
matrix1[i][j] = sc.nextInt();
}
System.out.println("Enter the elements of matrix2");
for ( i= 0 ; i < r2 ; i++ ) {
for ( j= 0 ; j < c2 ;j++ )
matrix2[i][j] = sc.nextInt();
}
System.out.println("\n\noutput matrix:-");
for ( i= 0 ; i < r1 ; i++ )

for ( j= 0 ; j <c2;j++)
{
sum=0;
for ( k= 0 ; k <r2;k++ )
{
sum +=matrix1[i][k]*matrix2[k][j] ;
}
product[i][j]=sum;
}
for ( i= 0 ; i < r1; i++ )
{
for ( j=0 ; j < c2;j++ )
System.out.print(product[i][j]+" ");
System.out.println();
}
}
else
System.out.print("multiplication does not exist ");
}

Q-2:
Solution:
import java.util.Scanner;
public class Question_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter length of square matrix: ");
int r = sc.nextInt();
int[][] list = new int[r][r];
for (int i =0;i<r;i++) {
for (int j =0;j<r;j++) {
list[i][j] = (int) (Math.random()*2);
System.out.print(list[i][j]); }
System.out.println();
}
boolean flag;
int j =0;
boolean row=false, column=false, major =false, sub = false;
for(int i=0; i<r;i++) {
flag = true;
for (j =0;j<r-1;j++) {
if (list[i][j] != list[i][j+1])
flag = false;}
if (flag) {
System.out.println("All " + list[i][j] + "s on row " + i);
row = true;}
}
for(int i=0; i<r;i++) {
flag = true;
for (j =0;j<r-1;j++) {
if (list[j][i] != list[j+1][i])
flag = false; }
if (flag) {
System.out.println("All " + list[j][i] + "s on column " + i);
column = true;
}}
flag = true;
for (int i=0;i<r-1;i++) {
if (list[i][i]!= list[i+1][i+1])
flag = false;
}
if (flag) {
System.out.println("All " + list[0][0] + "s on major diagonal");
major = true;
}
flag = true;
for (int i=0;i<r-1;i++) {
if (list[i][r-i-1]!= list[i+1][r-i-1-1])
flag = false;}
if (flag) {
System.out.println("All " + list[0][r-1] + "s on sub-diagonal");
sub = true;
}
if (!column)
System.out.println("No same numbers on a column");
if (!row)
System.out.println("No same numbers on a row");
if (!major)
System.out.println("No same numbers on the major diagonal");
if (!sub)
System.out.println("No same numbers on the sub-diagonal");
}}
Q-3:
Solution:
public class Question_3 {
public static void main(String[] args){
char my_array[] = {'a','b','a','c','b','c','a','b','c'};
arrange_Aplhabetically(my_array);
}
public static void arrange_Aplhabetically(char my_array[]){
int count = 0;
int count_a = 0;
int count_b = 0;
int count_c = 0;
for (char element: my_array){
if (element == 'a'){
count_a++;
}
if (element == 'b'){
count_b++;}
if (element == 'c'){
count_c++;
}
count++;
}
char a_array[] = new char[count_a];
char b_array[] = new char[count_b];
char c_array[] = new char[count_c];
int i = 0;
for (char a: my_array){
if (a == 'a'){
a_array[i] = a;
i++;
}
}
i = 0;
for (char b: my_array){
if (b == 'b'){
b_array[i] = b;
i++; }
}
i = 0;
for (char c: my_array){
if (c == 'c'){
c_array[i] = c;
i++;
} }
int x=0;
if (count>=x){
for (char a: a_array){
my_array[x] = a;
x++;
}
for (char b: b_array){
my_array[x] = b;
x++;
}
for (char c: c_array){
my_array[x] = c;
x++;
} }
for (char z: my_array){
System.out.println(z);
} }
}

Q-4:
Solution:
import java.util.Scanner;
public class Question_4{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the array you want to create: ");
int size = input.nextInt();
int my_array[] = new int[size];
int x = 0;
for (int element: my_array){
System.out.print("Enter the digit you want to enter in the array: ");
int num = input.nextInt();
my_array[x] = num;
x++; }
System.out.print("Choose any of the following options:\n1) count\n2) Partition\n3)
Duplicate\n4) Circular\n5)Shift Circular");
int choose = input.nextInt();
if (choose==1) {
count(my_array);}
if (choose==2) {
partition(my_array, size);}
if (choose ==3) {
duplicate(my_array, size); }
if (choose == 4) {
circular(my_array, size);
}
if (choose==5) {
shiftCircular(my_array, size);}}
public static void count(int my_array[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter the number whom occurence you want to find in array: ");
int num = input.nextInt();
int count = 0;
for (int element: my_array){
if (element == num){
count++;}}
System.out.println("The number occurred "+count+" times."); }
public static void partition(int my_array[],int size){
int num = my_array[0];
int count_greater = 0;
int count_smaller = 0;
for (int element: my_array){
if (element>num){
count_greater++; }
if (element<num){
count_smaller++;}}
int greater[] = new int[count_greater];
int smaller[] = new int[count_smaller];
int x = 0;
for (int a: my_array){
if (a>num){
greater[x] = a;
x++;} }
x = 0;
for (int a: my_array){
if (a<num){
smaller[x] = a;
x++;}}
x=0;
if (size>=x){
for (int a: smaller){
my_array[x] = a;
x++; }
if (num==num){
my_array[x]=num;
x++; }
for (int b: greater){
my_array[x] = b;
x++}}
for (int z: my_array){
System.out.println(z);}}
public static void circular(int my_array[], int size){
int x = 0;
int y = 0;
int nw_array[] = new int[size];
int temp1 = my_array[0];
int temp2 = my_array[1];
int temp3 = my_array[size-1];
try{
for (int element: my_array){
if ((x+1)!=(size-1) & x !=(size-1) ){
nw_array[x] = my_array[y + 1] + my_array[y + 2];
y++;
x++;
}
if (x+1==size-1){
nw_array[x] = temp3+temp1;
x++;}
if (x==size-1){
nw_array[x] = temp1+temp2;}
}
}catch (Exception e){
System.out.println("A2");
}
for (int element: nw_array){
System.out.println(element);}
public static void shiftCircular(int my_array[],int size){
int temp1 = my_array[0];
int temp2 = my_array[1];
int x = 0;
for (int element: my_array){
if (x!=size-1 & x!=size-2){
my_array[x] = my_array[x+2];
x++; }
if (x == size-2){
my_array[x] = temp1;
x++;
}
if (x==size-1){
my_array[x] = temp2; }
for (int element: my_array){
System.out.println(element);}
public static void duplicate(int my_array[], int size){
//Array fr will store frequencies of element
int [] fr = new int [size];
int visited = -1;
for(int i = 0; i < size; i++){
int count = 1;
for(int j = i+1; j < size; j++){
if(my_array[i] == my_array[j]){
count++;
//To avoid counting same element again
fr[j] = visited;
}
}
if(fr[i] != visited)
fr[i] = count;
}
for(int i = 0; i < fr.length; i++){
if (i==0){
System.out.println("Element : Frequency");
}
if(fr[i] != visited){
System.out.println(my_array[i] + " : " + fr[i]);}}}

You might also like