javaarraysoperatorslcdgreatest-common-divisor

Array of operators


I have to make a program that takes input from a 2d array with n columns and 2 rows ex: fractions=(1 2 3 4) (5 6 7 8)

it also has to take in a 1d array of operators (ex: Operators = [ + * − ])

the code has to add, subtract, multiply, divide fractions in the array depending on the operators in the 1d array - ex: 1/5 + 2/6 * 3/7 -4/8

I got my code to input both of the arrays correctly but am having a difficult time figuring out how to get it to do the math. I have read that the answer incorporates least common multiple and greatest common divider so I also did a separate program for that but don't know how to combine the programs together. Has anyone seen a problem like this before and can give some advice? Thank you in advance.

import java.util.Scanner;
public class ProjectCS {
    public static void main (String[]args){
        Scanner s = new Scanner(System.in);


        //1D ARRAY OF OPERATORS 
        System.out.println("How many operators are you going to enter?");
        int length = s.nextInt();
        String operators[]=new String[length];

        System.out.println("Enter operators(+, -, *)");

        for (int counter=0; counter<length; counter++){
            operators[counter]=s.next();

            System.out.print(operators[counter]); 
            //1D ARRAY OF OPERATORS END 
        }


        //INPUT OF ROWS AND COLUMNS

        System.out.print("Enter number of rows in matrix:");

        int rows = s.nextInt();

        System.out.print("Enter number of columns in matrix:");

        int n = s.nextInt();

        int fractions[][] = new int[rows][n];

        System.out.println("Enter all the elements of matrix:");

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < n; j++) {
                fractions[i][j] = s.nextInt();
            }
        }   
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < n;j++){



               System.out.print (fractions[i][j]);

            }
            System.out.println("");

            //END OF INPUT OF ROWS AND COLUMNS


        }
    }


}
//LCM code and GCM code 

import java.util.Scanner;
public class LCM_GCD_METHOD {

    public static void printFace() {
        int a;
        int b;
        int LCM;
        int temp;
        int GCD;


        Scanner sc= new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();

        a=x;
        b=y;


        while(b!=0){
            temp = b;
            b = a % b;
            a = temp;
        }
        GCD = a;
        LCM = (x*y)/GCD;

        System.out.println("GCD = " + GCD);
        System.out.println("LCM = " + LCM);   
    }
    public static void main (String [] args) {
        printFace();
        return;
    }
}

Solution

  • Your numbers arrays are arrays of integers, but your operators array is an array of characters. The most straight forward way i think is to

    for(int i = 0 ; i < operators.length ; i++){
    if(operators[i] == '+'){
    //do addition
    }else if(operators[i] == '-'){
    //do subtraction
    //and more conditions for the remaining operators
    }
    }