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
What is used to accurately position objects on the slide using a single horizontal and vertical line that intersects in the cent
mars1129 [50]
I think its the ruler c
7 0
2 years ago
Read 2 more answers
Quick I need help ASAP
Alexeev081 [22]

Answer:

1. Spreadsheets are an essential business and accounting tool. They can vary in complexity and can be used for various reasons, but their primary purpose is to organize and categorize data into a logical format. Once this data is entered into the spreadsheet, you can use it to help organize and grow your business.

2. A spreadsheet is a tool that is used to store, manipulate and analyze data. Data in a spreadsheet is organized in a series of rows and columns and can be searched, sorted, calculated and used in a variety of charts and graphs.

3. ?


GOOD LUCK!

4 0
2 years ago
Read 2 more answers
O novo funcionário da equipe de desenvolvimento de sistemas está aprendendo os termos mais utilizados no dia a dia da empresa. A
ratelena [41]

<span>Os insetos são insetos</span>
3 0
3 years ago
What are some of the issues that organizations need to be aware of when designing and managing data?
tensa zangetsu [6.8K]

Answer and Explanation:

There are two primary issues.  

The principal issues is Security of data or information.

The information must be overseen such that it is upheld up regularly and kept secure from any catastrophic event or outer hacking.  

Second, when planning a framework to oversee information, we have to make it versatile and adaptable.

The data should be scalable , i.e., the component used to store the information must be effectively ready to develop as we collect more information or the sort of information contribution to the framework needs to change after some time.

7 0
3 years ago
What is blogging
tester [92]
Typically, blogging is online journaling. If you've seen Ask Me Why, she blogs throughout the whole movie and tells those who are watching her blog what is happening in her life. So, the answer is A.
5 0
3 years ago
Read 2 more answers
Other questions:
  • What is an example of asynchronous communication
    7·1 answer
  • 1. Perform the following Binary Additions 101101 + 10100 *
    13·2 answers
  • Which word in brackets is most opposite to the word in capitals? PROSCRIBE (allow, stifle, promote, verify)​
    14·2 answers
  • . Two or more functions may have the same name, as long as their _________ are different.
    9·1 answer
  • "the magical number seven, plus or minus two" refers to the storage capacity of ________ memory.
    5·2 answers
  • Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an in
    13·1 answer
  • A(n) ____ string contacts the data source and establishes a connection with the database using the Data Source Configuration Wiz
    5·1 answer
  • Which would you trade on a stock exchange? A: Shares B: Bonds C: Annuities
    6·2 answers
  • Which of the following that can be use to access session variables in PHP?
    14·1 answer
  • From which os did windows xp evolve?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!