Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS CODE IN C ASSIGNMENT: ** I KNOW THIS CODE WORKS, BUT IT NEEDS TO BE MORE

ID: 3854839 • Letter: T

Question

THIS IS CODE IN C

ASSIGNMENT:

** I KNOW THIS CODE WORKS, BUT IT NEEDS TO BE MORE EFFICIENT***

1.What does the function calculate_modified do?

2.What problems/issue DO YOU SEE WITH TTHE CODE?

3. Modified it to BE MORE EFFICIENT .

4.improvements and the benefits?

#include

#include

#include

#include

#include

typedef int int32;

typedef char int8;

static void unite_arrays(int8 *output, int8 *input_1, const int8 *input_2, int32 length)

{

int32 i = 0;

for(i=0; i

{

output[i] = (int8)(input_1[i] ^ input_2[i]);

}

return;

}

static void itoa( int32 num, int8 *alpha, int32 radix )

{

if( radix == 10 )

{

sprintf(alpha, "%i", num);

}

else if( radix == 16 )

{

sprintf(alpha, "%X", num);

}

}

int8 *calculate_modified(int8 modifier, const int8 *input_1, int32 length)

{

int8 leading[3];

int32 i_leading;

int8 * temp_string = NULL;

int8 * ret;

int32 i = 0;

itoa(modifier/2, leading, 10);

//atoi converts string to integer

i_leading = atoi(leading);

temp_string = (int8 *) malloc(8);

//malloc allocates memory block returns to pointer at begginning of block

ret = (int8 *) malloc(length);

//memset fills block of memory

memset(temp_string, 0, 8);

temp_string[0] = 0;

if( (modifier+1)%2 == 0 ) {

temp_string[0] = (int8)((i_leading<<4) + 8);

}

else {

temp_string[0] = (int8)(i_leading<<4);

}

for(i=0; i<(length>>3); i++)

{

unite_arrays(ret+i*8, temp_string, input_1+i*8, 8);

}

free(temp_string);

return ret;

}

// Do not change anything in main

// main only contains example input

// Change anything else

int main(int argc, char **argv)

{

int8 data[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,

   0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,

   0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57};

int8 *resp = calculate_modified(0x10, data, sizeof(data));

free(resp);

return 0;

}

Explanation / Answer

Actual program given in question was not getting complied due to one loop in which condition and increment factor was not added.Please check the loop in unite_arrays method. I have added the same and please find the updated code below.

-----------------------------------------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int int32;
typedef char int8;
static void unite_arrays(int8 *output, int8 *input_1, const int8 *input_2, int32 length)
{
int32 i = 0;
for(i=0; i < length ; i++)
{
output[i] = (int8)(input_1[i] ^ input_2[i]);
printf(" Values %d ",output[i]);
}
return;
}
static void itoa( int32 num, int8 *alpha, int32 radix )
{
if( radix == 10 ){
sprintf(alpha, "%i", num);
//printf(" 10 case------ %s", alpha);
}else if( radix == 16 ){
sprintf(alpha, "%X", num);
//printf(" 16 case ----- %s", alpha);
}
}
int8 *calculate_modified(int8 modifier, const int8 *input_1, int32 length)
{
int8 leading[3];
int32 i_leading;
int8 * temp_string = NULL;
int8 * ret;
int32 i = 0;
printf("%d",modifier);
itoa(modifier/2, leading, 10);
//atoi converts string to integer
i_leading = atoi(leading);
printf("i_leading----%d ",i_leading);
temp_string = (int8 *) malloc(8);
//malloc allocates memory block returns to pointer at begginning of block
ret = (int8 *) malloc(length);
//memset fills block of memory
memset(temp_string, 0, 8);
temp_string[0] = 0;
if( (modifier+1)%2 == 0 ) {
temp_string[0] = (int8)((i_leading<<4) + 8);
}else {
temp_string[0] = (int8)(i_leading<<4);
}
  
for(i=0; i<(length>>3); i++){
printf(" loop run %d ",i);
unite_arrays(ret+i*8, temp_string, input_1+i*8, 8);
}
free(temp_string);
return ret;
}
// Do not change anything in main
// main only contains example input
// Change anything else
int main(int argc, char **argv)
{
int8 data[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57};
int temp = (int)0X10;
printf("%d",temp);
printf("%d",sizeof(data)>>3);
int8 *resp = calculate_modified(0x10, data, sizeof(data));
free(resp);
return 0;
}

--------------------------------- End ----------------------------------------------

Before understanding the above given program we need to understand the below c programming terminology which is being used in our program.
1. (int)0X10 - this statement is written to make use of Hexadecimal number instead of decimal in program.
The above statement converts 10 decimal number to hexadecimal number which is equivalent to 16 ( hexadecinal equl value).
so in our program integer 16 ( hex converted value) will be using.
2. int8 data[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57};
We have 24 length array which holds 24 integer value using concept given in point 1.

3. const int8 *input_1 = it is pointer type varaible which is holding starting address of array and the same is being passed in calculate_modified method.
4. atoi - c statndard fuction used for converting string to integer value. becacuse we are using sprintf fuction
which fruther responsible for storing string buffer.

5. (int8 *) malloc(8) - malloc fuction is used for providing memory space of byte size ( in our case 8 would be the value).
(int8 *) here means conversion of memory space type to int8 explicitly by complier. by default it is void* type ( any type)

6. free - c standard fuction which free the memory space which got occupied by malloc function.

7. length>>3 - >> is right shift operator in c which converts the binary number. let us see the example
suppose i have int value 24 ( actual array size in our case). we binary number equilent to 24 is 00011000. now if we shift 3 binary digit from right then we would have 00000011 ( equilent to 3 ) .
so we can say 24>>3 would be 3 integer value.

Now our program is doing nothing but stroing values in int8 data types which says it is unsigned 8-bit integer type data which meaning to say it accepts the values of 0-255 without overflowing.Now we have array ( data[] ) of 24 length and
we passing this array to method calculate_modified. in calculate_modified pointer type valraible is used to hold the starting address so that we fruther using loop iterate it. input_1+i*8 statement is used for the same
beacuse once we got the satreing address then we can move to another integer value in array by adding 8 byte and so on.

Please run the following above given program and take a look on above given terms then you will find the excatly below given output.
i have added printf( debug statement ) so that you can see each varaible value at each place.
Output
--------------

Conversion example ---  16                                                                                                                                                                                    

Right shift bitwise operator --- 3                                                                                                                                                                            

16i_leading----8                                                                                                                                                                                               

loop run 0                                                                                                                                                                                                    

Values -64                                                                                                                                                                                                    

Values 65                                                                                                                                                                                                     

Values 66                                                                                                                                                                                                     

Values 67                                                                                                                                                                                                     

Values 68                                                                                                                                                                                                     

Values 69                                                                                                                                                                                                     

Values 70                                                                                                                                                                                                     

Values 71                                                                                                                                                                                                     

loop run 1                                                                                                                                                                                                    

Values -56                                                                                                                                                                                                    

Values 73                                                                                                                                                                                                     

Values 74                                                                                                                                                                                                     

Values 75                                                                                                                                                                                                     

Values 76                                                                                                                                                                                                     

Values 77                                                                                                                                                                                                     

Values 78                                                                                                                                                                                                     

Values 79                                                                                                                                                                                                     

loop run 2                                                                                                                                                                                                    

Values -48                                                                                                                                                                                                    

Values 81                                                                                                                                                                                                     

Values 82                                                                                                                                                                                                     

Values 83                                                                                                                                                                                                     

Values 84                                                                                                                                                                                                     

Values 85                                                                                                                                                                                                     

Values 86                                                                                                                                                                                                     

Values 87   

Please let me know you still face any problem to make understanding on above given program.


Dr Jack
Hire Me For All Your Tutoring Needs
Quick quotes • Clear explanations • Study support
Chat Now And Get Quote