Question

may i ask for help with this c++ problem?

Take your solution (or the posted solution) of Question 2, Assignment 4, and change it to use the new Stack class instead of

this is the code i have for assignment 4 question 2:

#include<iostream>

#include<string>

#include<sstream>

#include<stack>

using namespace std;

int main()

{

string inputStr;

stack <int> numberStack;

cout<<"Enter your expression::";

getline(cin,inputStr);

int len=inputStr.length();

stringstream inputStream(inputStr);

string word;

int val,num1,num2;

while (inputStream >> word)

{

//cout << word << endl;

if(word[0] != '+'&& word[0] != '-' && word[0] != '*')

{

val=stoi(word);

numberStack.push(val);

// cout<<"Val:"<<val<<endl;

}

else if(word[0]=='+')

{

num1=numberStack.top();

numberStack.pop();

num2=numberStack.top();

numberStack.pop();

// cout<<num1<<" "<<word<<" "<<num2<<"="<<num1+num2<<endl;

numberStack.push(num1+num2);

}

else if(word[0]=='-')

{

num1=numberStack.top();

numberStack.pop();

num2=numberStack.top();

numberStack.pop();

// cout<<num1<<" "<<word<<" "<<num2<<"="<<num1-num2<<endl;

numberStack.push(num1-num2);

}

else if(word[0]=='*')

{

num1=numberStack.top();

numberStack.pop();

num2=numberStack.top();

numberStack.pop();

// cout<<num1<<" "<<word<<" "<<num2<<"="<<num1*num2<<endl;

numberStack.push(num1*num2);

}

}

int result=numberStack.top();

numberStack.pop();

if(!numberStack.empty())

{

cout<<inputStr<<" is invalid expression."<<endl;

}

else

{

cout<<"Result of RPN::"+inputStr<<" is "<<result<<endl;

}

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
//function to find First index of given number
int find(int a[],int n,int num)
{
   int static i=0;
   if(i==n)
   return n;
   if(a[i]==num)
   return i;
   i++;
   return find(a,n,num);
  
}
//function to find last index of given number
int rfind(int a[],int n,int num)
{
   int static i=n-1;
   if(i<0)
   return n;
   if(a[i]==num)
   return i;
   i--;
   return rfind(a,n,num);
}
int main( )
{
   int length;
   cout<<"Enter Array Length:";
   cin>>length;//length of array
   cout<<"\n";
   //cout<<length;
   if(length==0)
   return 0;
   else
   {
   int a[length];//array declaration
   cout<<"Enter elements in an array\n";
   //taking array input from user
   for(int i=0;i<length;i++)
   {
       cin>>a[i];
   }
   cout<<"Enter element to be searched :";
   int search;
   cin>>search;//taking input of search element
   cout<<"\n";
   cout<<"\nFirst Index of search element:(Index starts from zero)";
   cout<<find(a,length,search);//calling find() functin on search element
   cout<<"\n";
   cout<<"\nLast Index of search element:(index starts from zero)";
   cout<<rfind(a,length,search);//calling rfind() function on search element
   }
}

Add a comment
Know the answer?
Add Answer to:
May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • This is a fill in the code type: // FILL IN THE CODE - Write a...

    This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() {   int num1;       // holds 1st number   int num2;       // holds 2nd number   int result;       // holds result of multiplication   int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number   int *num2Ptr...

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • So the assignment is to write a program that have the user entered 3 numbers and...

    So the assignment is to write a program that have the user entered 3 numbers and than it will sort it by ascending order. Here are the code I wrote, but it won't compiled and I kept on getting an error message "Using uninitiazed memory" for all the variables. Can someone please help take a look and see whats going on? #include <iostream> using namespace std; int main() { //variables int num1, num2, num3; int lowest, middle, highest; //user inputs...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • I have C++ code. How I can change to Java code #include <iostream> using namespace std;...

    I have C++ code. How I can change to Java code #include <iostream> using namespace std; int initialNumber[4]; int main() {    void ShowResult();       //initialization    int NuberOfpage[20]={1,0,7,1,0,2,1,2,3,0,3,2,4,0,3,0,2,1,0,7},i,j,f12[3];    int num1=0,num2=0,countr=0,framsize=3;    int inital,current,current1;    for(i=0;i<3;i++) { initialNumber[i]=-1; } for(j=0;j<20;j++) { num1=0,num2=0;    for(i=0;i<3;i++) {    if(initialNumber[i]==NuberOfpage[j]) {    num1=1;    num2=1;    break; } }    if(num1==0)    { for(i=0;i<3;i++)    { if(initialNumber[i]==-1)    {    initialNumber[i]=NuberOfpage[j];    num2=1;    break; } } }    if(num2==0)   ...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT