So this is my first post. I looked everywhere and can't find any help. Here is the problem: Create a 2 dimensional array of integers with 3 rows and 8 columns. Fill this array with 0’s and 1’s (binary numbers) randomly. Each row represents a byte. You are to add these three bytes up in binary. To store your answer, use a single dimensional array of 10 elements (to account for any carry overs). The array binums[3][8] has random 0 and 1’s in it. The array ans[10] has all 0’s in it. Start a variable “carry” with 0, and “total” with 0. Loop through each column, starting with the LAST column and add the carry amount to the total of the column (loop through all three values in the column, adding to total, then add carry). Then, set the LAST element of ans to total % 2, and set carry to total /2 (we are doing integer division). Repeat for each column, going right to left. Set total to 0 before each time.

And this is what I have so far. I'm stuck where I have to try to add the binary numbers from right to left. I've added them normal and cout them so I can get started but now I need to figure out how to add them from right to left. Please help:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

// Declare constants
const int DIM1 = 3;  // number of rows
const int DIM2 = 8;  // number of colums

// Function Prototypes
void fill_Ar(int arr[][DIM2]);
void print_Ar(int arr[][DIM2]);
void add(int binums[][DIM2], int ans[]);
void print_ans(int ans[]);

int main()
{
	srand(time(0));
	int binums[DIM1][DIM2], ans[DIM2 + (DIM1-1)];

	fill_Ar(binums);
	print_Ar(binums);
	add(binums, ans);
	//print_ans(ans); Commented this so i can test my code thus far

	cout << endl;

	return 0;
}

// function definitions below here
void fill_Ar(int arr[][DIM2])
{
	int i,j, num = 0;
	for (i = 0; i < DIM1; i++)
		for (j = 0; j < DIM2; j++)
			arr[i][j] = rand() % 2;
}
void print_Ar(int arr[][DIM2])
{
	int i,j;
	for (i = 0; i < DIM1; i++)
	{
		for (j = 0; j < DIM2; j++)
			cout << arr[i][j] << " ";
		cout << endl;
	}
}
void add(int binums[][DIM2], int ans[])           //THIS IS WHERE I AM STUCK
{
	int total, carry = 0, i, j;
	for (j = 0; j < DIM2; j++)	
	{
		total = 0;
		for (i = 0; i < DIM1; i++)
		{
			total = total + binums[i][j];			
		}
		cout << total << endl;
	}			
	
}
/*****    Commented this so i can test my code thus far
void print_ans(int ans[])
{

}
****/

It looks like you need to know how to do this, but I'm kind of unsure, because it's very simple to do...

for(size_t i = DIM1 - 1; i > -1; i--)
{
  for( size_t j = DIM2 - 1; j > -1; j-- )
   ;//...
}

Wow I apologize about my previous post--It doesn't work as intended due to the unsigned type used.

I will have to revise my implementation.

for( size_t i = DIM1; i > 0; i-- )
{
  for( size_t j = DIM2; j > 0; j-- )
      ;//Use i-1, j-1.
}

Ok I figured out how to start at the end and work forward. Now my problem is putting the info into the array ans[] and cout the info. On line 20 of my code I made ans[] bigger than the column array so that when we are adding the last line of columns plus the carry over there are probably going to have to be 9 spaces for the answer instead of 8. Here is my new code for my add function:

void add(int binums[][DIM2], int ans[])
{
	int total, carry = 0, i, j;
	cout << endl;
	for (j = DIM2-1; j >= 0; j--)	
	{
		total = carry;
		for (i = 0; i < DIM1; i++)
		{
			total = total + binums[i][j];	
			ans[j] = total % 2;
			carry = total / 2;
		}
		cout << ans[j] << endl;
	}				
}

now when this runs it would look something like this:

0 0 1 0 0 0 1 1
0 1 1 1 0 1 1 0
1 0 1 0 1 0 1 0

1 //This is the answer for the last column. Which is correct. But it only goes to
1 8 spaces
0
0
0
0
1
0

What I need to do is have the answer actually go to at least nine spaces for this example and output the answers starting at the right hand side and filling in towards the left as so for the above example:

1 0 1 0 0 0 0 1 1

Notice that the first 1 is missing from my actual output

You mean you're having trouble getting it to output 8 numbers per line?

void add(int binums[][DIM2], int ans[])
{
	const int OUTPUT_MAX_LINE = 8;
	int total = 0,
		carry = 0,
		i = 0,
		j = 0,
		outputCount = 0;

	cout << endl;
	for (j = DIM2-1; j >= 0; j--)
	{
		total = carry;
		for (i = 0; i < DIM1; i++)
		{
			total = total + binums[i][j];	
			ans[j] = total % 2;
			carry = total / 2;
		}
		if( outputCount < OUTPUT_MAX_LINE )
		{
			cout << ans[j] << ' ';
			outputCount++;
		}
		else
		{
			cout << endl << ans[j] << ' ';
			outputCount = 0;
		}
	}
}

It's more that I need at least nine places cause once you start adding from right to left through the columns there will be some carry over and those numbers need to be outputted so what I did was:

void add(int binums[][DIM2], int ans[])
{
	int total, carry = 0, rem, i, j;
	cout << endl;
	for (j = DIM2-1; j >= 0; j--)	
	{
		total = carry;
		for (i = 0; i < DIM1; i++)
		{
			total = total + binums[i][j];	
			ans[0] = total % 2;
			carry = total / 2;
		}
		cout << ans[0] << endl;
	}	
		while (carry != 0)
		{
			ans[0] = carry % 2;
			carry = carry / 2;
			cout << ans[0] << endl;
		}				
}

This gets me what I want but the problem now is that as the answers are figured out they need to be stored in the ans[] array and any unused spaces should be zeroed. Then this ans[] array needs to be past to the print_ans function which should then out put the ans[] array starting from right to left that way the answers line up with fill array. For example if the random array was filled with all ones then the screen with answer included would look like follows:

1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1

1 0 1 1 1 1 1 1 0 1

I mean The answer would look like 0 1 0 1 1 1 1 1 1 0 1

It sounds like the solution would be to pass the size of array along with the pointer to the array's base.

It seems very hard for me to tell exactly what you're having a problem with. So you have information in your add function that you need to pass to your print_ans() function? Could you point out what you're trying to give to the print_ans() function?

Yeah that's basically my problem. I don't think I'm storing the values correctly in the ans[] array then passing them to the print_ans function. The second problem is that once the numbers are passed to the print_ans function I need them to be output so that the first number lines up with the LAST column of the matrix, the second number to line up with the second to LAST column of the matrix and so on. Here's my add and print_ans functions:

void add(int binums[][DIM2], int ans[])
{
	int total, carry = 0, rem, i, j;
	cout << endl;
	for (j = DIM2-1; j >= 0; j--)	
	{
		total = carry;
		for (i = 0; i < DIM1; i++)
		{
			total = total + binums[i][j];	
			ans[0] = total % 2;
			carry = total / 2;
		}
	}	
		while (carry != 0)
		{
			ans[0] = carry % 2;
			carry = carry / 2;
			ans[0]--;				
		}	
}
void print_ans(int ans[])
{
		cout << ans[j] << " ";
}

Finally got it all figured out. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.