Question
//*PLEASE ANSWER AS QUICKLY AS POSSIBLE* //This is my intro to c programming class, instructions & problems below #include #include #include #include /*HELPER CODE YOU
//*PLEASE ANSWER AS QUICKLY AS POSSIBLE*
//This is my intro to c programming class, instructions & problems below
#include
#include
#include
#include
/*HELPER CODE YOU DO NOT NEED TO CHANGE ANY OF THIS */
typedef struct {
char * pNames;
int iPhoneNum;
}PhoneEntries;
struct Node{
int phone;
struct Node *pNext;
};
typedef struct Node node;
node * createnode(node *pNode, int num)
{
pNode = (node*)malloc(1*sizeof(node));
pNode->pNext = 0;
pNode->phone = num;
return pNode;
}
node* addnodetostart(node *pRoot, int num)
{
node *pTemp;
pTemp = (node*)malloc(1*sizeof(node));
pTemp->phone = num;
pTemp->pNext = pRoot;
return pTemp;
}
void deletelist(node *pRoot)
{
while(pRoot!=0)
{
node * pTemp = pRoot;
pRoot = pTemp->pNext;
free(pTemp);
}
}
int binary(unsigned char num)
{
if (num == 0)
{
return 0;
}
else
{
return (num % 2) + 10 * binary(num / 2);
}
}
/*END HELPER CODE */
/*FUNCTIONS YOU HAVE TO WRITE. YOU MAY USE THE DEBUGGER AND THE MAN COMMAND. OTHERWISE CLOSED BOOK CLOSED NOTES */
/*WRITE A FUNCTION THAT COMPARES TWO STRINGS USING POINTER NOTATION. IT SHOULD RETURN AN INT. IF THE TWO STRINGS ARE THE SAME RETURN ZERO, OTHERWISE RETURN 1 or -1 BASED ON IF A COMES BEFORE B IN ALPHABETICAL ORDER(ie return the same thing that strcmp does) */
int comparestrings(const char * str1, const char *str2)
{
return 0;
}
/*WRITE A FUNCTION THAT SETS THE I-TH BIT OF A CHAR TO THE GIVEN VALUE*/
void setbit(unsigned char byte, int i, bool oneorzero)
{
}
/*WRITE A FUNCTION THAT PRINTS OUT THE CONTENTS OF A LINKED LIST, GIVEN THE ROOT NODE */
void printlist(node * pNode)
{
}
/*WRITE A COMPARE FUNCTION FOR PHONEBOOK FOR QSORT. YOU MAY USE YOUR PREVIOUSLY WRITTEN COMPARESTRINGS FUNCTION. IF YOU WERE UNABLE TO WRITE THIS YOU MAY YOU STRCMP */
int compare(const void * a, const void *b)
{
}
/*WRITE A FUNCTION THAT MALLOCS 1 PHONE BOOK, INITALIZES IT WITH A DEEP COPY OF THE GIVEN STRING, THEN REALLOCS TO 10 PHONE BOOKS, SETTING EACH OF THEM TO THE SAME DEFAULT NAME. YOU MAY USE STRCPY*/
PhoneEntries * initbook(char * defaultstring)
{
PhoneEntries * pBook;
int i = 0;
/*UNCOMMENT THIS CODE OUT AFTER YOU FINISH. IT SHOULD PRINT OUT TEST NAME TEN TIMES.
for( i =0; i < 10; i++)
printf("Phone entries %s ", pBook[i].pNames);*/
return pBook;
}
/*WRITE A FUNCTION THAT FREES THE PHONEBOOKS FROM THE PREVIOUS PROBLEM */
void freebooks(PhoneEntries* pBook)
{
}
/*WRITE A RECURISVE FUNCTION TO SEE IF A GIVEN NUMBER APPEARS IN A SORTED LIST OF INTEGERS. YOU WILL WANT TO SPLIT THE LIST INTO EQUAL TWO PARTS AND RECURE */
bool recursivesearch( int * array, int startofarray, int endofarray, int numbtosearchfor)
{
return false;
}
/*END FUNCTIONS YOU HAVE TO WRITE */
int main()
{
/*YOU DO NOT HAVE TO CHANGE THIS CODE */
int array[] = {1 ,2, 2, 3, 4, 5, 6 ,6, 7, 9, 10, 12};
char bitchange = 'H';
char str1[] = "Sloths are great"; /*Correct opinion */
char str2[] = "Sloths are not great";
int i = 0;
PhoneEntries *pPhoneBook;
PhoneEntries *pPhoneBook2;
node * pRoot;
printf("STRING COMPARE PROBLEM ");
if(!comparestrings(str1,str2))
printf("STRINGS ARE EQUAL, THIS IS A BUG ");
else
printf("STRINGS ARE NOT EQUAL ");
printf("BIT FLIP PROBLEM ");
printf("Bits before change %d ", binary(bitchange));
setbit(bitchange,1,1);
setbit(bitchange,3,0);
printf("Bits after change %d , should be 1000010 ", binary(bitchange));
pRoot = createnode(pRoot,50);
pRoot = addnodetostart(pRoot, 100);
pRoot = addnodetostart(pRoot, 160);
pRoot = addnodetostart(pRoot, 260);
printf("LINKED LIST PROBLEM ");
printlist(pRoot);
deletelist(pRoot);
pPhoneBook = (PhoneEntries*)malloc(sizeof(PhoneEntries)*5);
pPhoneBook[0].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[1].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[2].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[3].pNames = (char*)malloc(sizeof(char)*20);
pPhoneBook[4].pNames = (char*)malloc(sizeof(char)*20);
strcpy(pPhoneBook[0].pNames,"Ants");
pPhoneBook[0].iPhoneNum = 100;
strcpy(pPhoneBook[1].pNames,"Python");
pPhoneBook[1].iPhoneNum = 200;
strcpy(pPhoneBook[2].pNames,"Aardvark");
pPhoneBook[2].iPhoneNum = 300;
strcpy(pPhoneBook[3].pNames,"Penguin");
pPhoneBook[3].iPhoneNum = 400;
strcpy(pPhoneBook[4].pNames,"Cats");
pPhoneBook[4].iPhoneNum = 500;
printf("PHONE BOOK PROBLEM ");
printf("Sorted phone book ");
qsort(pPhoneBook,5,sizeof(pPhoneBook),compare);
for(i=0; i < 5; i++)
{
printf("Name %s ", pPhoneBook[i].pNames);
printf("Phone %d ", pPhoneBook[i].iPhoneNum);
free(pPhoneBook[i].pNames);
}
free(pPhoneBook);
printf("RECURSIVE SORT PROBLEM ");
if(recursivesearch(array,0,12,3)==true)
printf("Three appeared in the array! This is the correct result! ");
else
printf("Three did not appear in the array. Since its there, this is a bug ");
printf("MEMORY ALLOCATION PROBLEM ");
pPhoneBook2 = initbook("TEST NAME");
freebooks(pPhoneBook2);
/* END OF PROVIDED CODE THAT YOU DO NOT HAVE TO CHANGE*/
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started