Why is there segment fault line 46? - c++

evaluation of infix expression. there is segmentation fault in line 40.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int pre(char);
int operation(int, int, char);
int fun(string);
int main() {
string s;
cin >> s;
int res = fun(s);
cout << res;
return 0;
}
// function to get result
int fun(string s) {
stack<int> s1;
stack<char> s2;
for (int i = 0; i < s.length(); i++) {
if (isdigit(s[i])) {
int val = 0;
while (i < s.length() && isdigit(s[i])) {
val = val * 10 + (s[i] - '0');
i++;
}
s1.push(val);
} else if (s[i] == '(')
s2.push(s[i]);
else if (s[i] == ')') {
while (!s2.empty() && s2.top() != '(') {
char oprt = s2.top();
s2.pop();
int v2 = s1.top();
s1.pop();
int v1 = s1.top();
s1.pop();
int r = operation(v1, v2, oprt);
s1.push(r);
}
if (!s2.empty())
s2.pop();
} else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
while (s2.size() > 0 && pre(s[i]) <= pre(s2.top())) {
char oprt = s2.top();
int v2 = s1.top();
s1.pop();
int v1 = s1.top();
s1.pop();
int r = operation(v1, v2, oprt);
s1.push(r);
}
s2.push(s[i]);
}
}
while (!s2.empty()) {
int v2 = s1.top();
s1.pop();
int v1 = s1.top();
s1.pop();
char oprt = s2.top();
s2.pop();
int r = operation(v1, v2, oprt);
s1.push(r);
}
return s1.top();
}
int operation(int v1, int v2, char oprt) {
int r;
switch (oprt) {
case '+':
r = v1 + v2;
break;
case '-':
r = v1 - v2;
break;
case '/':
r = v1 / v2;
break;
case '*':
r = v1 * v2;
break;
}
return r;
}
int pre(char c) {
if (c == '+' || c == '-')
return 1;
else
return 2;
}

Look at this code:
for (int i = 0; i < s.length(); i++) {
if (isdigit(s[i])) {
int val = 0;
while (i < s.length() && isdigit(s[i])) {
val = val * 10 + (s[i] - '0');
i++;
}
s1.push(val);
}
You use the same variable in both loops. As the result, you would miss some characters. For example: "123()". In this example you would encounter the first digit at the position 0 and start the inner loop. The inner loop would stop at the position 3 where the symbol '(' is located. Then you finish the iteration and the new iteration starts... after the i++ increment of the outer loop. As the result you would miss the symbol.
Sooner or later you would get into this loop, where you check the stack for emptiness one time but pop three times:
while (!s2.empty() && s2.top() != '(') {
char oprt = s2.top();
s2.pop();
int v2 = s1.top();
s1.pop();
int v1 = s1.top();
s1.pop();
int r = operation(v1, v2, oprt);
s1.push(r);
}
Should be no surprise that on some inputs you would get the segmentation fault.

Related

problem combining bignum library with bubble sort

From this post: Compare two string as numeric value
I did not get a result.
Please open this post so that someone can answer my question.
I wrote this library (pr.h):
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
class BigNum {
private:
string doSum(string a, string b)
{
if(a.size() < b.size())
swap(a, b);
int j = a.size()-1;
for(int i=b.size()-1; i>=0; i--, j--)
a[j]+=(b[i]-'0');
for(int i=a.size()-1; i>0; i--)
{
if(a[i] > '9')
{
int d = a[i]-'0';
a[i-1] = ((a[i-1]-'0') + d/10) + '0';
a[i] = (d%10)+'0';
}
}
if(a[0] > '9')
{
string k;
k+=a[0];
a[0] = ((a[0]-'0')%10)+'0';
k[0] = ((k[0]-'0')/10)+'0';
a = k+a;
}
return a;
}
bool isSmaller(string str1, string str2)
{
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i = 0; i < n1; i++) {
if (str1[i] < str2[i])
return true;
else if (str1[i] > str2[i])
return false;
}
return false;
}
string findDiff(string str1, string str2)
{
if (isSmaller(str1, str2))
swap(str1, str2);
string str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n1 - n2;
int carry = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sub = ((str1[i + diff] - '0') - (str2[i] - '0')
- carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str.push_back(sub + '0');
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (str1[i] == '0' && carry) {
str.push_back('9');
continue;
}
int sub = ((str1[i] - '0') - carry);
if (i > 0 || sub > 0)
str.push_back(sub + '0');
carry = 0;
}
reverse(str.begin(), str.end());
return str;
}
public:
string num;
BigNum () {
num = "";
}
BigNum (string tmp) {
num = tmp;
}
string operator +(BigNum a1) {
return doSum(this->num,a1.num);
}
void operator = (BigNum * a1) {
this-> num = a1-> num;
}
void operator = (string tmp) {
this-> num = tmp;
}
bool operator > (BigNum a1) {
if (this->num>a1.num){
return true;
}
else{
return false;
}
}
bool operator < (BigNum a1) {
if (this->num<a1.num){
return true;
}
else{
return false;
}
}
string operator - (BigNum a1) {
return findDiff(this->num, a1.num);
}
friend ostream &operator<<( ostream &output, const BigNum &D ) {
output << D.num;
return output;
}
friend istream &operator>>( istream &input, BigNum &D ) {
input >> D.num;
return input;
}
string operator -=(string tmp){
this->num=findDiff(this->num, tmp);
return this->num;
}
};
And this code:
// C++ program for implementation of Bubble sort
#include "pr.h"
#include <bits/stdc++.h>
using namespace std;
void swap(BigNum *xp, BigNum *yp)
{
BigNum temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(BigNum arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
/* Function to print an array */
void printArray(BigNum arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver code
int main()
{
BigNum arr[5];
arr[0]="5";
arr[1]="5";
arr[2]="1";
arr[3]="3";
arr[4]="8";
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}
My problem is that this program can only sort 1-digit numbers and gives errors for multi-digit numbers
Example:
This code:
BigNum arr [5];
arr [0] = "100";
arr [1] = "5";
arr [2] = "1";
arr [3] = "3";
arr [4] = "8";
int n = sizeof (arr) / sizeof (arr [0]);
bubbleSort (arr, n);
cout << "Sorted array: \ n";
printArray (arr, n);
Prints this value:
1 100 3 5 8
bool operator < (BigNum a1) is calling std::string:: operator<, thus you getting lexicographical order. The operators < and > should call isSmaller.
bool operator > (const BigNum& a1) const {
return !isSmaller(a1) && num != a1.num;
}
bool operator < (const BigNum& a1) const {
return isSmaller(a1);
}
Do not use constructions
if (smth) return true else return false. You already get true or false in smth, use return smth.

c++. placing points on straight lines

In one of the largest cities in Bytland, Bytesburg, construction of the second stage of the metro is underway. During the construction of the first stage, N stations were built that were not interconnected. According to the master plan, the metro in Bytesburg should consist of no more than two lines. each metro line is straight. The president of the company responsible for laying the lines wants to make sure that no more than two metro lines can be laid, so that all stations built lie on at least one of the two
Exaple 1
input:
6
0 1
1 1
2 1
0 2
1 3
2 2
output:
no
Example 2
input:
6
2 2
4 6
1 0
2 1
6 1
1 1
output:
yes
I wrote the code, but on the seventh test on the testing system, it gives the wrong answer. Help me please. This is my code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct line {
int k, b;
bool x = false;
long long c = 0;
};
int n;
vector<line> lines;
vector<pair<int, int>> p;
bool Is3PointsOnLine(pair<float, float> p1, pair<float, float> p2, pair<float, float> p3) {
return ((p3.first - p1.first)*(p2.second - p1.second) == (p3.second - p1.second)*(p2.first - p1.first));
}
bool PointIsOnLine(line l, int x, int y) {
if (!l.x) {
if (y == ((l.k * x) + l.b))
return true;
}
else {
if (x == l.b)
return true;
}
return false;
}
line f(pair<int, int> c1, pair<int, int> c2) {
int x1 = c1.first;
int y1 = c1.second;
int x2 = c2.first;
int y2 = c2.second;
line ans;
if (x2 != x1) {
ans.k = (y2 - y1) / (x2 - x1);//fix
ans.b = -(x1 * y2 - x1 * y1 - x2 * y1 + x1 * y1) / (x2 - x1);
ans.x = false;
ans.c = 0;
}
else {
ans.k = 0;
ans.b = x1;
ans.x = true;
ans.c = 0;
}
return ans;
}
int a() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i) {
for (int k = 0; k < n; k++) {
if ((k != i) && (k != j) && (Is3PointsOnLine(p[i], p[j], p[k]))) {
lines.push_back(f(p[i], p[j]));
return 0;
}
}
}
}
}
}
int main() {
cin >> n;
p.resize(n);
vector<int> not_used;
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
if (n < 5) {
cout << "yes";
return 0;
}
else {
a();
if (lines.size() == 0) {
cout << "no";
return 0;
}
pair<int, int> e = { -8,-8 };
for (int i = 0; i < n; i++) {
if (PointIsOnLine(lines[0], p[i].first, p[i].second)) {
lines[0].c++;
}
else if (e.first == -8) {
e.first = i;
}
else if (e.second == -8) {
e.second = i;
lines.push_back(f(p[e.first], p[e.second]));
for (int j = 0; j <= i; j++) {
if (PointIsOnLine(lines[1], p[j].first, p[j].second)) {
lines[1].c++;
}
}
e = { -5,-5 };
}
else if (PointIsOnLine(lines[1], p[i].first, p[i].second)) {
lines[1].c++;
}
else {
cout << "no";
return 0;
}
}
if (lines[0].c+1 >= n && e.first != -2 && e.second == -8) {
cout << "yes";
return 0;
}
else if (lines[0].c + lines[1].c >= n) {
cout << "yes";
return 0;
}
else {
cout << "no";
return 0;
}
}
return 0;
}
Code v2
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct line {
pair<int, int> p1, p2;
long long c = 0;
};
int n;
vector<line> lines;
vector<pair<int, int>> p;
bool Is3PointsOnLine(pair<float, float> p1, pair<float, float> p2, pair<float, float> p3) {
return ((p3.first - p1.first)*(p2.second - p1.second) == (p3.second - p1.second)*(p2.first - p1.first));
}
//лежит ли точка на прямой
bool PointIsOnLine(line l, int x, int y) {
return Is3PointsOnLine(l.p1, l.p2, {x , y});
}
int a() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i) {
for (int k = 0; k < n; k++) {
if ((k != i) && (k != j) && (Is3PointsOnLine(p[i], p[j], p[k]))) {
line sdafsadf;
sdafsadf.p1 = p[i]; sdafsadf.p2 = p[j];
lines.push_back(sdafsadf);
return 0;
}
}
}
}
}
}
int main() {
cin >> n;
p.resize(n);
vector<int> not_used;
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
if (n < 5) {
cout << "yes";
return 0;
}
else {
//ищем первую прямую
a();
if (lines.size() == 0) {
cout << "no";
return 0;
}
pair<int, int> e = { -8,-8 };
for (int i = 0; i < n; i++) {
if (PointIsOnLine(lines[0], p[i].first, p[i].second)) {
lines[0].c++;
}
else if (e.first == -8) {
e.first = i;
}
else if (e.second == -8) {
e.second = i;
line sdafsadf;
sdafsadf.p1 = p[e.first]; sdafsadf.p2 = p[e.second];
lines.push_back(sdafsadf);
for (int j = 0; j <= i; j++) {
if (PointIsOnLine(lines[1], p[j].first, p[j].second)) {
lines[1].c++;
}
}
e = { -5,-5 };
}
else if (PointIsOnLine(lines[1], p[i].first, p[i].second)) {
lines[1].c++;
}
else {
cout << "no";
return 0;
}
}
if (lines[0].c+1 >= n && e.first != -2 && e.second == -8) {
cout << "yes";
return 0;
}
else if ((lines[0].c + lines[1].c >= n) && (lines[0].p1 != lines[1].p1) && (lines[0].p1 != lines[1].p2) && (lines[0].p2 != lines[1].p1) && (lines[0].p2 != lines[1].p2)) {
cout << "yes";
return 0;
}
else {
cout << "no";
return 0;
}
}
return 0;
}
Your function f is horribly broken because it uses integer division. Then everything after that which relies on k and b, including PointIsOnLine, will give wrong results. Is3PointsOnLine seems ok, change your line struct to store two points, not slope+intercept, and then PointInOnLine can just call Is3PointsOnLine.
I'm amazed that you passed any non-trivial test cases with such a bug.
This also will not work
if (lines[0].c + lines[1].c >= n)
because you aren't testing that lines[1] isn't a duplicate of lines[0].
Also, you can reach lines[0].c + lines[1].c == n and still miss one station completely, if another station lies at the intersection of the lines and gets double-counted.

Stack calculator

I found this code online so I could study stack calculators, but when I try to enter an expression for the program to evaluate, it gives me 78. Every time. I've combed through it to see if I could debug it by myself, but I have no clue how to fix this. I've added the comments myself to make things more readable. Could anyone help?
#include<iostream>
#include<stack>
#include<cmath>
using namespace std;
int pri(char ch) //precedence checking
{
switch (ch)
{
case '-':
case '+': return 1;
case '*':
case '/': return 2;
case '^': return 3;
case '(':
case ')': return 4;
default: return -1;
}
}
int calculate(char op, int l , int r) //actual calculations
{
if(op == '+') return l + r;
if(op == '-') return l - r ;
if(op == '*') return l * r;
if(op == '/')
{
if(r > 0)
{
return l/r;
}
return 0;
}
if(op == '^') return pow(l,r);
return -1;
}
int main(void)
{
string equ;
cout << "Enter an expression: ";
getline(cin, equ);
char math[equ.size()+1];
int l = sizeof(math)/sizeof(char);
stack<char> s;
stack<int> op_s;
int i = 0;
while(math[i] != '\0')
{
if(math[i] == '(')
{
s.push('(');
}
else if(math[i] == ')')
{
while(s.top() != '(')
{
int r = op_s.top();
op_s.pop();
int l = op_s.top();
op_s.pop();
int re = calculate(s.top(),l,r);
op_s.push(re);
s.pop();
}
s.pop();
}
else if(math[i] == '+' || math[i] == '-' || math[i] == '*' || math[i] == '/' || math[i] == '^')
{
int pC = pri(math[i]);
while(!s.empty() && pri(s.top()) >= pC)
{
int r = op_s.top();
op_s.pop();
int l = op_s.top();
op_s.pop();
int re = calculate(s.top(),l,r);
op_s.push(re);
s.pop();
}
s.push(math[i]);
}
else
{
op_s.push(int(math[i])- 48);
}
i++;
}
while(!s.empty())
{
int r = op_s.top();
op_s.pop();
int l = op_s.top();
op_s.pop();
int re = calculate(s.top(),l,r);
op_s.push(re);
s.pop();
}
cout <<"Result: " << op_s.top() << endl;
return 0;
}
OK So I didn't go through everything with a fine toothed combed, but one obvious issue is that the char values from equ are never copied into the math array.
Below I just removed the C style math array and instead loop through the char values in equ (I renamed the equ variable to math).
Also note I only tested this with 5*5, I did not do any further testing beyond that.
/** Removed for brevity**/
int main( )
{
string math{ "5*5" };
//cout << "Enter an expression: ";
//getline(cin, math);
stack<char> s;
stack<int> op_s;
for ( auto i = 0; i < math.size( ); i++ )
{
if( math[ i ] == '(' )
{
s.push( '(' );
}
else if(math[ i ] == ')')
{
while( s.top( ) != '(' )
{
int r = op_s.top();
op_s.pop();
int l = op_s.top();
op_s.pop();
int re = calculate(s.top(),l,r);
op_s.push(re);
s.pop();
}
s.pop();
}
else if(math[i] == '+' || math[i] == '-' || math[i] == '*' || math[i] == '/' || math[i] == '^')
{
int pC = pri(math[i]);
while(!s.empty() && pri(s.top()) >= pC)
{
int r = op_s.top();
op_s.pop();
int l = op_s.top();
op_s.pop();
int re = calculate(s.top(),l,r);
op_s.push(re);
s.pop();
}
s.push(math[i]);
}
else
{
op_s.push(int( math[ i ] ) - 48 );
}
}
while(!s.empty())
{
int r = op_s.top();
op_s.pop();
int l = op_s.top();
op_s.pop();
int re = calculate(s.top(),l,r);
op_s.push(re);
s.pop();
}
cout <<"Result: " << op_s.top() << endl;
return 0;
}

Find character in a string

I cant get the char search to work. The substring function is working but the char search won't provide the right location of the char it is looking for
#include<iostream>
#include <string>
using namespace std;
int charsearch(string searchInto, char ch, int start = 0)
{
int x = 0;
long n = searchInto.length();
for (int i = 1; i < n; i++)
{
cout << ch;
if (searchInto[i] == ch)
{
i = x;
}
else
i++;
}
cout<< x;
return x;
}
int substr(string src, string tosearch, int start = 0)
{
string searchInto = src;
long n = searchInto.size();
long m = tosearch.size();
int ans = -1;
for (int i = start; i < n; ++i)
{
int p = i;
int q = 0;
bool escape = false;
while (p < n && q < m) {
if (searchInto[p] == tosearch[q]) {
if (tosearch[q] == '/' && !escape) {
++q;
} else {
++p; ++q;
}
escape = false;
} else if (!escape && tosearch[q] == '*') {
++q;
while (q < m && p < n && searchInto[p] != tosearch[q]) ++p;
escape = false;
} else if (!escape && tosearch[q] == '?') {
++p; ++q;
escape = false;
} else if (tosearch[q] == '/' && !escape) {
escape = true;
++q;
} else break;
}
if (q == m) {
return i;
}
if (q == m - 1 && tosearch[q] == '*') {
if (q > 0 && tosearch[q - 1] == '/') continue;
else return i;
}
}
return ans;
}
int main()
{
string searchInto, tosearch;
cout<< "Enter string:";
getline(cin, searchInto);
cout << "Looking for :";
getline(cin, tosearch);
if (tosearch.length() < 2)
{
char ch = tosearch.at(0);
cout << "Found at: " <<charsearch(searchInto, ch) << endl;
cout << "Used Char" << endl;
}
else
cout << "Found at: " <<substr(searchInto, tosearch) << endl;
return 0;
}
To find a character in a string, you have two interfaces.
std::string::find will return the position of a character you find:
auto pos = yourStr.find('h');
char myChar = yourStr[pos];
If the character does not exist, then std::string::npos will be returned as the std::size_t returned for position.
stl algorithm std::find, in header algorithm returns an iterator:
auto it = std::find(yourStr.begin(), yourStr.end(), 'h');
char myChar = *it;
If the character does not exist, then it == yourStr.end().
There are some silly mistakes in your CharSearch method. First of all, You have to break the loop when you got your target character. And most importantly you are not assigning x when you are finding the target. Furthermore, there is extra increment of value i inside the loop. I have modified the function. Please check it below
int charsearch(string searchInto, char ch, int start = 0) {
int x = -1;
long n = searchInto.length();
for (int i = start; i < n; i++)
{
cout << ch;
if (searchInto[i] == ch)
{
x = i; // previously written as i = x which is wrong
break; // loop should break when you find the target
}
}
cout<< x;
return x;
}
Please note that,you can either also use find method of string or std::find of algorithm to search in string.
You need to make changes as per this code
int charsearch(string searchInto, char ch, int start = 0)
{
int x = -1; // : change, if return -1, means not found
long n = searchInto.length();
for (int i = start; i < n; i++) // : change
{
cout << ch;
if (searchInto[i] == ch)
{
x = i; // : change
break; // : change
}
}
cout<< x;
return x;
}
Note : This function will return 1st match.

Why my program terminate calling the KERNEL32!FileTimeToSystemTime ()?

When I debug, the program collapse in the 3rd line of subset().
There are two picture of running the program and debugging...
#include <iostream>
#include <cmath>
#include <cstdio>
#include <stack>
using namespace std;
int c[6];
char s[101];
int end, yes;
stack<char> sstk;
stack<int> nstk;
int toInt(char ch)
{
int temp;
switch(ch)
{
case 'p': temp = c[0];
break;
case 'q': temp = c[1];
break;
case 'r': temp = c[2];
break;
case 's': temp = c[3];
break;
case 't': temp = c[4];
break;
default:
cout<<"Char incorrect :"<<endl;
}
return temp;
}
int oper(int x, int y, char ch)
{
switch(ch)
{
case 'K':
if(1 == x && 1 == y)
return 1;
else
return 0;
case 'A':
if(0 == x && 0 == y)
return 0;
else
return 1;
case 'C':
if(1 == x && 0 == y)
return 0;
else
return 1;
case 'E':
if(x == y)
return 1;
else
return 0;
default:
cout<<"Operation error"<<endl;
return -1;
}
}
bool isOper(char ch)
{
switch(ch)
{
case 'K': case 'A': case 'N': case 'C': case 'E':
return true;
default:
return false;
}
}
int isTau()
{
char a, b, p, t;
int f; //f==1 means the char was asigned
p = s[0]; cout<<"A time p="<<p<<endl;
if(p == 0)
return -1;
if(!isOper(p) )
return 0;
int i = 1;
while((t=s[i++]) != '!')
{ cout<<t<<endl;
if(isOper(t) ) //If the t char is operator, push previous opertator
{
if(1 == f) //and push the a operand if there is one
nstk.push(a);
sstk.push(p);
p = t; //replace the p with t
f = 0;
continue;
}
int num = toInt(t); //If t is not operatand, trans it to int
if(p == 'N') //To operate either there is an N or f==1(a, b, p)
{
b = (num == 1 ? 0 : 1);
}
if(1 == f)
{
b = oper(a, num, p);
}
else
{
a = num;
f = 1;
continue;
}
while(!sstk.empty() )
{
p = sstk.top();
if('N' == p)
{
b = (b == 1 ? 0 : 1);
sstk.pop();
continue;
}
a = nstk.top();
b = oper(a, b, p);
nstk.pop();
sstk.pop();
}
a = b;
f = 0;
}
if(1 == a)
return 1;
else
return 0;
}
void subset(int n,int *A, int cur)
{
for(int i=0; i<cur; i++)
{ cout<<A[i]<<" ";
c[A[i]] = 1;
} cout<<endl;
if(cur != 0)
{
int t = isTau(); //cout<<"Is Tas: "<<t<<endl;
if(t == -1)
{
end = 1;
return;
}
if(t == 0)
yes = 0;
}
int s = cur ? A[cur-1]+1 : 0;
for(int i=s; i<n; i++)
{
A[cur] = i;
subset(n, A, cur+1);
if(end == 1)
return;
}
}
int main()
{
freopen("input.txt","r",stdin);
do
{ cout<<"Loop : "<<endl;
int i;
char t;
for(i=0; (t=getchar() ) != '\n'; i++)
{
s[i] = t;
}
s[i] = '!';
int *a;
subset(5,a,0); cout<<"what?";
if(yes == 1)
cout<<"tautology"<<endl;
else
cout<<"not"<<endl;
}while(end == 0);
return 0;
}
Running
Call stack
int *a;
subset(5,a,0); cout<<"what?";
Here a is defined as a pointer to integer(s) but is never initialized. Then it is passed to subset which attempts to dereference it...
cout<<A[i]<<" ";
...which results in UB (undefined behavior). Everything that happens from there on, including crashing, is a possible outcome of UB.
You can either point a to an allocated buffer before using it:
int *a = new int[5]; // uninitialized, use instead 'new int[5]();' to initialize to all-0
subset(5,a,0); cout<<"what?";
// ...
// remember to 'delete [] a;' when no longer used
Or you can change a to a local array:
int a[5]; // uninitialized, use instead 'int a[5] = { 0 };` to initialize to all-0
subset(5,a,0); cout<<"what?";