Amalshanth
Amalshanth

Reputation: 29

Basic math operations using array concept [java]

I,am creating a java code for doing basic math operations like Addition,Subtraction,Division,Multiplication.I build it using array concept.I want to perform multiple operations within an execution.And must follow the BODMAS rule.My code did run with no error.But did not get the output successfully. Here is the minimal reproducible example .With Division(/) operator:

package trial;
import java.util.Scanner;
public class trial {
    public static void main(String[] args) {
    float[] val=new float[10];
    char[] op=new char[10];
    float res;
    int lim;
        Scanner sc= new Scanner(System.in);
        System.out.println("how many operands");
        lim=sc.nextInt();
        for(int i=0;i<lim;i++) {
            if(i==lim-1) {
                System.out.println("enter a number");
                val[i]=sc.nextFloat();
            }
        else {
        System.out.println("enter a number");
        val[i]=sc.nextFloat();
        System.out.println("enter the operation(+,-,/,*)");
        op[i]=sc.next().charAt(0);
        }}
        for(int i=0;i<lim;i++) {
            System.out.print(val[i]+" "+op[i]+" ");
        }
        for(int i=0;i<lim-1;i++) {
            if(op[i]=='/') {
                res=val[i]/val[i+1];
                val=removeArrayF(val,i);
                val[i]=res;
                op=removeArrayC(op,i);
            }
        }
        System.out.println("res is"+val[0]);
        }
    static float[] removeArrayF(float[] array,int index){
        float[] r=new float[array.length-1];
        System.arraycopy(array, 0, r, 0, index);
        System.arraycopy(array, index+1, r, index,array.length-index-1);
        return r;
        }
    static char[] removeArrayC(char[] array,int index){
        char[] r=new char[array.length-1];
        System.arraycopy(array, 0, r, 0, index);
        System.arraycopy(array, index+1, r, index,array.length-index-1);
        return r;
        }
} 

Output example:

how many operands
2
enter a number
12
enter the operation(+,-,/,*)
/
enter a number
3
12.0 / 3.0 

Output stops Here .I didn't get the result of the operation.What is the mistake?What are the changes i need to do for the proper result!?

When I adding an extra System.out.println(""); statement anywhere on the program, It executes the System.out.println("res is "+val[0]); statement.And didnt execute the extra added statement.There I get a problem, the second System.out.println() statement doesn't work. What is the reason for that?

Upvotes: 0

Views: 109

Answers (2)

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

public static void main(String... args) {
    Scanner scan = new Scanner(System.in);

    System.out.print("how many operands: ");
    int totalOperands = scan.nextInt();
    float[] nums = new float[totalOperands];
    char[] operations = new char[totalOperands - 1];

    for (int i = 0, j = 0; i < totalOperands; i++) {
        if (i > 0) {
            System.out.print("enter the operation (+,-,/,*): ");
            operations[j++] = scan.next().charAt(0);
        }

        System.out.print("enter a number: ");
        nums[i] = scan.nextFloat();
    }

    for (int i = 0, j = 0; i < totalOperands; i++) {
        if (i > 0)
            System.out.print(' ');

        System.out.print(nums[i]);

        if (j < operations.length)
            System.out.print(" " + operations[j++]);
    }

    float res = 0;

    for (int i = 0, j = 0; i < totalOperands; ) {
        if (i == 0)
            res = nums[i++];
        else {
            if (operations[j] == '+')
                res += nums[i++];
            else if (operations[j] == '-')
                res -= nums[i++];
            else if (operations[j] == '/')
                res /= nums[i++];
            else if (operations[j] == '*')
                res *= nums[i++];
            else
                throw new RuntimeException("unknown operation");

            j++;
        }
    }

    System.out.println(" = " + res);
}

Upvotes: 0

Keshav
Keshav

Reputation: 1133

You need to change one line in the code.

res = val[i] / val[i + 1];

you are using + 2 for index but the operand is in op[] and not in val[]

Upvotes: 1

Related Questions