exit from recursive void method in c++ - c++

I have a recursive void method which is written in c++. How can I exit from this recursive method when I get my needed value.
#include<bits/stdc++.h>
using namespace std;
int a[25];
char x[25];
int n, m ;
string s;
void go(int pos)
{
if(pos == n - 1)
{
int suma = a[0];
for(int i = 0 ; i < n - 1;i++)
{
if(x[i] == '+')suma += a[i + 1];
else suma -= a[i + 1];
}
//here I need hint how to close this method when suma will equal to m
//if(suma == x) here I should break this method
return ;
}
x[pos] = '+';
go(pos+1);
x[pos] = '-';
go(pos+1);
}
main()
{
cin >> n >> m;
for(int i = 0 ; i < n;i++)
cin >> a[i];
go(0);
for(int i = 0 ; i < n- 1;i++)
cout << x[i]<<" ";
}
I need to hint how to break void method when my summa will equal to x data everything was declared please do you know some hint for closing this method and I should get value of x arrays.

Throw where you have found your result, and catch at the initial call site
#include <string>
#include <iostream>
int a[25];
char x[25];
int n, m ;
std::string s;
struct result_found{};
void go(int pos)
{
if(pos == n - 1)
{
int suma = a[0];
for(int i = 0; i < n - 1; i++)
{
if(x[i] == '+')suma += a[i + 1];
else suma -= a[i + 1];
}
if (suma == m)
throw result_found{};
}
x[pos] = '+';
go(pos+1);
x[pos] = '-';
go(pos+1);
}
main()
{
std::cin >> n >> m;
for(int i = 0 ; i < n;i++)
std::cin >> a[i];
try {
go(0);
}
catch (result_found&) {}
for(int i = 0 ; i < n- 1;i++)
std::cout << x[i] << " ";
}

You may use an 'static bool' flag on the method.
static fields on functions keep their value between several functions calls.
void go(int pos)
{
static bool found=false;
if (found) return;
//rest of your code here...
//of course when you found what are you searching, do found = true;
}

Related

Why does the code below causes Segmentation Fault (SIGSEGV)?

PROBLEM STATEMENT
You are given a strictly increasing sequence of integers A1,A2,…,AN. Your task is to compress this sequence.
The compressed form of this sequence is a sequence of ranges separated by commas (characters ','). A range is either an integer or a pair of integers separated by three dots (the string "..."). When each range a...b in the compressed form is decompressed into the subsequence (a,a+1,…,b), we should obtain the (comma-separated) sequence A again.
For each maximal contiguous subsequence (a,a+1,…,b) of A such that b≥a+2, the compressed form of A must contain the range a...b; if b≤a+1, such a sequence should not be compressed into a range. A contiguous subsequence is maximal if it cannot be extended by at least one element of A next to it. It can be proved that the compressed form of any sequence is unique (i.e. well-defined).
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
The second line contains N space-separated integers A1,A2,…,AN.
Output
For each test case, print a single line containing one string ― the compressed form of the given sequence.
Constraints
1≤T≤100
1≤N≤100
1 ≤ Ai ≤ 1000 for each valid i
A1 < A2 < …... <AN
Subtasks
Subtask #1 (100 points): Original constraints
Example Input
3
12
1 2 3 5 6 8 9 10 11 12 15 17
4
4 5 7 8
1
4
Example Output
1...3,5,6,8...12,15,17
4,5,7,8
4
MY Code:
#include <bits/stdc++.h>
using namespace std;
bool b[1005];
int a[1005];
int main()
{
int test, i, j, size, count;
cin >> test;
while (test--)
{
for (i = 0; i < 1005; i++)
b[i] = false;
cin >> size;
for (i = 0; i < size; i++)
{
cin >> a[i];
b[a[i]] = true;
}
for (i = 0; i < 1005; i++)
{
if (b[i] == true)
{
cout << i;
j = i;
count = 0;
while (b[j] == true)
{
count++;
j++;
}
if (count > 2)
{
i = j;
if ((j - 1) != a[size - 1])
cout << "..." << i - 1 << ",";
else
cout << "..." << i - 1;
}
if (count == 2)
{
i = j;
if ((j - 1) != a[size - 1])
cout << "," << i - 1 << ",";
else
cout << "," << i - 1;
}
if (count == 1 && ((j - 1) != a[size - 1]))
cout << ",";
}
}
}
return 0;
}
}
MY Question:
Above code runs perfectly on my device giving desired output. But when I am submitting this solution to
Online Judge , it says segmentation fault. It's sure that fundamentally I am accessing the memory incorrectly. Could you please show me where it is?
b is defined a bool[1005]
In this part
for(i=0 ; i<4000 ; i++)
b[i] = false;
You are writing false value 4000 times, exceeding the array size.
Overwriting past the array is allowed on the compiler but will have undefined behaviour in runtime.
In short: it can or can not cause a segfault.
Here is another approach given that the input data is in a file input.txt:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
class Reader {
public:
Reader(const std::string& filename) :
filename_(std::move(filename)), is_(filename_)
{
is_.exceptions( std::ifstream::failbit | std::ifstream::badbit );
}
int get_N() {
int N;
is_ >> N;
return N;
}
std::vector<int> get_ints(int N) {
std::vector<int> v;
v.reserve(N);
for (int i = 0; i < N; i++ ) {
int value;
is_ >> value;
v.push_back(value);
}
return v;
}
int get_num_cases() {
int num_cases;
is_ >> num_cases;
return num_cases;
}
private:
std::string filename_;
std::ifstream is_;
};
bool start_range_cur( std::vector<int> &v, int j, int N )
{
if ( j>= (N - 2) ) return false;
return ((v[j+1] - v[j]) == 1) && ((v[j+2] - v[j+1]) == 1);
}
bool in_range_cur( std::vector<int> &v, int j )
{
return (v[j+1] - v[j]) == 1;
}
void print_range( int min, int max, bool print_comma)
{
std::cout << min << ".." << max;
if (print_comma) std::cout << ",";
}
void print_single(int val, bool print_comma)
{
std::cout << val;
if (print_comma) {
std::cout << ",";
}
}
int main() {
Reader is {"input.txt"};
int num_cases = is.get_num_cases();
for (int i = 0; i < num_cases; i++) {
int N = is.get_N();
std::vector<int> v = is.get_ints(N);
bool in_range = false;
int range_start;
for( int j = 0; j< N; j++ ) {
if (in_range) {
if (j == (N - 1)) {
print_range(range_start, v[j], false);
}
else if (in_range_cur(v, j)) {
continue;
}
else {
print_range(range_start, v[j], true);
in_range = false;
}
}
else {
if (j == (N - 1)) {
print_single(v[j], false);
}
else if (start_range_cur(v, j, N)) {
in_range = true;
range_start = v[j];
}
else {
print_single(v[j], true);
}
}
}
std::cout << '\n';
}
return 0;
}

C++ - Finding x Factor of a math expression

I'm trying to get the x factor of an input math expression, but it seems buggy.
Here is my code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
char a[100];
int res = 0; // final answer
int temp = 0; // factor of the x we are focused on - temporarily
int pn = 0; // power of 10 - used for converting digits to number
int conv; // used for conversion of characters to int
cout<< "Enter a: ";
cin>> a; //input expression
for(int i=0; i<100; i++){
// checking if the character is x - then get the factor
if(a[i]=='x'){
for(int j=1; j<=i+1; j++){
conv = a[i-j] - '0'; // conversion
if(conv>=0 && conv<=9){ // check if the character is a number
temp = temp + conv*pow(10, pn); // temporary factor - using power of 10 to convert digit to number
pn++; // increasing the power
}
else{
if(a[i-j]=='-'){
temp = -temp; // check if the sign is - or +
}
break;
}
if(i-j==0){
break;
}
}
res = res+temp; // adding the x factor to other ones
pn = 0;
temp = 0;
}
}
cout<< res;
return 0;
}
It doesn't work for some inputs, for example:
100x+3x gives 102 and 3x+3997x-4000x gives -1
But works for 130x and 150x!
Is there a problem with my code, or is there an easier way to do this?
I think you're not parsing the +. There may be some other problem I can't see in the original code, probably not. Here's the X-File:
int main(){
char a[100];
int res(0);
cout << "Enter a: ";
cin >> a;
for(int i = 0; i < 100; i++){
if(a[i] == 'x'){
int temp(0);
int pn(0);
for(int j = 1; j <= i; j++){
int conv = a[i - j] - '0';
if(conv >= 0 && conv <= 9){
temp += conv*pow(10, pn);
pn++;
} else if(a[i - j] == '-') {
temp = -temp;
break;
} else if(a[i - j] == '+') {
break;
} else {
// what to do...
}
}
res += temp;
}
}
cout << res;
return 0;
}

Why is my grapher printing multiple instances of the function? C++

I have this program that graphs simple parametric equations on a board of a defined length and width by me. It compiles fine but prints multiple instances of the function in different positions of the graph. If someone could please help me figure out why I am getting this output, I would greatly appreciate it. I included comments throughout the code to help understand what is going on.
I do not have enough reputation to post a picture of the output but if you compile and execute it you will see what I am talking about.
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[M][N]) {
int i, j;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++)
if (i==0) cout << '=';
else if (j==0) cout << '|';
else if (i==N) cout << '=';
else if (j==M) cout << '|';
else if (p[i][j]== '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t,2)/60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[M][N], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i=0; i<=N; i++) {
for (j=0; j<=M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[M][N];
print_board(p);
int t = 0;
while(t <= M) {
cout << string(80, '\n');
next_board(p , t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}
Please help and thank you for all who try!
everywhere in your program where you have
char p[M][N]
change them to
char p[N][M]
and you should get the results that youd expect, your mixing axes in your program
heres the whole working code if youd like
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <time.h>
#include <cmath>
#include <string>
using namespace std;
#define N 25
#define M 60
/*
This function prints the board each time it is called and places an *
in the place corresponding to the value of the function.
*/
void print_board(char p[N][M]) {
int i, j;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++)
if (i == 0) cout << '=';
else if (j == 0) cout << '|';
else if (i == N) cout << '=';
else if (j == M) cout << '|';
else if (p[i][j] == '*') cout << '*';
else cout << ' ';
cout << endl;
}
}
/*
These functions accepts an integer for time and computes a value for x and y
for the parametirc equations given and returns each.
*/
int fx(int t) {
int x = t;
return x;
}
int fy(int t) {
//int y = 5 * sin(0.2 * t) + 15;
int y = (pow(t, 2) / 60) - t + 25;
return y;
}
/*
This function copies the old board and comoputes what the new board is.
*/
void next_board(char p[N][M], int t) {
int i, j;
//copies the old board
int q[M][N];
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
q[i][j] = p[i][j];
}
}
//creates the new board
int x, y;
for (i = 0; i <= N; i++) {
for (j = 0; j <= M; j++) {
x = fx(t);
y = fy(t);
if (i == y && j == x) {
p[i][j] = '*'; //stores an * for the values of x and y
}
}
}
}
int main() {
char p[N][M];
print_board(p);
int t = 0;
while (t <= M) {
cout << string(80, '\n');
next_board(p, t);
print_board(p);
usleep(20000);
t++;
}
return 0;
}

Trying to figure out an issue with corrupted stack while adding int arrays in c++

I am working on visual studio 2013, with a windows8 hp.My code is trying to add two int arrays of size [20] and output the sum. I know I am out or range some where ,but I can't seem to find where. I am the first digit from each array during my convert function and my answer[i] output is only 19 digits when it should be 21digits.
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;
int globalnum[20];
int total[21];
int i;
void convert(char[], int);
void add(int[], int[], int);
void printAnswer(int[], int);
int main()
{
char n1[20];
char n2[20];
int num1[20];
int num2[20];
int answer[21];
cin >> n1 >> n2;
int l1 = strlen(n1);
int l2 = strlen(n2);
int max = fmax(l1, l2);
convert(n1, l1);
for (int i = 0; i < max - 1; i++)
num1[i] = globalnum[i];
for (int i = 0; i < max; i++)
cout << num1[i];
cout << endl;
convert(n2, l2);
for (int i = 0; i < max - 1; i++)
num2[i] = globalnum[i];
for (int i = 0; i < max; i++)
cout << num2[i];
cout << endl;
add(num1, num2, max);
for (int i = 0; i < max - 1; i++)
answer[i] = total[i];
// printAnswer(answer,max);
for (int i = 0; i < max - 1; i++)
cout << answer[i];
return 0;
}
void convert(char c1[], int size)
{
for (int i = 0; i < size - 1; i++)
globalnum[i] = c1[size - 1 - i] - '0';
}
void add(int add1[], int add2[], int s1)
{
int sum[21];
int remain = 0;
for (i = 0; i < s1 - 1; i++)// This starts to add the numbers.
{
sum[i] = (add1[s1 - 1 - i] + add2[s1 - 1 - i] + remain) % 10;
if (add1[s1 - 1 - i] + add2[s1 - 1 - i] + remain >= 10)
remain = 1;
else
remain = 0;
if (remain != 0)
total[s1 - 1 - i] = 1;
else total[s1 - 1 - i] = 0;
total[s1 - 1 - i] = sum[i];
}
if (remain != 0)
total[0] = 1;
}
//void printAnswer(int t[], int b)
// {
// for (int i = b - 1; i < 0; i--)
// cout << t[i];
// }
// cout << endl;
//}
There's too many problems with your code to give a simple answer. It's maybe easier to work backwards from working code. You haven't fully specified the problem but this was my best guess at what you're trying to do:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
vector<int> toInts(const string& s) {
vector<int> v(s.size());
transform(cbegin(s), cend(s), begin(v), [](int c) { return c - '0'; });
return v;
}
int main() {
string a, b;
cin >> a >> b;
auto n = toInts(a);
auto m = toInts(b);
const auto size = max(n.size(), m.size());
n.resize(size);
m.resize(size);
vector<int> sums(size);
transform(cbegin(n), cend(n), cbegin(m), begin(sums), plus<>{});
copy(cbegin(sums), cend(sums), ostream_iterator<int>{cout, ", "});
cout << endl;
}

C++ Error: Line 1440 Expression: string subscript out of range

The program builds and runs, however after entering the first integer and pressing enter then the error pop up box appears, then after pressing ignore and entering the second integer and pressing enter the pop up box appears and after pressing ignore it returns the correct answer. I am at my wits end with this can somebody help me fix the pop up box thing.
#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
#define numbers 100
class largeintegers {
public:
largeintegers();
void
Input();
void
Output();
largeintegers
operator+(largeintegers);
largeintegers
operator-(largeintegers);
largeintegers
operator*(largeintegers);
int
operator==(largeintegers);
private:
int integer[numbers];
int len;
};
void largeintegers::Output() {
int i;
for (i = len - 1; i >= 0; i--)
cout << integer[i];
}
void largeintegers::Input() {
string in;
int i, j, k;
cout << "Enter any number:";
cin >> in;
for (i = 0; in[i] != '\0'; i++)
;
len = i;
k = 0;
for (j = i - 1; j >= 0; j--)
integer[j] = in[k++] - 48;
}
largeintegers::largeintegers() {
for (int i = 0; i < numbers; i++)
integer[i] = 0;
len = numbers - 1;
}
int largeintegers::operator==(largeintegers op2) {
int i;
if (len < op2.len) return -1;
if (op2.len < len) return 1;
for (i = len - 1; i >= 0; i--)
if (integer[i] < op2.integer[i])
return -1;
else if (op2.integer[i] < integer[i]) return 1;
return 0;
}
largeintegers largeintegers::operator+(largeintegers op2) {
largeintegers temp;
int carry = 0;
int c, i;
if (len > op2.len)
c = len;
else
c = op2.len;
for (i = 0; i < c; i++) {
temp.integer[i] = integer[i] + op2.integer[i] + carry;
if (temp.integer[i] > 9) {
temp.integer[i] %= 10;
carry = 1;
} else
carry = 0;
}
if (carry == 1) {
temp.len = c + 1;
if (temp.len >= numbers)
cout << "***OVERFLOW*****\n";
else
temp.integer[i] = carry;
} else
temp.len = c;
return temp;
}
largeintegers largeintegers::operator-(largeintegers op2) {
largeintegers temp;
int c;
if (len > op2.len)
c = len;
else
c = op2.len;
int borrow = 0;
for (int i = c; i >= 0; i--)
if (borrow == 0) {
if (integer[i] >= op2.integer[i])
temp.integer[i] = integer[i] - op2.integer[i];
else {
borrow = 1;
temp.integer[i] = integer[i] + 10 - op2.integer[i];
}
} else {
borrow = 0;
if (integer[i] - 1 >= op2.integer[i])
temp.integer[i] = integer[i] - 1 - op2.integer[i];
else {
borrow = 1;
temp.integer[i] = integer[i] - 1 + 10 - op2.integer[i];
}
}
temp.len = c;
return temp;
}
largeintegers largeintegers::operator*(largeintegers op2) {
largeintegers temp;
int i, j, k, tmp, m = 0;
for (i = 0; i < op2.len; i++) {
k = i;
for (j = 0; j < len; j++) {
tmp = integer[j] * op2.integer[i];
temp.integer[k] = temp.integer[k] + tmp;
temp.integer[k + 1] = temp.integer[k + 1] + temp.integer[k] / 10;
temp.integer[k] %= 10;
k++;
if (k > m) m = k;
}
}
temp.len = m;
if (temp.len > numbers) cout << "***OVERFLOW*****\n";
return temp;
}
using namespace std;
int main() {
int c;
largeintegers num1, num2, result;
num1.Input();
num2.Input();
num1.Output();
cout << " + ";
num2.Output();
result = num1 + num2;
cout << " = ";
result.Output();
cout << "\n\n";
num1.Output();
cout << " - ";
num2.Output();
result = num1 - num2;
cout << " = ";
result.Output();
cout << "\n\n";
num1.Output();
cout << " * ";
num2.Output();
result = num1 * num2;
cout << " = ";
result.Output();
cout << "\n\n";
c = num1 == num2;
num1.Output();
switch (c) {
case -1:
cout << " is less than ";
break;
case 0:
cout << " is equal to ";
break;
case 1:
cout << " is greater than ";
break;
}
num2.Output();
cout << "\n\n";
system("pause");
}
It seems you are falling victim to the difference between C-style strings and C++ strings. C-style strings are a series of chars followed by a zero (or null) byte. C++ strings are objects that contain a series of characters (usually char, but eventually this will be an assumption you should break) and that know their own length. C++ strings can contain null bytes in the middle of themselves without problem.
To loop through all of the characters of a C++-style string, you can do one of a number of things:
You can use the .size() or .length() members of a string variable to find the number of characters in it, as in for (int i=0; i<str.size(); i++) { char c = str[i];
You can use .begin() and .end() to get iterators to the beginning and end of the string, respectively. A for loop in the form for (std::string::iterator it=str.begin(); it!=str.end(); ++it) will loop you through the members of the string by accessing *it.
If you're using C++11, you can use the for loop construct as follows: for (auto c: str) where c will be of the type of a character of the string str.
In the future, to solve problems like these, you can try using the debugger to see what happens when your program crashes or hits an exception. You likely would find that inside of largeintegers::Input() you running into either a memory access violation or some other problem.
Finally, as a future-looking criticism, you should not use C-style arrays (where you say int integer[ numbers ];) in favor of using C++-style containers, such as vector. A vector is a series of objects (such as ints) that can expand as needed.