SlideShare a Scribd company logo
1 of 8
Download to read offline
I am not able to complete the last function. Could anyone please help?!
data.c
#include
#include
#include
#include
int convertCharToNumber(char ch) {
if (ch >= '0' && ch <= '9') {
return ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
return ch - 'A' + 10;
} else {
return -1;
}
}
char convertNumberToChar(int n) {
if (n >= 0 && n <= 9) {
return n + '0';
} else if (n >= 10 && n <= 15) {
return n - 10 + 'A';
} else {
return 0;
}
}
Data convert_to_base_n(Data src, unsigned char n) {
if (n < 2 || n > 16 || src.data == NULL) {
printf("Invalid input");
exit(1);
}
if (src.base == n) {
printf("Already in base %d. n", n);
return src;
}
Data new_data;
new_data.base = n;
new_data.sign = src.sign;
new_data.number_bits = src.number_bits;
new_data.len = 0;
new_data.data = NULL;
int decimal_num = 0;
DataNode *node_curr = src.data;
int curr_power = src.len - 1;
while (curr_power >= 0 && node_curr != NULL) {
decimal_num = decimal_num + (pow(src.base, curr_power) *
convertCharToNumber(node_curr->number));
node_curr = node_curr->next;
curr_power = curr_power - 1;
}
int num_remain;
DataNode *node_prev = NULL;
while (decimal_num > 0) {
num_remain = decimal_num % n;
decimal_num /= n;
DataNode *node_new = (DataNode*)malloc(sizeof(DataNode));
node_new->number = convertNumberToChar(num_remain);
node_new->next = node_prev;
new_data.len = new_data.len + 1;
new_data.data = node_new;
node_prev = node_new;
}
while (new_data.data != NULL && new_data.data->number == '0') {
new_data.data = new_data.data->next;
}
return new_data;
}
int convert_to_int(Data src) {
int int_num = 0;
DataNode *decimal_num = NULL;
int neg_num = 0;
if (src.sign == 1) {
Data binary_num = convert_to_base_n(src, 2);
if (binary_num.data->number == '1' && binary_num.len == binary_num.number_bits) {
DataNode *node_curr = binary_num.data;
while (node_curr != NULL) {
if (node_curr->number == '0') {
node_curr->number = '1';
}
else {
node_curr->number = '0';
}
node_curr = node_curr->next;
}
decimal_num = convert_to_base_n(binary_num, 10).data;
neg_num = 1;
}
else {
decimal_num = convert_to_base_n(binary_num, 10).data;
}
}
else {
decimal_num = convert_to_base_n(src, 10).data;
}
while (decimal_num != NULL) {
int_num = int_num * 10 + convertCharToNumber(decimal_num->number);
decimal_num = decimal_num->next;
}
if (neg_num == 1) {
int_num = -int_num;
}
return int_num;
}
Data left_shift(Data src, int n) {
if (n == 0) {
return convert_to_base_n(src, 2);
}
Data new_data = convert_to_base_n(src, 2);
if (new_data.len <= n) {
new_data.data = (DataNode*) malloc(sizeof(DataNode));
new_data.sign = src.sign;
new_data.len = 1;
new_data.data->number = '0';
new_data.data->next = NULL;
return new_data;
}
DataNode* node_curr = new_data.data;
DataNode* node_prev = NULL;
while (node_curr != NULL) {
node_prev = node_curr;
node_curr = node_curr->next;
}
int shift_count = n;
while (shift_count >= 1) {
DataNode* node_temp = (DataNode*) malloc(sizeof(DataNode));
node_temp->number = '0';
node_temp->next = NULL;
node_prev->next = node_temp;
node_prev = node_temp;
shift_count = shift_count - 1;
}
node_curr = new_data.data;
for (int i = 1; i <= n; i++) {
node_prev = node_curr;
node_curr = node_curr->next;
free(node_prev);
}
new_data.data = node_curr;
return new_data;
}
Data right_shift(Data src, int n) {
Data new_data;
#ToDo
return new_data;
}
-------------------------------------------------------
data.h
#ifndef __DATA
#define __DATA
typedef struct DataNode DataNode;
typedef struct Data Data;
// Represents a node in the linked list.
struct DataNode {
unsigned char number; // '0' ~ '9' or 'A' ~ 'F'
DataNode *next;
};
// Represents data in base 2 - 16.
struct Data {
unsigned char base; // indicate what base this data is
unsigned char sign; // indicate whether this data is signed or unsigned
// number. 0 means unsigned and 1 means signed.
unsigned char number_bits; // the number of bits can be used to represent
// this number in binary.
// The maximal number of bits is 32.
unsigned char len; // length of linkedList
DataNode *data; // represent data in LinkedList
};
int convertCharToNumber(char ch); // return decimal number of ch in base 2 ~ 16
// return -1 if ch is not a valid number
char convertNumberToChar(
int n); // return char representation of n in base 2 ~ 16
// return '0' if n is not a number of 0 ~ 15
Data convert_to_base_n(
Data src,
unsigned char n); // Return a new Data that represents src in base n
int convert_to_int(Data src); // convert data to an int
Data left_shift(Data src,
int shift); // Return a new Data in base 2 that represents an
// application of binary operator left shift on src
// where shift is less than src.number_bits
Data right_shift(Data src,
int shift); // Return a new Data in base 2 that represents an
// application of binary operator right shift on
// src where shift is less than src.number_bits
#endif
------------------------------------------------------------------
main.c
#include
#include
#include
int main() {
DataNode b0 = {'5', NULL};
DataNode b1 = {'0', &b0};
DataNode b2 = {'2', &b1};
Data d205;
d205.base = 10;
d205.sign = 0;
d205.len = 3;
d205.number_bits = 8;
d205.data = &b2;
Data b11001101 = convert_to_base_n(d205, 2);
printf("The base should be 2, and your base is %dn", b11001101.base);
printf("The sign should not be changed, and your sign is %dn",
b11001101.sign);
printf("The number_bits should not be changed, and your number_bits is %dn",
b11001101.number_bits);
printf("The length should be 8, and your len is %dn", b11001101.len);
printf("The data should be 11001101, and your data is ");
for (DataNode *node = b11001101.data; node; node = node->next) {
printf("%c", node->number);
}
printf("nn");
Data hCD = convert_to_base_n(d205, 16);
printf("The base should be 16, and your base is %dn", hCD.base);
printf("The sign should not be changed, and your sign is %dn", hCD.sign);
printf("The number_bits should not be changed, and your number_bits is %dn",
hCD.number_bits);
printf("The length should be 2, and your len is %dn", hCD.len);
printf("The data should be CD, and your data is ");
for (DataNode *node = hCD.data; node; node = node->next) {
printf("%c", node->number);
}
printf("nn");
Data b00110100 = left_shift(d205, 2);
printf("The base should be 2, and your base is %dn", b00110100.base);
printf("The sign should not be changed, and your sign is %dn",
b00110100.sign);
printf("The number_bits should not be changed, and your number_bits is %dn",
b00110100.number_bits);
printf(
"The length should be 6 since it cannot exceed the number_bit and"
" the first two 0 should not be included in the list.Your len is %dn",
b00110100.len);
printf("The data should be 110100, and your data is ");
for (DataNode *node = b00110100.data; node; node = node->next) {
printf("%c", node->number);
}
printf("n");
printf("We expect 205, and your function returns %dn",
convert_to_int(b11001101));
b11001101.sign = 1;
printf("We expect -51, and your function returns %dn",
convert_to_int(b11001101));
b11001101.number_bits = 16;
printf("We expect 205, and your function returns %dn",
convert_to_int(b11001101));
return 0;
}

More Related Content

Similar to I am not able to complete the last function. Could anyone please hel.pdf

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++Vineeta Garg
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 
Pointer in C
Pointer in CPointer in C
Pointer in Cbipchulabmki
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfAggarwalelectronic18
 
Effective C#
Effective C#Effective C#
Effective C#lantoli
 
Please solve the TODO parts of the following probelm incl.pdf
Please solve the TODO parts of the following probelm  incl.pdfPlease solve the TODO parts of the following probelm  incl.pdf
Please solve the TODO parts of the following probelm incl.pdfaggarwalopticalsco
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSaraswathiRamalingam
 
C++ adt c++ implementations
C++   adt c++ implementationsC++   adt c++ implementations
C++ adt c++ implementationsRex Mwamba
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfmaheshkumar12354
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...AntareepMajumder
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfandreaplotner1
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in CSaket Pathak
 
Help with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfHelp with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfezzi97
 
I am try to create a program that takes a user typed MIPS instructio.pdf
I am try to create a program that takes a user typed MIPS instructio.pdfI am try to create a program that takes a user typed MIPS instructio.pdf
I am try to create a program that takes a user typed MIPS instructio.pdfallystraders
 

Similar to I am not able to complete the last function. Could anyone please hel.pdf (20)

9.C Programming
9.C Programming9.C Programming
9.C Programming
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdfCountryData.cppEDIT THIS ONE#include fstream #include str.pdf
CountryData.cppEDIT THIS ONE#include fstream #include str.pdf
 
Effective C#
Effective C#Effective C#
Effective C#
 
Please solve the TODO parts of the following probelm incl.pdf
Please solve the TODO parts of the following probelm  incl.pdfPlease solve the TODO parts of the following probelm  incl.pdf
Please solve the TODO parts of the following probelm incl.pdf
 
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAMSIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
 
C++ adt c++ implementations
C++   adt c++ implementationsC++   adt c++ implementations
C++ adt c++ implementations
 
7 functions
7  functions7  functions
7 functions
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdf
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
Lab Question
Lab QuestionLab Question
Lab Question
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Help with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdfHelp with the following code1. Rewrite to be contained in a vecto.pdf
Help with the following code1. Rewrite to be contained in a vecto.pdf
 
I am try to create a program that takes a user typed MIPS instructio.pdf
I am try to create a program that takes a user typed MIPS instructio.pdfI am try to create a program that takes a user typed MIPS instructio.pdf
I am try to create a program that takes a user typed MIPS instructio.pdf
 

More from fantoosh1

Identify the step in the innovation process at which the following a.pdf
Identify the step in the innovation process at which the following a.pdfIdentify the step in the innovation process at which the following a.pdf
Identify the step in the innovation process at which the following a.pdffantoosh1
 
If the demand for a product is given by the equation Qd = 100 .pdf
If the demand for a product is given by the equation Qd = 100 .pdfIf the demand for a product is given by the equation Qd = 100 .pdf
If the demand for a product is given by the equation Qd = 100 .pdffantoosh1
 
If the assets of a business are 260000 and the liabilities are 190000 .pdf
If the assets of a business are 260000 and the liabilities are 190000 .pdfIf the assets of a business are 260000 and the liabilities are 190000 .pdf
If the assets of a business are 260000 and the liabilities are 190000 .pdffantoosh1
 
Identify and analyze the factors that influence the level of trust p.pdf
Identify and analyze the factors that influence the level of trust p.pdfIdentify and analyze the factors that influence the level of trust p.pdf
Identify and analyze the factors that influence the level of trust p.pdffantoosh1
 
Identify a true statement about the theory of Symbolic Interactionis.pdf
Identify a true statement about the theory of Symbolic Interactionis.pdfIdentify a true statement about the theory of Symbolic Interactionis.pdf
Identify a true statement about the theory of Symbolic Interactionis.pdffantoosh1
 
Identification of the issue healthcare to all Impact of the issue at a.pdf
Identification of the issue healthcare to all Impact of the issue at a.pdfIdentification of the issue healthcare to all Impact of the issue at a.pdf
Identification of the issue healthcare to all Impact of the issue at a.pdffantoosh1
 
I suggest that the Chocolate Bliss Company expands to somewhere in E.pdf
I suggest that the Chocolate Bliss Company expands to somewhere in E.pdfI suggest that the Chocolate Bliss Company expands to somewhere in E.pdf
I suggest that the Chocolate Bliss Company expands to somewhere in E.pdffantoosh1
 
i want to create an app on powerapps that has 4 buttons as follows o.pdf
i want to create an app on powerapps that has 4 buttons as follows o.pdfi want to create an app on powerapps that has 4 buttons as follows o.pdf
i want to create an app on powerapps that has 4 buttons as follows o.pdffantoosh1
 
I need some help with creating a login page for android studio. This.pdf
I need some help with creating a login page for android studio. This.pdfI need some help with creating a login page for android studio. This.pdf
I need some help with creating a login page for android studio. This.pdffantoosh1
 
I need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdfI need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdffantoosh1
 
I need help with the code pattern Flowers.cpp - This program r.pdf
I need help with the code pattern   Flowers.cpp - This program r.pdfI need help with the code pattern   Flowers.cpp - This program r.pdf
I need help with the code pattern Flowers.cpp - This program r.pdffantoosh1
 
I need help with responding to two people in my week one class discu.pdf
I need help with responding to two people in my week one class discu.pdfI need help with responding to two people in my week one class discu.pdf
I need help with responding to two people in my week one class discu.pdffantoosh1
 
I need help understanding how this equation highlight in blue comes .pdf
I need help understanding how this equation highlight in blue comes .pdfI need help understanding how this equation highlight in blue comes .pdf
I need help understanding how this equation highlight in blue comes .pdffantoosh1
 
I have an assignment which is called Technology project where I will.pdf
I have an assignment which is called Technology project where I will.pdfI have an assignment which is called Technology project where I will.pdf
I have an assignment which is called Technology project where I will.pdffantoosh1
 
In Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdf
In Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdfIn Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdf
In Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdffantoosh1
 
In Edit Mode, reposition the shapes in the True route to match the a.pdf
In Edit Mode, reposition the shapes in the True route to match the a.pdfIn Edit Mode, reposition the shapes in the True route to match the a.pdf
In Edit Mode, reposition the shapes in the True route to match the a.pdffantoosh1
 
In early December, Alice and Bob decided to open the Sample Cafe with .pdf
In early December, Alice and Bob decided to open the Sample Cafe with .pdfIn early December, Alice and Bob decided to open the Sample Cafe with .pdf
In early December, Alice and Bob decided to open the Sample Cafe with .pdffantoosh1
 
In C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdfIn C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdffantoosh1
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdffantoosh1
 
In 2009, Internationale Outstanding University (IOU) had an enrollme.pdf
In 2009, Internationale Outstanding University (IOU) had an enrollme.pdfIn 2009, Internationale Outstanding University (IOU) had an enrollme.pdf
In 2009, Internationale Outstanding University (IOU) had an enrollme.pdffantoosh1
 

More from fantoosh1 (20)

Identify the step in the innovation process at which the following a.pdf
Identify the step in the innovation process at which the following a.pdfIdentify the step in the innovation process at which the following a.pdf
Identify the step in the innovation process at which the following a.pdf
 
If the demand for a product is given by the equation Qd = 100 .pdf
If the demand for a product is given by the equation Qd = 100 .pdfIf the demand for a product is given by the equation Qd = 100 .pdf
If the demand for a product is given by the equation Qd = 100 .pdf
 
If the assets of a business are 260000 and the liabilities are 190000 .pdf
If the assets of a business are 260000 and the liabilities are 190000 .pdfIf the assets of a business are 260000 and the liabilities are 190000 .pdf
If the assets of a business are 260000 and the liabilities are 190000 .pdf
 
Identify and analyze the factors that influence the level of trust p.pdf
Identify and analyze the factors that influence the level of trust p.pdfIdentify and analyze the factors that influence the level of trust p.pdf
Identify and analyze the factors that influence the level of trust p.pdf
 
Identify a true statement about the theory of Symbolic Interactionis.pdf
Identify a true statement about the theory of Symbolic Interactionis.pdfIdentify a true statement about the theory of Symbolic Interactionis.pdf
Identify a true statement about the theory of Symbolic Interactionis.pdf
 
Identification of the issue healthcare to all Impact of the issue at a.pdf
Identification of the issue healthcare to all Impact of the issue at a.pdfIdentification of the issue healthcare to all Impact of the issue at a.pdf
Identification of the issue healthcare to all Impact of the issue at a.pdf
 
I suggest that the Chocolate Bliss Company expands to somewhere in E.pdf
I suggest that the Chocolate Bliss Company expands to somewhere in E.pdfI suggest that the Chocolate Bliss Company expands to somewhere in E.pdf
I suggest that the Chocolate Bliss Company expands to somewhere in E.pdf
 
i want to create an app on powerapps that has 4 buttons as follows o.pdf
i want to create an app on powerapps that has 4 buttons as follows o.pdfi want to create an app on powerapps that has 4 buttons as follows o.pdf
i want to create an app on powerapps that has 4 buttons as follows o.pdf
 
I need some help with creating a login page for android studio. This.pdf
I need some help with creating a login page for android studio. This.pdfI need some help with creating a login page for android studio. This.pdf
I need some help with creating a login page for android studio. This.pdf
 
I need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdfI need help with this code working Create another project and add yo.pdf
I need help with this code working Create another project and add yo.pdf
 
I need help with the code pattern Flowers.cpp - This program r.pdf
I need help with the code pattern   Flowers.cpp - This program r.pdfI need help with the code pattern   Flowers.cpp - This program r.pdf
I need help with the code pattern Flowers.cpp - This program r.pdf
 
I need help with responding to two people in my week one class discu.pdf
I need help with responding to two people in my week one class discu.pdfI need help with responding to two people in my week one class discu.pdf
I need help with responding to two people in my week one class discu.pdf
 
I need help understanding how this equation highlight in blue comes .pdf
I need help understanding how this equation highlight in blue comes .pdfI need help understanding how this equation highlight in blue comes .pdf
I need help understanding how this equation highlight in blue comes .pdf
 
I have an assignment which is called Technology project where I will.pdf
I have an assignment which is called Technology project where I will.pdfI have an assignment which is called Technology project where I will.pdf
I have an assignment which is called Technology project where I will.pdf
 
In Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdf
In Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdfIn Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdf
In Edit Mode, connect the Start Shape to the Decision Shape.Test t.pdf
 
In Edit Mode, reposition the shapes in the True route to match the a.pdf
In Edit Mode, reposition the shapes in the True route to match the a.pdfIn Edit Mode, reposition the shapes in the True route to match the a.pdf
In Edit Mode, reposition the shapes in the True route to match the a.pdf
 
In early December, Alice and Bob decided to open the Sample Cafe with .pdf
In early December, Alice and Bob decided to open the Sample Cafe with .pdfIn early December, Alice and Bob decided to open the Sample Cafe with .pdf
In early December, Alice and Bob decided to open the Sample Cafe with .pdf
 
In C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdfIn C++Add the function min as an abstract function to the classar.pdf
In C++Add the function min as an abstract function to the classar.pdf
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
 
In 2009, Internationale Outstanding University (IOU) had an enrollme.pdf
In 2009, Internationale Outstanding University (IOU) had an enrollme.pdfIn 2009, Internationale Outstanding University (IOU) had an enrollme.pdf
In 2009, Internationale Outstanding University (IOU) had an enrollme.pdf
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

I am not able to complete the last function. Could anyone please hel.pdf

  • 1. I am not able to complete the last function. Could anyone please help?! data.c #include #include #include #include int convertCharToNumber(char ch) { if (ch >= '0' && ch <= '9') { return ch - '0'; } else if (ch >= 'A' && ch <= 'F') { return ch - 'A' + 10; } else { return -1; } } char convertNumberToChar(int n) { if (n >= 0 && n <= 9) { return n + '0'; } else if (n >= 10 && n <= 15) { return n - 10 + 'A'; } else { return 0; } } Data convert_to_base_n(Data src, unsigned char n) { if (n < 2 || n > 16 || src.data == NULL) { printf("Invalid input");
  • 2. exit(1); } if (src.base == n) { printf("Already in base %d. n", n); return src; } Data new_data; new_data.base = n; new_data.sign = src.sign; new_data.number_bits = src.number_bits; new_data.len = 0; new_data.data = NULL; int decimal_num = 0; DataNode *node_curr = src.data; int curr_power = src.len - 1; while (curr_power >= 0 && node_curr != NULL) { decimal_num = decimal_num + (pow(src.base, curr_power) * convertCharToNumber(node_curr->number)); node_curr = node_curr->next; curr_power = curr_power - 1; } int num_remain; DataNode *node_prev = NULL; while (decimal_num > 0) { num_remain = decimal_num % n; decimal_num /= n; DataNode *node_new = (DataNode*)malloc(sizeof(DataNode)); node_new->number = convertNumberToChar(num_remain); node_new->next = node_prev; new_data.len = new_data.len + 1; new_data.data = node_new; node_prev = node_new; } while (new_data.data != NULL && new_data.data->number == '0') { new_data.data = new_data.data->next; }
  • 3. return new_data; } int convert_to_int(Data src) { int int_num = 0; DataNode *decimal_num = NULL; int neg_num = 0; if (src.sign == 1) { Data binary_num = convert_to_base_n(src, 2); if (binary_num.data->number == '1' && binary_num.len == binary_num.number_bits) { DataNode *node_curr = binary_num.data; while (node_curr != NULL) { if (node_curr->number == '0') { node_curr->number = '1'; } else { node_curr->number = '0'; } node_curr = node_curr->next; } decimal_num = convert_to_base_n(binary_num, 10).data; neg_num = 1; } else { decimal_num = convert_to_base_n(binary_num, 10).data; } } else { decimal_num = convert_to_base_n(src, 10).data; } while (decimal_num != NULL) { int_num = int_num * 10 + convertCharToNumber(decimal_num->number); decimal_num = decimal_num->next; } if (neg_num == 1) {
  • 4. int_num = -int_num; } return int_num; } Data left_shift(Data src, int n) { if (n == 0) { return convert_to_base_n(src, 2); } Data new_data = convert_to_base_n(src, 2); if (new_data.len <= n) { new_data.data = (DataNode*) malloc(sizeof(DataNode)); new_data.sign = src.sign; new_data.len = 1; new_data.data->number = '0'; new_data.data->next = NULL; return new_data; } DataNode* node_curr = new_data.data; DataNode* node_prev = NULL; while (node_curr != NULL) { node_prev = node_curr; node_curr = node_curr->next; } int shift_count = n; while (shift_count >= 1) { DataNode* node_temp = (DataNode*) malloc(sizeof(DataNode)); node_temp->number = '0'; node_temp->next = NULL; node_prev->next = node_temp; node_prev = node_temp; shift_count = shift_count - 1; } node_curr = new_data.data; for (int i = 1; i <= n; i++) {
  • 5. node_prev = node_curr; node_curr = node_curr->next; free(node_prev); } new_data.data = node_curr; return new_data; } Data right_shift(Data src, int n) { Data new_data; #ToDo return new_data; } ------------------------------------------------------- data.h #ifndef __DATA #define __DATA typedef struct DataNode DataNode; typedef struct Data Data; // Represents a node in the linked list. struct DataNode { unsigned char number; // '0' ~ '9' or 'A' ~ 'F' DataNode *next; }; // Represents data in base 2 - 16. struct Data { unsigned char base; // indicate what base this data is unsigned char sign; // indicate whether this data is signed or unsigned // number. 0 means unsigned and 1 means signed.
  • 6. unsigned char number_bits; // the number of bits can be used to represent // this number in binary. // The maximal number of bits is 32. unsigned char len; // length of linkedList DataNode *data; // represent data in LinkedList }; int convertCharToNumber(char ch); // return decimal number of ch in base 2 ~ 16 // return -1 if ch is not a valid number char convertNumberToChar( int n); // return char representation of n in base 2 ~ 16 // return '0' if n is not a number of 0 ~ 15 Data convert_to_base_n( Data src, unsigned char n); // Return a new Data that represents src in base n int convert_to_int(Data src); // convert data to an int Data left_shift(Data src, int shift); // Return a new Data in base 2 that represents an // application of binary operator left shift on src // where shift is less than src.number_bits Data right_shift(Data src, int shift); // Return a new Data in base 2 that represents an // application of binary operator right shift on // src where shift is less than src.number_bits #endif ------------------------------------------------------------------ main.c #include #include #include int main() {
  • 7. DataNode b0 = {'5', NULL}; DataNode b1 = {'0', &b0}; DataNode b2 = {'2', &b1}; Data d205; d205.base = 10; d205.sign = 0; d205.len = 3; d205.number_bits = 8; d205.data = &b2; Data b11001101 = convert_to_base_n(d205, 2); printf("The base should be 2, and your base is %dn", b11001101.base); printf("The sign should not be changed, and your sign is %dn", b11001101.sign); printf("The number_bits should not be changed, and your number_bits is %dn", b11001101.number_bits); printf("The length should be 8, and your len is %dn", b11001101.len); printf("The data should be 11001101, and your data is "); for (DataNode *node = b11001101.data; node; node = node->next) { printf("%c", node->number); } printf("nn"); Data hCD = convert_to_base_n(d205, 16); printf("The base should be 16, and your base is %dn", hCD.base); printf("The sign should not be changed, and your sign is %dn", hCD.sign); printf("The number_bits should not be changed, and your number_bits is %dn", hCD.number_bits); printf("The length should be 2, and your len is %dn", hCD.len); printf("The data should be CD, and your data is "); for (DataNode *node = hCD.data; node; node = node->next) { printf("%c", node->number);
  • 8. } printf("nn"); Data b00110100 = left_shift(d205, 2); printf("The base should be 2, and your base is %dn", b00110100.base); printf("The sign should not be changed, and your sign is %dn", b00110100.sign); printf("The number_bits should not be changed, and your number_bits is %dn", b00110100.number_bits); printf( "The length should be 6 since it cannot exceed the number_bit and" " the first two 0 should not be included in the list.Your len is %dn", b00110100.len); printf("The data should be 110100, and your data is "); for (DataNode *node = b00110100.data; node; node = node->next) { printf("%c", node->number); } printf("n"); printf("We expect 205, and your function returns %dn", convert_to_int(b11001101)); b11001101.sign = 1; printf("We expect -51, and your function returns %dn", convert_to_int(b11001101)); b11001101.number_bits = 16; printf("We expect 205, and your function returns %dn", convert_to_int(b11001101)); return 0; }