Question

In: Computer Science

Consider the following code. Explain what this code does and determine the output. Let us discuss....

Consider the following code. Explain what this code does and determine the output. Let us discuss.

#include <iostream>
using namespace std;
int top;
void check (char str[ ], int n, char stack [ ])
{
for(int i = 0 ; i < n ; i++ )
{
if (str [ i ] == ‘(’)
{
top = top + 1;
stack[ top ] = ‘ ( ’;
}
if(str[ i ] == ‘)’ )
{
if(top == -1 )
{
top = top -1 ;
break ;
}
else
{
top = top -1 ;
}
}
}
if(top == -1)
cout << “String is balanced!” << endl;
else
cout << “String is unbalanced!” << endl ;
}

int main ( )
{

char str[ ] = { ‘(‘ , ‘a’ , ‘+’, ‘ ( ’, ‘b ’ , ‘-’ , ‘ c’ ,‘)’ , ‘ ) ’} ;


char str1 [ ] = { ‘(’ , ‘(’ , ‘a’ , ‘ + ’ , ‘ b’ , ‘)’ } ;
char stack [ 15 ] ;
top = -1;
check (str , 9 , stack ); //Passing balanced string
top = -1 ;
check(str1 , 5 , stack) ; //Passing unbalanced string

}

Solutions

Expert Solution

/*
In the provide code the code checks the given expression is balanced or not. for this
here we are using a check function that identifies the expression and give the proper
output. for this in function check. we are using stack array as a stack for push the braces {[()]} for operation
and if the braces come in expression then it will go to stack. if the operator comes +-/*then
it will pop the expression and match from another stack symbol. and finally it tells us .. it is
balanced or not


*/


#include <iostream>
using namespace std;
int top;
void check (char str[ ], int n, char stack [ ])
{
for(int i = 0 ; i < n ; i++ )
{
if (str [ i ] == '(')
{
top = top + 1;
stack[ top ] = ' ( ';
}
if(str[ i ] == ')' )
{
if(top == -1 )
{
top = top -1 ;
break ;
}
else
{
top = top -1 ;
}
}
}
if(top == -1)
cout << "String is balanced!" << endl;
else
cout << "String is unbalanced!" << endl ;
}

int main ( )
{

char str[ ] = { '(' , 'a' , '+', ' ( ', 'b ' , '-' , ' c' ,')' , ' ) '} ;


char str1 [ ] = { '(' , '(' , 'a' , ' + ' , ' b' , ')' } ;
char stack [ 15 ] ;
top = -1;
check (str , 9 , stack ); //Passing balanced string
top = -1 ;
check(str1 , 5 , stack) ; //Passing unbalanced string

}


Related Solutions

Explain what this code does. Will this work? Let us discuss cents = new *Ent[num_cents]; for...
Explain what this code does. Will this work? Let us discuss cents = new *Ent[num_cents]; for (i=1;i<num_cents+1;i++) { cents[i-1] = new Ent(); }
Determine the output of the following pseudo-code. Also, explain how did you get this output. fun(int...
Determine the output of the following pseudo-code. Also, explain how did you get this output. fun(int n){ if (n==1) then return; println(n); if (n%2=0) then fun(n/2); else fun(3*n + 1); } Main(){ fun(14); }
What is the output of the following code? Explain your answer in a paragraph or two....
What is the output of the following code? Explain your answer in a paragraph or two. class ExceptionThrown { static int divideByZero(int a, int b){ int i = a/b; return i; }    static int computeDivision(int a, int b) { int res =0; try{    res = divideByZero(a,b); } catch(NumberFormatException ex){ System.out.println("NumberFormatException is occured"); }    return res; }    public static void main(String args[]){ int a = 1;    int b = 0; try{ int i = computeDivision(a,b); }...
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
Run the following code and explain what the code does Run this code at least twice...
Run the following code and explain what the code does Run this code at least twice and take screenshots Explain what the code does What is the running result supposed to be What caused this issue? #include <thread> #include <iostream> using namespace std; const unsigned int NTHREADS = 20; const int ITERS = 10000000; int counter; void increment() {        for (int i = 0; i < ITERS; i++)                      counter++;               } void decrement() {        for (int i...
no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
What output is produced by the following code?   Explain how it works. public class A {...
What output is produced by the following code?   Explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void main(String[]...
Let us consider strategic risk. Look at the matrix and identify the two parameters that determine...
Let us consider strategic risk. Look at the matrix and identify the two parameters that determine strategic risk. These two parameters are about the threat of opportunistic behavior from third-parties from which a company sources services. They do not necessarily distinguish between offshore and onshore providers. This observation leads us to a question: Now think of the measures that a company can adopt to mitigate strategic risk. Are some of those measures weakened by (or are costlier to implement) in...
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
What output is produced by the following code? Explain how it works (at least 5 lines)....
What output is produced by the following code? Explain how it works (at least 5 lines). class Super { int num = 1; int id = 2; } class Sub extends Super { int num = 3; public void display() { System.out.println(num); System.out.println(super.num); System.out.println(id); } } public class Inheritance1 { public static void main (String[] args) { Sub obj = new Sub(); obj.display(); } } =============================== Example of "Explain how it works" Sample Question: What output is produced by the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT