Collatz Conjecture C++ [closed] - c++

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 8 years ago.
Improve this question
I created this function (see below), that works perfectly; however, I was tasked in changing the function to take a parameter that does the same thing just for the function I passed into it.
int collatz(){
int temp, num;
//num = 0;
cout << "pick a number to turn into one:";
cin >> num;
temp = 0;
while(num != 1){
if(num%2 == 0){
num = num/2;
temp++;
}
else if(num&2 != 0){
num = (3*num) + 1;
temp++;
}
}
cout << num << "number of times run: " << temp;
return 0;}
I came up with this; however, it gives me an error:
error C3861: 'collatztwo': identifier not found warning C4554: '&' :
check operator precedence for possible error;
use parentheses to clarify precedence
int collatztwo(int a){
int temp, num;
//num = a;
temp = 0;
while(a != 1){
if(a%2 == 0){
a = a/2;
temp++;
}
else if(a&2 != 0){
a = (3*a) + 1;
temp++;
}
}
cout << "looped: " << temp;
return 0;}

From the errors OP listed in his comment:
error C3861: 'collatztwo': identifier not found
When you made your new function, did you include a prototype for it before the call site? You have to declare int collatztwo(int a); at the top of your source file, or move the function definition before where you use it.
warning C4554: '&' : check operator precedence for possible error; use parentheses to clarify precedence
As already mentioned in the comments, you need to include a pair of parentheses around a&2 or it will do the wrong thing. else if ((a&2) != 0){

Related

If statement still going through when my condition should be false [closed]

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 1 year ago.
Improve this question
I am attempting to write a Caesar encryption program. I have written two functions.
The first (sanitize) allows me to make sure that all strings are fully capitalized, here is the source code.
string sanitize(string message) {
for(int i; i < message.length(); i++){
message[i] = toupper(message[i]);
}
return message;
}
The second (caesar) encrypts the message given. Here is the source code for that as well.
string caesar(string c_message, char direction) {
if (direction = 'R') {
for(int j; j < c_message.length(); j++) {
if((int)c_message[j] + 3 > 90) {
c_message[j] = (char)(64 + (3 - (90 - (int)c_message[j])));
} else {
c_message[j] = (char)((int)c_message[j] + 3);
}
}
} else if (direction = 'L') {
for(int i; i < c_message.length(); i++) {
if((int)c_message[i] - 3 < 65) {
c_message[i] = (char)(91 - (3 - ((int)c_message[i] - 65)));
} else {
c_message[i] = (char)((int)c_message[i] - 3);
}
}
} else {
cout << "directions: 'L' or 'R'" << endl;
}
return c_message;
}
An example of execution :
int main(){
cout << sanitize("HELLO") << " " << (char)3 << endl;
cout << caesar("HELLO", 'L') << endl;
return 0;
}
The first if statement works, but the second does not.
if (direction = 'R') {
} else if (direction = 'L') {
These lines are wrong. = in C++ is an assignment operator and it sets the value of a variable in lefthand to the value of righthand. Then, it is evaluated to the new (righthand) value. Another point is that nonzero values are considered as true when used as condition.
You should use a comparision operator == instead of that like this:
if (direction == 'R') {
} else if (direction == 'L') {

Error in program to find the Armstrong numbers between intervals given by the user [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
Error:
//Count(variable) not declared! error.
But I have declared it. This is a program to calculate the number of Armstrong digits in the interval given by the user. It will continue to run until there is at least one Armstrong number in the interval. I have used a Do-While loop for this purpose.
C++:
#include<iostream>
#include<cmath>
using namespace std;
//Function to check if the number is an Armstrong number
bool Armstrong(int n) {
int number, original, remainder, result = 0, k = 0;
original = n;
//Calculating the number of digits
while (original != 0) {
original /= 10;
++k;
}
original = n;
while (original != 0) {
remainder = original % 10;
result += pow(remainder, k);
original /= 10;
}
if (result == n)
return true;
else
return false;
}
//Checking Armstrong in the interval
int main() {
do {
int start, stop, n, i = 1;
std::cout << "Enter Starting Point: ";
std::cin >> start;
std::cout << "Enter Stop Point: ";
std::cin >> stop;
n = start;
int count = 0; //printing the numbers in the interval
for (; start <= stop; start++) {
if (Armstrong(start)) {
std::cout << "Armstrong Number " << i << " : " << start;
count++;
i++;
}
n--;
}
//It is showing the error here. "Count not Declared"
}
while (count == 0);
}
The problem is that you declare int count; inside the do-while loop, so you can't check for it in the loop condition. Move it to outside of the loop:
int count = 0;
do {
int start, stop, n, i = 1;
...
} while (count == 0);

C++ error returning a string from a function [closed]

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 4 years ago.
Improve this question
I'm trying to return a string from the function solution() but I am getting the error below. Apologies if this is pretty basic but could anybody explain how to return the string. I understand that it is related to pointers.
error: could not convert ‘(std::__cxx11::string*)(& hexaDeciNum)’ from
‘std::__cxx11::string* {aka std::__cxx11::basic_string*}’ to
‘std::__cxx11::string {aka std::__cxx11::basic_string}’
string solution(string &S){
int n = stoi(S);
int answer = 0;
// char array to store hexadecimal number
string hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j].compare("A") ==0 or hexaDeciNum[j].compare("B") ==0 or hexaDeciNum[j].compare("C") ==0 or hexaDeciNum[j].compare("D") ==0 or hexaDeciNum[j].compare("E") ==0 or hexaDeciNum[j].compare("F") ==0 or hexaDeciNum[j].compare("1") ==0 or hexaDeciNum[j].compare("0") ==0 ) {
answer = 1;
}
}
if (answer == 1){
return hexaDeciNum;
}
else {
return "ERROR";
}
}
int main() {
string word = "257";
string answer = solution(word);
return 0;
}
hexaDeciNum is defined as string hexaDeciNum[100]. It is not a string - it is an array of 100 string instances.
You're attempting to return it from a function that should return string.
You should define hexaDeciNum as string hexaDeciNum; instead of string hexaDeciNum[100];. With that way, you can still indexing operator. However you can not use compare method anymore, because each element of string is a char. Instead use operator == like in the following for your piece of code.
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j] == 'A' or hexaDeciNum[j]=='B' or
hexaDeciNum[j] == 'C' or hexaDeciNum[j] == 'D' or
hexaDeciNum[j] == 'E' or hexaDeciNum[j] == 'F' or
hexaDeciNum[j] == '1' or hexaDeciNum[j] == '0' ) {
answer = 1;
}
}
and please don't forget to compile it for c++ 11 with -std=c++11 option of compiler.

C++ Using for loops when you don't know when the iteration would end [closed]

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 5 years ago.
Improve this question
So I have this code for c++ (not c++11)
#include <iostream>
using namespace std;
int calcTrip (string s) {
int ans = 1;
for (int i = 0; i < sizeof(s); i++) {
char c = s[i];
ans = ((ans*(c - 'A' + 1)) );
}
return ans;
}
int main() {
string a1, a2;
cin >> a1 >> a2;
cout << calcTrip(a1) << endl;
if (calcTrip(a1) != calcTrip(a2)) {
cout << "STAY" << endl;
}
else {
cout << "GO" << endl;
}
}
For my for loop in the variable i in calctrip, if I do i<sizeof(s) I get i is less than 32 because a string has a size of 32. Since string s is a user input, how do I make i less than the number of characters in a user input, s.
P.S And I know how to do this in c++ 11, but for class, I need to know how to do it in c++ 98
To iterate over an std::string (or over the any of Standard Containers) in C++98, you can use iterators too:
for(std::string::iterator it = s.begin(); it != s.end(); ++it) {
char c = *it;
ans = ((ans * (c - 'A' + 1)));
}
The range-based for loop is a C++11 feature, but iterators aren't. Another option is to use a simple for loop as follows:
for(int i = 0; i < s.size(); i++) {
char c = s[i];
ans = ((ans * (c - 'A' + 1)));
}
The std::string::size returns the number of characters in the std::string.
sizeof operator returns the sizeof datatype not variable.Using sizeof(s) will return the size of string rather than size of a1...So can use ,
a1.size()
or
a1.length()
to get the length of string variable.

binary search in c++ using 2d array [closed]

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 9 years ago.
Improve this question
Write a loop to ask the user for a gpa and check that it's not negative before doing the search end the loop when the user enters a negative value. When there is a match, print the corresponding student number; when there is no match, print "No match".
I am working on the binary search function. I want to code to check and stop if a negative number is entered and print the student nummber (or "Not found") when a gpa is entered.
void search (double AVGgpa[][NUM_STUDENTS])
{
double gpa;
int first = 0,
last = NUM_STUDENTS - 1,
mid,
position = -1;
int row = 2;
bool found = false; // not found yet
cout << "Enter gpa(-1 to end): ";
cin >> gpa;
if (gpa==-1)
{
cout<<"You enter a nagative value"<<endl;
}
while (!found && first <= last)
{
mid = (first + last) / 2;
if (AVGgpa[row][mid]== gpa)
{
cout << "found at index " << mid << endl;
found = true;
}
else if (AVGgpa[row][mid]> gpa)
last = mid - 1;
else
first = mid + 1;
}
Assuming your data is correct and the array sorted by row, you are probably running into trouble because of float-inacurracy.
Look at the following questions for details: What is the most effective way for float and double comparison?
From that you can use a comparision function like:
bool doubleCompare(double d1, double d2)
{
static const double epsilon = 0.001; // Define this as needed
return std::fabs(d1 - d2) < epsilon;
}
If that does not help you, you should check your input data and/or post more code, since your binary search looks ok to me.