1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Rom4ik [11]
3 years ago
15

Write a function int checkBalance(char exp[]); that can take an expression as input and check whether the parenthesis used in th

at expression are valid or invalid. It returns 1 if it is valid, and returns 0 if it is not valid.For example, the following expressions are valid: [ A * {B (C D)}] and [{()()}]The following expressions are invalid: [ A * {B (C D})], [ { ( ] ) ( ) }
Computers and Technology
1 answer:
mylen [45]3 years ago
3 0

Answer:

Explanation:

The following code is written in Java. It uses for loops to loop through the array of characters, and uses IF statements to check if the symbols in the expression are balanced. If so it returns true, otherwise it breaks the function and returns false. Two test cases have been provided in the main method and the output can be seen in the attached image below.

import java.util.ArrayList;

class Brainly {

   public static void main(String[] args) {

       char[] exp = { '[', 'A', '*', '{', 'B', '(', 'C', 'D', ')', '}', ']'};

       char[] exp2 = {'[', 'A', '*', '(', 'C', '}', ']'};

       System.out.println(checkBalance(exp));

       System.out.println(checkBalance(exp2));

   }

   public static boolean checkBalance(char exp[]) {

       ArrayList<Character> symbols = new ArrayList<>();

       for (char x: exp) {

           if ((x =='[') || (x == '{') || (x == '(')) {

               symbols.add(x);

           }

           if (x ==']') {

               if (symbols.get(symbols.size()-1) == '[') {

                   symbols.remove(symbols.size()-1);

               } else {

                   return false;

               }

           }

           if (x =='}') {

               if (symbols.get(symbols.size()-1) == '{') {

                   symbols.remove(symbols.size()-1);

               } else {

                   return false;

               }

           }

           if (x ==')') {

               if (symbols.get(symbols.size()-1) == '(') {

                   symbols.remove(symbols.size()-1);

               } else {

                   return false;

               }

           }

       }

       return true;

   }

}

You might be interested in
Dynamic addressing: __________.
Anarel [89]

Explanation:

jwjajahabauiqjqjwjajjwwjnwaj

6 0
3 years ago
Read 2 more answers
This image shows a web designer's grids for different pages on a website. The uppermost box on each page is the website's identi
allsm [11]

Answer: d

Explanation: hope this helps

6 0
3 years ago
Read 2 more answers
The faster the film (the higher the ISO), the more visible its grain. When you are using higher ISO, you are not just bringing g
NikAS [45]
I could not tell you
3 0
3 years ago
Read 2 more answers
7. Little Big Planet advanced the platforming genre of games by __________.
patriot [66]

Answer:

Explanation:

c because it will hook the person

5 0
3 years ago
What would be the results of the following code? int[] x = { 55, 33, 88, 22, 99, 11, 44, 66, 77 }; int a = 10; if(x[2] &gt; x[5]
Nezavi [6.7K]

Answer:

The answer is "Option A".

Explanation:

In the given code an integer array "x" is defined, that stores some elements and another integer variable "a" is declared, that holds a value that is "10". In this code a conditional statement is defined, which checks array element value, in if block element of array that position is 2 is greater then the element of array that position is 5.if this condition is true, so variable a value is change that is equal to 5. In else block if the above condition is not true so the value of a variable is equal to 8, and another options is wrong that can be described as follows:

  • In option B, The given condition is not false, that's why it is not correct.
  • In option C, The value of variable a is changed when condition is true or false, that's why it is not correct.
  • In option D, It is wrong because we can compare array elements.

6 0
3 years ago
Other questions:
  • Which graphic design tool allows you to lay images on top of one another? A. Layers B. Selection C. Drawing D. Color
    10·2 answers
  • Less than 40 percent of teens have used marijuana within the past year. True or false?
    8·1 answer
  • The exercise instructions here are LONG -- please read them all carefully. If you see an internal scrollbar to the right of thes
    8·1 answer
  • The ____ command displays the last 10 lines of a text file.
    5·1 answer
  • Which system utility can you use to troubleshoot a computer that's slow to start by enabling or disabling startup programs?
    7·2 answers
  • Due dates is the final date an assignment will be accepted. Plan ahead to ensure should you face difficulty with the assignment
    9·2 answers
  • Dose anyone know how to change username, grade level, and gender here? I have tried it in the change preferences and it says it
    8·2 answers
  • A occurs when you reset a mobile device but retain your installed applications and personal settings
    10·2 answers
  • Which of the following statements describes the general idea of an assistive media​
    7·1 answer
  • Why does Homework exist? Why is it so important?
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!