I need help completing this function. The first picture is the directions for th
ID: 3726723 • Letter: I
Question
I need help completing this function. The first picture is the directions for the parseString function and the second is the kw26-m1.c. Thank you.
This is the kw26.h file if needed
#ifndef __KW26_H
#define __KW26_H
#define MAX40 40
int fib(int n)
{
//base cases: F(0) = 0, F(1) = 1
if (n < 2)
return n;
//definition of Fibonacci: F(n) = F(n - 1) + F(n - 2)
return fib(n - 1) + fib(n - 2);
};
typedef struct Int40
{
// a dynamically allocated array to hold a 40
// digit integer, stored in reverse order
int *digits;
} Int40;
typedef struct kw26RatingStruct{
char *NID; //pointer to a malloc'ed buffer for the NID
float degreeOfDifficulty; //1.0 for super easy to 5.0 for insanity++
float duration; // Hours spent writing, reading,
// designing & building the code
} kw26RatingStruct;
char *cryptoVariableFilename; // for the filename
int seed;//to seed the RNG or not
int nFib; //control the number of Fibonacci numbers to calculate
// F[0] is loaded with the cryptovariable
Int40 *cryptoVariable; // 40 digits used to start the F[x]
// F[1] is loaded with the hwConfigvariable
Int40 *hwConfigVariable; // 40 digits of psuedo or real
// randomness to start the F[x]
// Functional Prototypes
Int40 *kw26Add(Int40 *p, Int40 *q);
Int40 *kw26Destroyer(Int40 *p);
Int40 *fibKw26(int n, Int40 *first, Int40 *second);
void kw26Rating(void);
Int40 *parseString(char *str);
Int40 *loadHWConfigVariable(int doSeed);
Int40 *loadCryptoVariable(char *inputFilename);
Int40 *loadPlainText(char *inputFilename);
Int40 *encrypt(Int40 *key, Int40 *inputText);
#endif
Int40 parseString(char str); Description:Coert a number from string format to Int-40 format. (For example function calls, Special Notes: I the empty string() is passed to this function, eat as a zero ("O"). If any see kw26-ml.c.) dynamic memory allocation functions fail within this fuction, or if str is NULL, return NULL, be careful to avoid memory leaks when you do so. You may assume the string will only contain ASCII digits '0' through 9' and the letters 'A' thru 'F either upper or lower case, for a minimum of 40 digits In the event that 40 digits are not in the input string print an error message to STDERR andl with leading zeroes. Also, if there are more than 40 digits in the input string use the first 40 digits in the string. Returns: A pointer to the newly allocated Int-40 strut, or NULL if dynamic memory allocation fails or if the input str is NULL The subseript of 16 indicate base 16. However the two expressions 106 and 10, are equivalent and used equally oftenExplanation / Answer
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>>
#include "kw26.h"
//print the contents of Int40
void kw26Print(Int40 *p)
{
int i;
if(p==NULL)
{
printf("(null pointer) ");
return;
}printf(" ");
for(i=MAX40-1;i>=0;i--)
{
printf("%x",p->digits[i]);
}
printf(" ");
}
//destroy the memory
Int40 *kw26Destroyer(Int40 *p)
{
if(p!=NULL){
free(p->digits);//free int array
}
return p;//return p
}
//parse string (convert string into Int40 format
Int40 *parseString(char *str)
{
int i;
Int40 *p;
if(str==NULL)
{
return NULL;//if str is NUlL then return NULL
}
p->digits=(int *)malloc(sizeof(int)*MAX40);//allocate memory
if(p->digits==NULL)//if problem in allocating then
{
return NULL;//return NULL
}
int len=strlen(str);//find length of string
//if length is less than 40 then print error message and add leading zeros
if(len<40)
{
fprintf( stderr, "Error ! String containes less than 40 characters" );//print error message
//store the str contents in reverse order
for(i=0;i<len;i++)
{
char ch=str[len-1-i];//reverse
if(ch>='0' && ch<='9')
p->digits[i]=ch-'0';
else if(ch>='A' && ch<='F')
p->digits[i]=ch-'A'+10;//add 10 in case of alphabets cause a-10 and f-15 in case of hexadecimal
else if(ch>='a' && ch<='f')
p->digits[i]=ch-'a'+10;
}
for(i=len;i<MAX40;i++)//add leading zeros
{
p->digits[i]=0;
}
}
else if(len>40)//if length is greater than 40 then print error message
{
fprintf( stderr, "Error ! String containes greater than 40 characters" );
}
if(len>=40)
{
//first 40 digits into array
for(i=0;i<MAX40;i++)
{
char ch=str[MAX40-1-i];
if(ch>='0' && ch<='9')
p->digits[i]=ch-'0';
else if(ch>='A' && ch<='F')
p->digits[i]=ch-'A'+10;
else if(ch>='a' && ch<='f')
p->digits[i]=ch-'a'+10;
}
}
return p;//return p
}
//add two Int40
Int40 *kw26Add(Int40 *p, Int40 *q)
{
int carry=0;//carry
int add,i,digit;
Int40 *result;//to return
result->digits=(int*)malloc(sizeof(int)*40);//allocate
//start calculating
for(i=0;i<MAX40;i++)
{
add=carry+p->digits[i]+p->digits[i];//add both digits[i]
if(add<=15)//if with in 15 then alright
{
digit=add;
carry=0;
}
else{
digit=add-16;//if greater than 15 then digit is add-16
carry=1;//carry is 1
}
result->digits[i]=digit;//assign values
}
return result;
}
int main()
{
Int40 *p;
Int40 *q;
Int40 *r;
printf("hello");
//40 digits
kw26Print(p=parseString("0123456789abcdef0123456789abcdef01234567"));
kw26Destroyer(p);
//<40 digits
kw26Print(p=parseString("354913546879519843519843548943513179"));
kw26Destroyer(p);
//>40 digits
kw26Print(p=parseString("012345678901234567890123456789012345678901234567899999"));
kw26Destroyer(p);
//Null digits
kw26Print(p=parseString(NULL));
kw26Destroyer(p);
//40 digit add
p=parseString("ffffffffffffffffffffffffffffffffffffffff");//40 f's
q=parseString("1");
r=kw26Add(p,q);
kw26Print(p);
kw26Print(q);
kw26Print(r);
//40 digit add
p=parseString("7777777777777777777777777777777777777777");//40 7s
q=parseString("5555555555555555555555555555555555555555");//40 5s
r=kw26Add(p,q);
kw26Print(p);
kw26Print(q);
kw26Print(r);
return 0;
}
Header file:
#ifndef __KW26_H
#define __KW26_H
#define MAX40 40
int fib(int n)
{
//base cases: F(0) = 0, F(1) = 1
if (n < 2)
return n;
//definition of Fibonacci: F(n) = F(n - 1) + F(n - 2)
return fib(n - 1) + fib(n - 2);
};
typedef struct Int40
{
// a dynamically allocated array to hold a 40
// digit integer, stored in reverse order
int *digits;
} Int40;
typedef struct kw26RatingStruct{
char *NID; //pointer to a malloc'ed buffer for the NID
float degreeOfDifficulty; //1.0 for super easy to 5.0 for insanity++
float duration; // Hours spent writing, reading,
// designing & building the code
} kw26RatingStruct;
char *cryptoVariableFilename; // for the filename
int seed;//to seed the RNG or not
int nFib; //control the number of Fibonacci numbers to calculate
// F[0] is loaded with the cryptovariable
Int40 *cryptoVariable; // 40 digits used to start the F[x]
// F[1] is loaded with the hwConfigvariable
Int40 *hwConfigVariable; // 40 digits of psuedo or real
// randomness to start the F[x]
// Functional Prototypes
Int40 *kw26Add(Int40 *p, Int40 *q);
Int40 *kw26Destroyer(Int40 *p);
Int40 *fibKw26(int n, Int40 *first, Int40 *second);
void kw26Rating(void);
Int40 *parseString(char *str);
Int40 *loadHWConfigVariable(int doSeed);
Int40 *loadCryptoVariable(char *inputFilename);
Int40 *loadPlainText(char *inputFilename);
Int40 *encrypt(Int40 *key, Int40 *inputText);
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.