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
I have this assignment due TONIGHT BEFORE 10PM! Please help. 50 points for whoever will help!! Here is the assignment.
saveliy_v [14]

Answer:

# include <iostream.h>

# include <conio.h>

using namespace std;

int generatelist ( int z[]);

int addlist(int x []; int y []);

int mullist(int x []; int y []);

int displayList(int z[]);

main()

{

int a[10] , b[10], add[10], mul[10];

cout<<"Enter value in array a";

generatelist(a[10]);

cout<<"Enter value in array b";

generatelist(b[10]);

for (int i=0; i<=9; i++)

{

add[i]=addlist( a[i], b[i]);

cout<<"sum of list = "<< add[i];

}

for (int i=0; i<=9; i++)

{

mul[i]=mullist( a[i], b[i]);

cout<<"multiplication of list = "<< mul[i];

}

for (int i=0; i<=9; i++)

{

displayList(a[i]);

}

for (int i=0; i<=9; i++)

{

displayList(b[i]);

}

for (int i=0; i<=9; i++)

{

displayList(add[i]);

}

for (int i=0; i<=9; i++)

{

displayList(mul[i]);

}

getch();

}

int generatelist ( int z[])

{

for (int j=0 ; j<=9 ; j++)

{

cin>>z[j];

return z[j]

}

}

int addlist(int x []; int y [])

{

int add

add=x+y;

return add;

}

int mullist(int x []; int y [])

{

int mul

mul=x*y;

return mul;

}

int displayList(int z[]);

{

for(int i=0; i<=9;i++)

cout << z[i]

}

7 0
4 years ago
Which button do you use to put data in a specific order
3241004551 [841]
<span>arrange a list of names in alphabetical order, compile a list of product inventory levels from highest to lowest, or order rows by colors or icons</span>
8 0
3 years ago
The music is the soul who says this
Crazy boy [7]

Question:the music is the soul who says this

Answer: Arthur Schopenhauer

Explanation: he says music is the language of feeling and of passion

question answered by

(jacemorris04)

4 0
3 years ago
A company asked you help mitigate the brute force attacks carried out against its users' Windows account passwords. You successf
prohojiy [21]

Answer:

The following options are true.

Explanation:

A corporation has requested the user to support minimize the brute force attacks on credentials of their Microsoft account holder's. They effectively eliminated the malware involved in those assaults and have to protect their credentials allocated towards their user profiles properly without restricting the usefulness of the program.

The following should be included in securing the user accounts are need credentials for the user profile, create logon constraints that failed as well as need powerful credentials.

7 0
3 years ago
Alison retrieved data from a company database containing personal information on customers. When she looks at the SSN field, she
Aleksandr [31]

Answer:

Data Masking

Explanation:

Data masking, also known as data obfuscation, is a process companies use to hide data. The main reason for applying masking to a data field is to protect data that is classified as personally identifiable information, sensitive personal data, or commercially sensitive data. Imagine a scenario where your organization is working with a contractor to build a database. Masking your data allows the contractor to test the database environment without having access to actual sensitive customer information.

5 0
4 years ago
Other questions:
  • Which of the following steps in enumeration penetration testing extracts information about encryption and hashing algorithms, au
    13·1 answer
  • What shooting position is commonly used when hunting with a shotgun?
    14·2 answers
  • What is the most important external issue when using social media in emergency management?
    6·1 answer
  • By default, the Windows desktop displays
    6·1 answer
  • In UML behavioral modeling, a message is _____. (Points : 6) a named location in memory where information is deposited and retri
    7·1 answer
  • Â A number of companies and/or agencies, such as National ____ and Atmospheric Administrationâs National Weather Service, provid
    9·1 answer
  • SYSTEMY INFORMATYCZNE: Archiwizacja i kompresja zbiorów systemu Linu
    8·1 answer
  • Madeleine's Instructor praised her for capturing a nearly perfect image. It included dominance, harmony, and proportion among ot
    13·1 answer
  • Click print to publish your post in a blog true or false.​
    5·2 answers
  • How easily can teachers see browsing history on chromebook]
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!