Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am new to C++ and I want to use a stack to evaluate an expression given as an input (2+3*5+4 for example), containing only numbers, + and *. I wrote this code but it gives me Segmentation fault: 11. Could you please help me solve this or give me a hint about what could be wrong? Thank you! (I noticed there were similar questions on this site, but I couldn't use them to solve my problem)
#include <iostream>
#include <stack>
using namespace std;
bool highPrecedence(char a, char b){
if((a=='+')&&(b=='*'))
return true;
return false;
}
int main()
{
char c = 'a';
double x;
stack<char> stack;
double v[10];
int i=0;
double res;
while(true)
{
c = cin.get();
if(c=='\n'){
while(stack.size()!=0){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
break;
}
if ( '0'<=c && c<='9' )
{
cin.putback(c);
cin>>x;
cout<<"Operand "<<x<<endl;
i=i+1;
v[i]=x;
}
else
{
if(c!=' ') cout<< "Operator " <<c<<endl;
if (stack.size()==0)
stack.push(c);
else{
while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
if (stack.top()=='*'){
double res = v[i]*v[i-1];
i--;
v[i]=res;
stack.pop();
}
if (stack.top()=='+'){
res = v[i]+v[i-1];
i--;
v[i]=res;
stack.pop();
}
}
stack.push(c);
}
}
}
cout<<v[0]<<endl;
}
Using stack.top() is illegal if the stack is empty.
Change while((!highPrecedence(stack.top(),c)) && (stack.size()!=0)){
to while((!stack.empty()) && (!highPrecedence(stack.top(),c))){
The initiali value of i is not good and you are printing uninitialized variable, which has indeterminate value.
Change int i=0; to int i=-1;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000478 at pc 0x0000003a5517 bp 0x7ffe2f5c2670 sp 0x7ffe2f5c2668
Here is my code:
class Solution {
public:
vector summaryRanges(vector& nums) {
if(nums.size() == 0)
return {};
if(nums.size() == 1)
return {to_string(nums[0])};
vector<string> ans{};
int i=0,j=1,initial=nums[0];
if(nums.empty()||nums.size()==0)
return ans;
while(j<=nums.size())
{
if(i<nums.size())
{
if(nums[i]+1==nums[j])
{
i++;
j++;
}
else
{
string str3;
if(initial!=nums[i])
{
string str = to_string(initial);
string str1= to_string(nums[i]);
string str2 = "->";
str3=str+str2+str1;
}
else
{
str3 = to_string(initial);
}
ans.push_back(str3);
initial=nums[j];
i=j;
j++;
}
}
else
{
string str = to_string(initial);
ans.push_back(str);
}
}
return ans;
}
};
I think you are accessing an index out of vector size . Index j is probably going out of bounds .
while(j< = nums.size())
and
nums[j]
This
while(j<=nums.size())
and these
if(nums[i]+1==nums[j])
...
initial=nums[j];
look very suspicious. If j == nums.size() then nums[j] is an out of bounds error on num. Maybe you meant this?
while(j<nums.size())
(I.e. the same as you have with the i variable).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am writing this code on Visual studio 2019 which converts a prefix number to infix.
When i press F5 the window says: (process 10428) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
I don't know much about C++. I know Java and python fairly well but our instructor wants us to use C++.
string s[7];
int f = -1;
void push(string a)
{
s[f++] = a;
}
string pop()
{
return s[f--];
}
bool isop(char x) {
switch (x)
{
case '+':
case '-':
case '*':
case '/':
return true;
}
return false;
}
int main() {
string a = "*+ab+cd";
reverse(a.begin(), a.end());
for (int i = 0;i<int(a.length());i++) {
if (isop(a[i])) {
string v1 = pop();
string v2 = pop();
string h = "(" + v1 + a[i] + v2 + ")";
push(h);
}
else {
push(string(1, a[i]));
}
}
for (int i = 0;i < 7;i++)
{
cout << s[i];
}
return 0;
}
Problem here (it would be exactly the same in Java)
string s[7];
int f = -1;
void push(string a)
{
s[f++] = a;
}
The first time you push f is -1 so you have an out of bounds array access. I guess you meant this
void push(string a)
{
s[++f] = a;
}
You could have avoided this error by using a std::vector (similar to an ArrayList in Java).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This is an assignment given to me.
I am a noob just started programming.
The string as a whole won't push onto the stack and how to pop it.
Problem statement:- divide a whole string consisting of a full name, divide the string into 3 parts to get the first name middle name and surname and display them in the order of surname first name middle name USING STACKS ONLY.
I've tried using 2D stack
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string>
using namespace std;
using std :: string;
char s1[100];
char s2[50],s3[50],s4[50];
int i=0,j=0,k=0,max1=9,top=-1;
char stack[10][10];
char re[10];
void push(char val[])
{
if(top>=max1)
{
cout<<"Stack overflow";
}
else
{
top++;
int a=0;
for (int i=0;i<stack[top]['\0'];i++)
{
stack[top][a]=val[i];
a++;
}
}
}
char* pop()
{
if(top<0)
{
cout<<"Stack underflow";
}
else
{
//for(int j=0;j<=top)
for(int i=0;i<stack[top]['\0'];i++)
{
re[i]=stack[top][i];
top--;
return re;
}
}
}
void divstring()
{
for(i=0;s1[i]!=' ';i++)
{
s2[i]=s1[i];
}
s2[i]='\0';
i++;
while(s1[i]!=' ')
{
s3[j]=s1[i];
j++;
i++;
}
s3[j]='\0';
i++;
while(s1[i]!='\0')
{
s4[k]=s1[i];
k++;
i++;
}
s4[k]='\0';
i++;
}
int main()
{
//clrscr();
cout<<"Enter the string: ";
gets(s1);
divstring();
cout<<"The 1 part is "<<s2<<endl;
cout<<"The 2 part is "<<s3<<endl;
cout<<"The 3 part is "<<s4<<endl;
// getch();
push(s1);
push(s2);
push(s3);
cout<<pop();
return 1;
}
There are no compile time errors but the strings don't get pushed onto the stack nor popped.
There's multiple problems here.
There is no such thing as a 2D stack. A stack is one dimensional. It's got one usable end. In C++, you use std::stack, push(), pop() and empty().
You need to get rid of the global variables.
You seem to want to implement your own stack. You need to decide, do you want to implement a stack, that is one question. Or, do you want to use an existing stack to perform the assignment? that's a different question. I can't tell between the two.
You should use std::string.
You don't handle boundary condition. For example passing a string without spaces will loop infinitely in
for(i=0;s1[i]!=' ';i++)
The push function is very confused, here's a working version
void push(char val[])
{
if (top >= max1)
{
cout << "Stack overflow";
}
else
{
top++;
int i = 0;
for (; val[i] != '\0'; i++)
stack[top][i] = val[i];
stack[top][i] = '\0';
}
}
There are many ways you could improve this, you could use the C strcpy function instead of copying your strings by hand. Or, heaven forbid, you could use some C++, like std::string and std::stack.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The program below is for sorting a list using quicksort C++.The code typed below has compiled successfully in code::blocks and in http://cpp.sh/, but unfortunately it hangs after entering in the elements,any help will be appreciated..
void quicksort(vector<int> v,int left_index,int right_index)
{
if(left_index>=right_index)
return;
int pivot=(right_index+left_index)/2;
int left=left_index;
int right=right_index;
while(left<=right)
{
while(v[left]<v[pivot])
left++;
while(v[right]>v[pivot])
right--;
if(left<=right)
{
swap(v,left,right);
left++;right--;
}
}
quicksort(v,left_index,right);
quicksort(v,left,right_index);
}
Passing by reference is must as others have pointed out.
Keep pivot constant during a partition. pivot = v[pivot] ensures that.
outer loop bounds changed to left<=right from left<right.
The running code.
#include <iostream>
#include<vector>
using namespace std;
void print(const vector<int> &v)
{
cout<<"The sorted list is:"<<endl;
for(int i=0;i<(int)v.size();i++)
cout<<v[i]<<' ';
cout<<endl;
}
void swap(vector<int> &v,int left,int right)
{
int temp=v[left];
v[left]=v[right];
v[right]=temp;
}
void quicksort(vector<int> &v,int left_index,int right_index)
{
if(left_index>=right_index)
return;
int pivot=(right_index+left_index)/2;
pivot = v[pivot];
int left=left_index;
int right=right_index;
while(left<right)
{
while(v[left]<=pivot)
left++;
while(v[right]>pivot)
right--;
if(left<right){
swap(v,left,right);
left++;
right--;
}
}
quicksort(v,left_index,right);
quicksort(v,left,right_index);
print(v);
}
int main()
{
int no;
vector<int> v;
cout << "Please enter the elements in your list" << endl;
cout << "Enter 0 to exit..."<<endl;
while(cin >> no)
{
if(no==0)
break;
v.push_back(no);
}
quicksort(v,0,v.size()-1);
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've started C++ not too long ago, and I have problems understanding why I can'T seem to be able to create two functions outside my main. When I only have 1, all is good, the second I add the second one, which is the Far one, tell me to put a ; after my cel function declaration...
// Lab03.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main()
{
double celcius(int);
double far(int);
std::cout<<"DEGREE DE FAR A CEL\n";
for (int i=32; i<213; i++)
{
std::cout.precision(3);
std::cout<<i<<"F = " <<celcius(i)<<"C ";
if ((i+1)%4==0)
{
std::cout<<"\n";
}
std::cout<<"\n\n\nDEGREE DE CEL A FAR\n";
for (int i=0; i<101; i++)
{
std::cout.precision(3);
std::cout<<i<<"C = " <<far(i)<<"C ";
if ((i+1)%4==0)
{
std::cout<<"\n";
}
}
_gettch();
return 0;
}
double celcius(int n)
{
double endcel;
endcel= (n-32.0)*(5.0/9.0);
return endcel;
}
double far(int o)
{
double endfar=(o*(9/5))+32;
return endfar;
}
It looks like you're missing an end } to close your main function just prior to the celcius function.
Proper code indentation will help you find problems like this in the future.
// Lab03.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
int main() {
double celcius(int);
double far(int);
std::cout<<"DEGREE DE FAR A CEL\n";
for (int i=32; i<213; i++) {
std::cout.precision(3);
std::cout<<i<<"F = " <<celcius(i)<<"C ";
if ((i+1)%4==0) {
std::cout<<"\n";
}
std::cout<<"\n\n\nDEGREE DE CEL A FAR\n";
for (int i=0; i<101; i++) {
std::cout.precision(3);
std::cout<<i<<"C = " <<far(i)<<"C ";
if ((i+1)%4==0) {
std::cout<<"\n";
}
}
_gettch();
return 0;
}
}
double celcius(int n) {
double endcel;
endcel= (n-32.0)*(5.0/9.0);
return endcel;
}
double far(int o) {
double endfar=(o*(9/5))+32;
return endfar;
}