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
Which chart element provides the boundaries of the graphic?
slava [35]
If your choices are the following:
<span>A. Legend
B. Chart area
C. Slices
D. Chart elements

Then the answer is letter B. </span>Chart area is the boundary that contains all the chart and all its elements including the chart titles, legends, plot area, labels, etc. 
4 0
4 years ago
What controls are available on the ribbon in a contact window
sergij07 [2.7K]

Contact solution is the answer and the work is your brain
8 0
4 years ago
A spreedsheet program display data in a special document called a
Tamiku [17]
Google sheets. or excel. 
7 0
3 years ago
Read 2 more answers
Which statement is false?
Tanzania [10]

Answer:

b) A type parameter name must be unique among different generic methods.

Explanation:

<u>These statements are true</u>

Each type parameter list contains one or more type parameters, separated by commas.

The type parameters act as placeholders for the types of the arguments passed to a method.

The type parameter names throughout the method declaration must match those declared in the type parameter list.

<u>But</u>

A type parameter name may be different among different generic methods.

7 0
4 years ago
1. What is the connection between a credit report and a credit score?
viktelen [127]

Answer:

<em>Credit report: A credit report is a record of a consumer's credit history and serves as credit references. Credit score: A credit score is an algorithm that measures your credit risk based on the information in your credit report at one point in time.</em>

Hope this helps!!

5 0
3 years ago
Other questions:
  • Use the following business rules to assign name to the tables on the ERD: Note: These rules are in the context of a small retail
    7·1 answer
  • Adam is so good at playing arcade games that he will win at every game he plays. One fine day as he was walking on the street, h
    13·1 answer
  • Validation ensures that a website looks good.<br> True or False
    9·1 answer
  • In 4-bit two's complement representation, what is the binary encoding of the number -5?
    8·1 answer
  • . Which of the following is the correct syntax for a method header with parameters? a. public static void example(x, y) { b. pub
    8·1 answer
  • A set of instructions to increase a programmer’s pay rate by 10 percent is hidden inside an authorized program. It changes and u
    12·1 answer
  • A customer seeks to buy a new computer for a private use at home.The customer primarily needs the computer to use the Microsoft
    8·1 answer
  • What key do I use to start my presentation
    9·1 answer
  • what is the role of media in our society, and how can we become responsible consumers producers of news and information in the d
    7·1 answer
  • What is the difference between an html opening tag and a closing tag?.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!