s

ddd
Last edited on
*plonk* - no more free food for you hassy
Last edited on
Hello Hasnain Attarwala,

When I loaded your code the first problem I had came from the opening comment. It may have been a copy problem that you missed the opening "/*" of the comment, but it gave the compiler fits when I tried to compile the program.

Something that may help is to write your comment as:
1
2
3
4
/*
FUNCTION:   This program adds and subtracts binary with some flags
********************************************************************
*/

Putting the open and closing "/* */" on their own line helps to see what is to be commented.

After working with the program a bit I changed some parts:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> // io to screen
#include <iomanip>  // <--- Works with "iostream" to manipulate the output. Usually "setw()" and "setprecision".
#include <string>

#include <fstream> //io files

//#include <cstring> //strlen function
//#include <sstream>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/


// Add your function prototypes here
std::string isEven(std::string v);
std::string findTwoscomplement(std::string str);
std::string fullAdder(const std::string&, const std::string&, std::string&, std::string&);

The first group of header I have found that these tend to be the most used in any program. I also found that alphabetical order helps you to remember when you forget a header file. Since "iomanip" works with "iostream" I generally keep them in this order.

The second group of header files would be any header file that the program needs.

The third group is there to show you the header files that you do not need or use in this program.

The comments on using namespace std; should explain that part.

In the prototype for "fullAdder" I changed this. First I passed the first two variables by reference. The advantage here is that you can see the whole string in the function when debugging and second it is easier to pass by reference rather than having to make a copy for the function. Lastly I made the variables "const" so they could not be changed in the function.

When defining variable names try not to use single letter names. This makes the program hard to follow. Since this is a function you could use the variable names "v1" and "v2" because they are local to the function and will not conflict with what you defined in "main" because the scope of the program has changed.

This is just the beginning of the function to show what I changed:
1
2
3
4
5
6
7
8
9
//full adder function
std::string fullAdder(const std::string& v1, const std::string& v2, std::string& inPtr, std::string& outPtr)
{
	size_t len1 = v1.length(), len2 = v2.size();
	size_t maxSize = std::max(len1, len2); // <--- Requires "algorithm" header file.
	std::string result(maxSize + 1,'0');  // <--- Initialize result 
	int s = 0;          // Initialize digit sum 

	for (size_t i = maxSize - 1; i >= 0; i--)

The function parameters show how I changed the first two.

Line 4 shows that "length" and "size" functions return the same value. Eventually I came to using "size" because it returns the same value as "length" and it is shortrt to type.

Line 5 will return the larger of the two "len?" variables or if they are the same size it will return the correct value.

Lines 4 and 5 are just thinking ahead in that "v1" or "v2" may be different lengths. If your program needs "v1" and "v2" to be the same length then you need to check for this back in "main" just after you read the file.

The functions ".length()" and ".size()" both return a "size_t" variable which is usually a typedef for an "unsigned int", but not always the case. You should check your compiler and header file to see how this is defined. Not knowing what IDE you are using I am not sure where to tell where to look. Foe me and my VS2017 IDE it turned out to be in the "vcruntime.h" header file.

The next problem I had was with std::string result;. Defining it this way just creates an empty container with no size. Later on in the code you have result[i] = ('0');. First I do not believe the () around "0" are needed. More importantly is result[i]. If "i" is 3 then it would be result[3], but "result" is empty at this point, so you are trying to write to a memory location that does not exist yet. Line 6 takes care of this problem by initializing the string to "maxSize" and setting each element to '0'. The "+ 1" is because the result could be one larger than you original string.

After that there are problems with the code that produce wrong answers. Given the input strings of "00" and "11" the result should be "11", but it is coming out as "00" which is wrong. Then there is the second two numbers although the string sizes are equal in length the result of addition would be one character larger than the original string length. This part I need to do some more work on.

I hope that helps for now,

Andy
Hello Hasnain Attarwala,

Looking at salem c's response I realized I for got to say anything about your file name.

Your file is just a text file. You can leave off the ".txt", but it is better if you leave it so you know what kind of file you are dealing with.

When I created the file it had the ".txt" extension, so it was easier to change the code than change the file name.

Andy
Hello,
a full adder. That's interesting. I build some with wire and relais long time ago ;)
Now I tried I with std::bitset

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
77
78
79
80
81
82
#include <iostream>
#include <bitset>
#include <cassert>

template<size_t N>
std::bitset<N+1> fullAdder(const std::bitset<N> a, const std::bitset<N> b) {
    assert(a.to_ulong() >= 0);
    assert(b.to_ulong() >= 0);

    std::bitset<N+1> result;
    bool carry = false;
    /*
     A | B | Cin | Cout | S
     0 | 0 |  0  |   0  | 0
     0 | 0 |  1  |   0  | 1
     0 | 1 |  0  |   0  | 1
     0 | 1 |  1  |   1  | 0
     1 | 0 |  0  |   0  | 1
     1 | 0 |  1  |   1  | 0
     1 | 1 |  0  |   1  | 0
     1 | 1 |  1  |   1  | 1 */
    for(size_t i=0; i < a.size(); ++i) {
        if(not a[i]) {
            if(not b[i] and not carry) {
                carry = false;
                result[i] = false;
            } else if(not b[i] and carry) {
                carry = false;
                result[i] = true;
            } else if(b[i] and not carry) {
                carry = false;
                result[i] = true;
            } else if(b[i] and carry) {
                carry = true;
                result[i] = false;
            }
        } else {
            if(not b[i] and not carry) {
                carry = false;
                result[i] = true;
            } else if(not b[i] and carry) {
                carry = true;
                result[i] = false;
            } else if(b[i] and not carry) {
                carry = true;
                result[i] = false;
            } else if(b[i] and carry) {
                carry = true;
                result[i] = true;
            }
        }
    }

    result[N] = carry;

    return result;
}

int main() {
    std::bitset<8> b1(2);
    std::bitset<8> b2(3);

    std::bitset result = fullAdder(b1, b2);
    std::cout << b1.to_ulong() << " + " << b2.to_ulong() << " = " << result.to_ulong() << "\n";


    std::cout << "test all 8bit numbers...\n";
    for(int i=0; i < 256; ++i) {
        for(int j=0; j < 256; ++j) {
            std::bitset<8> b1(i);
            std::bitset<8> b2(j);
            std::bitset result = fullAdder(b1, b2);
            if(result.to_ulong() != i+j) {
                std::cout << i << " + " << j << " wrong\n";
            }

            assert(result.to_ulong() == i+j);
        }
    }
    std::cout << "done\n";
    return 0;
}


And, of course, write less code with more brain power
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    for(size_t i=0; i < a.size(); ++i) {
        if(a[i] and b[i] and carry) {
            carry = true;
            result[i] = true;
        } else if(not a[i] and not b[i] and not carry) {
            carry = false;
            result[i] = false;
        } else if((a[i] and b[i]) or (a[i] and carry) or (b[i] and carry)) {
            carry = true;
            result[i] = false;
        } else {
            carry = false;
            result[i] = true;
        }
}
Last edited on
Damn, the OP sure hates getting help, going so far to report anyone who tried. As well as trying to hide he is cheating his homework.
Last edited on
Well I ain't helping the ingrate any more.
If it helps any, salem, the last time the idiot showed up was January 2018, and 2013 before then.

He's a lie-in-wait ingrate.
2013 to 2019 - not the sharpest tool in the tool box then.
Moss on a wet rock would have made more progress.
Topic archived. No new replies allowed.