How do i Do this function(dealing with arrays)

Joined
Dec 10, 2021
Messages
1
Reaction score
0
cycle instructions, vectors (arrays), matrices (two-dimensional arrays) and functions. Students should develop a top-down approach to building programs by organizing them into roles.
The simplicity of the solutions is valued, which, however, must meet the requirements of the statement. The compilation of programs must not generate warnings, the variable names must describe their purpose and all values read from the keyboard must be validated.
The use of “magic numbers” in the code should be avoided and the definition of constants using the #define directive is recommended.
The student must mention the websites that he consulted when carrying out and implementing the work's questions.
Questions
1. Consider the BIG_INT type, used to perform operations with integers up to 255 digits.
#define MAX_DIGITS 255
typedef unsigned char byte;
typedef byte BIG_INT [MAX_DIGITS + 2];
The array is used to represent the BIG_INT as indicated in Figure 1.
Captura de ecrã 2021-12-08 195846.png
The value present at index 0 indicates the number of digits that BIG_INT contains, at index 1 the sign of the number (1 indicates a positive and 0 a negative number) and from of index 2

1)
/**
* Description:
* The function calculates the multiplication between two BIG_INT and stores the result in the third BIG_INT,
* taking into account the BIG_INT sign
* Parameters:
* b1 - first operand BIG_INT
* b2 - second operand BIG_INT
* bm - BIG_INT which stores the result
* Return:
* The function returns false if there is an overflow, otherwise it returns true.
*/
so far ive done this


bool big_mul( const BIG_INT b1, const BIG_INT b2, BIG_INT bm )
{
BIG_INT baux;
int carry=0;
int r;
for (int i=2;i<b2[0]+1;i++)

{
for(int i=2;i<b1[0]+1;i++)
{
r=b2*b1+carry;
if(r>9)
{
baux=r%10;
carry=r/10;
}

else{
baux=r%10;
carry=0;

}
}

if (big_size (bm)>255)

{

return false;
}
else
{

return true;
}

}
big_add_aux(bm,baux,bm);

return 0;
}

But it doesnt seem to be working, can you help me understand this please!
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Well, it doesn't compile and we're missing a few functions, but I'll do the best I can. Firstly, your for loops are definitely borked; you're using the same variable (i) for each, so it's not going to do what you intend. Each time you're referencing b1, b2, and baux in your loop, you're using the pointer. When you reference b2 and b1, during the actual multiplication, you'll want to use the loop indexes (e.g, b1[k], b2[j]). Same goes when you're writing to baux, but I'm not sure what index you should use. I'm not checking the algorithm, 'cause it's late.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top