Segmentation Fault on Recursion - c++

I want to count the number of recursivily call that has a number in the Collatz Sequence. But for such a bigger number for example 4565458458
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int f(int value){
if(value==1) return 1;
else if(value%2 == 0) return value/2;
else return 3*value+1;
}
int g(int value){
if(value == 0) return 0;
if (f(value)==1) return 1;
return 1 + g(f(value));
}
int main(int argc, char *argv[]){
int nSteps=0;
istringstream iss(argv[1]);
int;
if(!(iss >> num).fail()){
if(num < 0) cout << "0" << endl;
else{
nSteps = g(num);
cout << "Result: " << nSteps << endl;
}
}
else{
cout << "Incorrect line paramaters: ./g n" << endl;
}
return 0;
}

Your program will use a lot of stack-memory for large inputs.
In addition f should have the same input and output type (originally it had "unsigned long long" as input and int as output), or the result will be wrong.
I would advise you to first rewrite g without recursion, and if that works try to investigate how to get g to be efficient with tail-recursion (the current variant does probably not support it).
Using a debugger as others suggested is also good, especially if it crashes before calling 'g'.
Finally 'num<0' does not make sense for an unsigned 'num'.

Related

Having trouble with std::string::compare() return values in c++

Relatively new to c++.
Having trouble understanding an issue I am having with the compare() function returning 1 instead of 0.
I have a program which reads a text file containing an arbitrary number of questions and answers for a quiz. It is formatted as such:
Q: How many days in a week?
A: seven
I have three files, main.cpp, Quiz.cpp, and Quiz.h:
main.cpp:
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include "Quiz.h"
using namespace std;
int main(int argc, char* argv[]){
srand(unsigned(time(0)));
vector<Quiz> quizVector;
ifstream inputQuiz;
inputQuiz.open(argv[1]);
string q, a;
int questionCount = 0;
if(inputQuiz.is_open()){
getline(inputQuiz, q);
getline(inputQuiz, a);
while(!inputQuiz.eof()){
Quiz *instance = new Quiz(q, a);
quizVector.push_back(*instance);
questionCount++;
getline(inputQuiz, q);
getline(inputQuiz, a);
}
}
random_shuffle(quizVector.begin(), quizVector.end());
string userInput;
for(int i = 0; i < questionCount; i++){
cout << quizVector[i].getQuestion() << endl;
cout << "A: ";
getline(cin, userInput);
if(quizVector[i].getAnswer().compare("A: " + userInput) == 0){
cout << "Correct." << endl;
}
else{
cout << "Incorrect." << endl;
}
}
return 0;
}
Quiz.cpp:
#include <string>
#include "Quiz.h"
int Quiz::score = 0;
std::string Quiz::getQuestion(){
return question;
}
std::string Quiz::getAnswer(){
return answer;
}
Quiz.h:
#ifndef QUIZ_H
#define QUIZ_H
class Quiz{
private:
std::string question {""};
std::string answer {""};
public:
Quiz() = default;
Quiz(std::string q, std::string a) : question {q}, answer {a} {}
std::string getQuestion();
std::string getAnswer();
};
#endif
My problem lies within main.cpp:
for(int i = 0; i < questionCount; i++){
cout << quizVector[i].getQuestion() << endl;
cout << "A: ";
getline(cin, userInput);
if(quizVector[i].getAnswer().compare("A: " + userInput) == 0){
cout << "Correct." << endl;
}
else{
cout << "Incorrect." << endl;
}
}
When I input the correct answer corresponding to each question, compare() does not return 0, but consistently returns 1. There are no leading or trailing spaces at the start or ends of each line in the text file. Am I misunderstanding how getline() or compare() works? Is it something else? Any help is appreciated!
I see a number of problems with this code:
std::random_shuffle() is deprecated in C++14 and removed in C++17, use std::shuffle() instead.
you are not validating that argv contains an input parameter before using it.
Your use of eof() in the while loop is wrong. For instance, if the last question/answer pair in the file is terminated by EOF instead of a line break, getline() will still return the question/answer to you, but it will also set the eofbit flag on the stream, which will cause eof() to return true and thus you will skip saving the last pair into the vector. The stream is not technically in a failed state yet in this situation (see the diagram at https://en.cppreference.com/w/cpp/io/basic_ios/eof), so you shouldn't skip the last pair if it terminates with EOF rather than a line break.
Your while loop is leaking memory.
you don't need questionCount at all, use quizVector.size() instead. Or better, a range-for loop.
you don't really need to use compare() at all, you can use operator== instead. But, if you do use compare(), you should take into account that it is case-sensitive (as is operator==). You should also take advantage of the fact that compare() lets you specify an index to start comparing from, so you can ignore the A: prefix in the stored answer (alternatively, you could just strip off the Q: and A: prefixes when storing the question/answer in Quiz's constructor). Otherwise, you can use your compiler's strcmpi() function instead (if it offers one).
Try something more like this instead:
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <random>
#include <cctype>
#include "Quiz.h"
using namespace std;
string toLowercase(string s) {
transform(s.begin(), s.end(), s.begin(),
[](unsigned char c){ return tolower(c); }
);
return s;
}
int main(int argc, char* argv[]){
if (argc < 2){
cerr << "Please specify a file to open!" << endl;
return 0;
}
ifstream inputQuiz(argv[1]);
if (!inputQuiz.is_open()) {
cerr << "Can't open the file!" << endl;
return 0;
}
vector<Quiz> quizVector;
string q, a, userInput;
while (getline(inputQuiz, q) && getline(inputQuiz, a)) {
quizVector.emplace_back(q, a);
}
random_device rd;
mt19937 g(rd());
shuffle(quizVector.begin(), quizVector.end(), g);
for(auto &quiz : quizVector){
cout << quiz.getQuestion() << endl;
cout << "A: ";
getline(cin, userInput);
userInput = toLowercase(userInput);
a = toLowercase(quiz.getAnswer());
if (a == ("a: " + userInput)) {
// or:
// if (a.compare(2, string::npos, userInput) == 0) {
// or, if you strip off "A:" beforehand:
// if (a == userInput) {
cout << "Correct." << endl;
}
else {
cout << "Incorrect." << endl;
}
}
return 0;
}

Binary number function

#include <iostream>
using namespace std;
void binary(unsigned a) {
int i;
cout << "0" << endl;
do {
for (i = 1; i < a; i=i/2) {
if ((a & i) != 0) {
cout << "1" << endl;
}
else {
cout << "0" << endl;
}
}
}
while (1 <= a && a <= 10);
}
int main(void) {
binary(4);
cout << endl;
}
I wrote a code about binary numbers. İt should give bits respect to entering number like for
4 (0100) for 2 (10). However my code goes infinity could you explain. I wrote in visual
studio and I cannot use <bits/stdc++.h> because there is no such a library in visual studio
Initially i is 1 but i = i / 2 sets i to 0, where it remains. The inner loop, therfore, loops for ever.
To output an unsigned number a in binary, use
#include <bitset>
#include <climits>
std::cout << std::bitset<sizeof(a) * CHAR_BIT>(a) << '\n';
(There is, at the time of writing no std::bin i/o manipulator cf. std::hex.)
Without using a built-in function, you can write your own function and perform your operation as follows.
Solution-1
#include <iostream>
void binary(unsigned int number)
{
if (number / 2 != 0) {
binary(number / 2);
}
std::cout << number % 2;
}
int main() {
binary(10);
}
Solution-2
#include <iostream>
#include<string>
void binary(unsigned int number)
{
std::string str = "";
while (number != 0) {
str = (number % 2 == 0 ? "0" : "1") + str;
number /= 2;
}
std::cout << str;
}
int main()
{
binary(4);
}
Note : Don't use using namespace std; . Why is "using namespace std;" considered bad practice?

Make console title bar display the value of a variable

I'm working on a small program that counts up to a number given by the user. The number they enter is stored in the variable limit. I want the number in that variable to be displayed in the title kind of like this: "Counting up to 3000" or "Limit set to 3000" or something like that. I've tried using SetConsoleTitle(limit); and other things but they just don't work. With the code that I have posted bellow, I get the following error:
argument of type "int" is incompatible with parameter of type "LPCWSTR"
I'm currently using Visual Studio 2015 if that's important in any way.
#include <iostream>
#include <windows.h>
#include <stdlib.h>
using namespace std;
int main()
{
begin:
int limit;
cout << "Enter a number you would like to count up to and press any key to start" << endl;
cin >> limit;
SetConsoleTitle(limit); // This is my problem
int x = 0;
while (x >= 0)
{
cout << x << endl;
x++;
if (x == limit)
{
cout << "Reached limit of " << limit << endl;
system("pause");
system("cls");
goto begin;
}
}
system("pause");
return 0;
}
The SetConsoleTitle() function expects a string as its argument, but you're giving it an integer. One possible solution would be to use std::to_wstring() to convert an integer to a wide-character string. C++ string that you get as a result has a different format from the null-terminated wide-character string that SetConsoleTitle() expects, so we need to make the necessary conversion using the c_str() method. So, instead of
SetConsoleTitle(limit);
you should have
SetConsoleTitle(to_wstring(limit).c_str());
Don't forget to #include <string> for to_wstring() to work.
If you want a title that includes more than just a number, you'll need to use a string stream (a wide character string stream in this case):
wstringstream titleStream;
titleStream << "Counting to " << limit << " goes here";
SetConsoleTitle(titleStream.str().c_str());
For string streams to work, #include <sstream>. Here's the full code:
#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
#include <stdlib.h>
using namespace std;
int main()
{
begin:
int limit;
cout << "Enter a number you would like to count up to and press any key to start" << endl;
cin >> limit;
wstringstream titleStream;
titleStream << "Counting to " << limit << " goes here";
SetConsoleTitle(titleStream.str().c_str());
int x = 0;
while (x >= 0)
{
cout << x << endl;
x++;
if (x == limit)
{
cout << "Reached limit of " << limit << endl;
system("pause");
system("cls");
goto begin;
}
}
system("pause");
return 0;
}

Program always producing same value?

the point of my program is to write the numbers 1 - 1,000,000 to a text file, generate a random number between 1 and 1,000,000, search for that line in the text file, take the value, and square it (this is just an exercise for me, it has no practical application). The problem is that whenever I run it, the value remains the same, but the rand() function is seeded by time(0). I suspect that it's a garbage value but I don't know where it's coming from (I have no experience with GDB or any other standalone debuggers). Here's my source code:
#include <fstream>
#include <ctime>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv){
ofstream file("log.txt", ios::app);
ofstream programLog("programlog.dat", ios::app);
cout << "Test Start" << endl;
programLog << "Test Start" << endl;
cout << "Log file created" << endl;
programLog << "Log file created" << endl;
ifstream readFile("log.txt");
int foundNum;
std::string line = "";
unsigned int loopCount = 1000000;
unsigned int numToSearch;
const unsigned short min = 1;
const int max = 1000000;
unsigned int randomLine = 0;
for(int i = 0; i <= loopCount; i++){
file << i << endl;
}
//select random line
srand((unsigned)time(0));
while(!(randomLine > min) && !(randomLine < max)){
randomLine = (unsigned)rand();
programLog << randomLine;
int newlines = 0;
//size_t found;
while(getline(readFile, line)){
if(line.find("\n") != string::npos)
newlines++;
if(newlines == randomLine)
numToSearch = atoi(line.c_str());
}
}
programLog << "Random line selected" << endl;
//read line
while(std::getline(readFile,line)){
if(atoi(line.c_str()) == numToSearch){
foundNum = numToSearch;
break;
}
else
continue;
}
//square it
const unsigned int squared = foundNum*foundNum;
programLog << squared;
readFile.close(); //end read
file.close(); //end log
programLog.close(); //end programlog
return 0;
}
You never enter the while loop as you are using:
while(!(randomLine > min) && !(randomLine < max))
while immediately evaluates to false. You should use:
while(randomLine < min || randomLine > max)
Also, why do all your variables have different types? This could lead to unintended errors. You should change them to have the same type.
randomLineis initialized to 0, and still has that value once it reaches the while, so the loop body never executes.

unsigned int value not giving correct result

following program should print "error" but its printing success.why?
#include<iostream>
using namespace std;
int main()
{
unsigned int a;
a=-10;
if(a == -10)
cout << "success" ;
else
cout << "error" ;
return 0;
}
The conversion for comparison makes them equal again. But it should cause the compiler to emit a warning.