Bool function

closed account (ShqoT05o)
Need help with a bool function in which returns1 (true) if the class instance B is divisible by a base 10 integer x. Otherwise, it returns 0 (false).
using namespace std;

As well a creating a function that coverts the "302" linked list into a base 10 integer, in this case 50.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

using namespace std;

struct Node{
string data;
Node *next;
};

class Base4int{
private:
Node *head;
Node *tail;
public:
Base4int(){
head = NULL;
tail = NULL;
}
void generate(string s){
char n= s.size();
for(int i = 0; i<n ; i++){
Node* newnode = new Node();
newnode->data = s[i];
if(head==NULL){
head = newnode;
newnode->next = NULL;

}
Node* itr = head;
while(itr->next != NULL){
itr = itr->next;

}
itr->next = newnode;
newnode->next = NULL;
}
}

void show(){
Node *itr = head;
while (itr != NULL){
cout << "[" << itr->data << "->" << itr->next << "]";
itr = itr ->next;
}
cout << endl;
}

bool is_divisible_by(string s, int x){
char n= s.size();
if(n < x)
{
return 1;
}
else{
return 0;
}
}

};







int main(){

Base4int B;

B.generate("302");
B.show();
B.is_divisible_by("302",10);
B.show();


}



Last edited on
Hello cplusbeginner2222,

In "main" you have: B.is_divisible_by("302",10);. This calls a function that returns a "bool", but you do not capture the returned value or make use if it.

In your function:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool is_divisible_by(string s, int x)
{
    char n = s.size();

    if (n < x)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

On line 3 you define a "char" and set it to the value of the ".size()" function. The problem is that ".size()" returns a "size_t" variable type. This could be an "unsigned int" or an "unsigned long". It depends on how your header files define "size_t". In the end you are setting a "char" to an "unsigned integer" type.
See https://en.cppreference.com/w/c/types/size_t

In line 5 you are comparing a char to an int. Not a good idea. It may not return the result that you want.

What you need to do is convert the string to an int then use "% 10". if it returns (0)zero than it is dividable be 10 or what ever value "x" is.

"n", "x" cute, but give these variables a proper name that describes what it is. You will be doing your-self a big favor now and in the future. If you make your code easy to understand it helps when looking for errors.

Between the close of the class and the beginning of "main" you have to many blank lines. 1 is sufficient.

Your class is in dire need of some blank lines to make it easier to read.

As an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Base4int
{
    private:  // <--- This section is private by default until changed.
        Node *head;
        Node *tail;

    public:
        Base4int()
        {
            head = NULL;
            tail = NULL;
        }

        void generate(string s)
        {
            char n = s.size();

            for (int i = 0; i < n; i++)
            {
                Node* newnode = new Node();
                newnode->data = s[i];

                if (head == NULL)
                {
                    head = newnode;
                    newnode->next = NULL;

                }

                Node* itr = head;

                while (itr->next != NULL)
                {
                    itr = itr->next;

                }

                itr->next = newnode;

                newnode->next = NULL;
            }
        }

        void show()
        {
            Node *itr = head;

            while (itr != NULL)
            {
                cout << "[" << itr->data << "->" << itr->next << "]";
                itr = itr->next;
            }
            cout << endl;
        }

        bool is_divisible_by(string s, int x)
        {
            char n = s.size();

            if (n < x)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
};

And watch your indenting.

Andy
This corrects various issues with the list code. It will create a list from the chars of the given string and display the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <string>
#include <iostream>

struct Node {
	char data;
	Node* next {};

	Node(char c) : data(c) {}
};

class Base4int
{
	Node* head {};
	//Node* tail {};	//NOT YET USED

public:
	Base4int() {}
	Base4int(const std::string& s) { generate(s); }

	~Base4int() {
		while (head) {
			const auto n {head->next};

			delete head;
			head = n;
		}
	}

	// PROVIDE THESE IF NEEDED
	Base4int(const Base4int&) = delete;
	Base4int& operator=(const Base4int&) = delete;


	void generate(const std::string& s) {
		auto n {s.size()};

		for (size_t i = 0; i < n; ++i) {
			const auto newnode {new Node(s[i])};

			if (head == nullptr)
				head = newnode;
			else {
				auto itr {head};

				for (; itr->next != nullptr; itr = itr->next);
				itr->next = newnode;
			}
		}
	}

	void show() {
		for (auto itr = head; itr != nullptr; itr = itr->next)
			std::cout << itr->data << ' ';

		std::cout << '\n';
	}

	bool is_divisible_by(int x) {
		//TO BE DONE
		return false;
	}
};

int main()
{
	Base4int B("302");

	B.show();

	//B.is_divisible_by(10);
	//B.show();
}



3 0 2

Last edited on
Your Base4Int class has a head and a tail pointer, but you don't use or set the tail.

It looks like Base4int stores each digit as a string. Why? why not store them as ints or chars? My advice is to store the base 4 integer as a vector, not a list. You'll find that sometimes you need to traverse it from least significant digit to most significant, and sometimes the other way. A list will work, but it's a lot of extra work and storage.

Also, your class has no destructor so it leaks the Nodes. Using a vector would solve this, but so would using a std::list.

How is is_divisible_by() supposed to work? I'm not talking about the code, I mean the algorithm. And what is the string that you're passing in? It makes more intuitive sense to see if the number represented by *this is divisible by x:

1
2
// return true if this number is divisible by x
bool is_divisible_by(int x);
closed account (ShqoT05o)
I need the algorithm to check if "302" which is base 4 integer to be divisible by a base 10 integer.
IF that is what you need, then consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>

int main()
{
	std::string b4;
	int b10 {};

	std::cout << "Enter a b4 number: ";
	std::cin >> b4;

	const auto num {std::stoi(b4, nullptr, 4)};

	std::cout << "Enter a b10 number :";
	std::cin >> b10;

	std::cout << b4 << " is " << num << '\n';

	if (num % b10)
		std::cout << "It is not divisible by " << b10 << '\n';
	else
		std::cout << "It is divisible by " << b10 << '\n';
}

Topic archived. No new replies allowed.