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
A "slideument" is:
Sophie [7]

Answer:

c. An electronic slide that tries to function as both a presentation visual and a printed document

Explanation:

Slideument combines both text and visual aids to form a document that can be fully understood by readers without the use of speakers when presenting these documents. It is a presentation with information containing visual aids and presented in form of slide deck so that it can be easily understood by readers.

8 0
3 years ago
A good sentence about art
victus00 [196]

Answer:

Art, in its broadest sense, is a form of communication. It means whatever the artist intends it to mean, and this meaning is shaped by the materials, techniques, and forms it makes use of, as well as the ideas and feelings it creates in its viewers . Art is an act of expresThe three classical branches of visual art are painting, sculpture, and architecture. Music, theatre, film, dance, and other performing arts, as well as literature and other media such as interactive media, are included in a broader definition of the arts.

sing feelings, thoughts, and observations.

Now, remove any element founded in creativity, art and design, and all that remains are piles of materials that require human imagination and visual thinking. Art forces humans to look beyond that which is necessary to survive and leads people to create for the sake of expression and meaning.

MAY THE FORC E BE WITH U

6 0
3 years ago
Why are conditional statements valuable to programmers? Check all that apply
Gnom [1K]

Answer:They provide a way for programmers to add decision-making to a computer program.

They allow programmers to specify conditions that must be met for an action to occur.

They allow a program to evaluate multiple values within a statement.

Explanation:

7 0
3 years ago
Read 2 more answers
Background information
Ber [7]
Well I guess no breathing for you lol
6 0
3 years ago
Read 2 more answers
What are the pros and cons of using the internet in a medical office setting?
Shkiper50 [21]
Pros: are doctors are able to educate themselves in order to diagnose a patient, it's fast and convenient to put in records, medical facilities can transfer information from different locations etc.

Cons: the system can breached, this could leak health and personal information from patients, and the power could go down, delaying the use of signing in patients and etc
5 0
3 years ago
Other questions:
  • To save the changes to the layout of a table, click the Save button on the _____.
    13·1 answer
  • Trading your bicycle for a snowboard exemplifies ?
    15·1 answer
  • Employers will check you out on social media sites like Facebook, MySpace, and Twitter.
    6·2 answers
  • 58:30
    8·1 answer
  • Which option will you use to expose your presentation to the audience
    11·2 answers
  • Which steps will create a new blank word document
    9·1 answer
  • In most operating systems what is running application called?
    5·1 answer
  • A __________ search engine focuses on a specific subject.<br><br>answer : Specialized
    8·1 answer
  • Write a method, including the method header, that will receive an array of integers and will return the average of all the integ
    7·1 answer
  • Marcus creates a HTML webpage on his favourite game, football. On clicking the word “football” another website containing the in
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!