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
jeff has just upgraded from windows 7 to windows 10 and he is confused. He has started several universal apps but he can't figur
Andre45 [30]

Answer: Find the app you want to close, then go to the moveable panel and click the X.

3 0
3 years ago
Which type of error is a random error
zlopas [31]

Answer:

statistical fluctuations

Explanation:

8 0
3 years ago
Read 2 more answers
Help brainleist giving exam
Digiron [165]

Explanation:

11. True

12: Electronic Fund Transfer

13. True

14. I'm not sure but I think yes?..

(I'm not sure if your including number 15 but just in case, the full form of PIN is personal identification number)

4 0
2 years ago
Consider the following schema for customer: (id, name, street, city, state, zip, phone), in which customers are uniquely determi
Art [367]

Answer:

Super key is a set of attributes that uniquely identifies all attributes in a relation.Super keys are more than candidate key.

Explanation:

For example in the given schema where

Customer: (id , name , street , city,state,zip,phone)

We have a relation we check super keys by following dependencies

Functional Dependencies                          Super Key

id                                                                     YES

name                                                               YES

id , name -> street , city,state,zip,phone       YES

id , street  ->name , city,state,zip,phone       YES

id,city ->name , street ,state,zip,phone         YES

id,state ->name , street ,city,zip,phone         YES

id,phone  ->name , street ,state,zip,city        YES

id , name , street                                             YES

id , name , street , city                                    YES

id , name , street , city,state                          YES

id , name , street , city,state,zip,phone         YES

All these are uniquely identified

All these are super key but in reverse they are not super key such as

street , city,state,zip,phone->id , name        No

8 0
2 years ago
Can a result that contains road maps for European countries have a highly meets rating
Mice21 [21]

Đápán3e

Explanation:

8 0
3 years ago
Other questions:
  • What is the IEEE standard for the Wi-Fi Protected Access 2 (WPA2) security protocol?
    6·1 answer
  • The expressionvecCont.empty() empties the vector container of allelements.
    13·1 answer
  • How do optical discs store data? select one:
    15·1 answer
  • Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binar
    12·1 answer
  • when using presentation software, which menu can you use to duplicate a slide to use as a template for a new slide?
    14·1 answer
  • New product ideas must fit into a company's mission statement and?
    12·1 answer
  • Which of these is a negative effect of computer technology's role in creating
    9·1 answer
  • Please help it’s timed
    13·1 answer
  • ARGENT !!20 POINTS <br> А ________ translates commands from a computer to draw lines on paper.
    10·2 answers
  • What are the value and data type (float, int, bool, or str) of this expression:<br> 5 // 2
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!