Question

I have the problem with the code, when I input 0xef1e4 0x5 3, it doesn't execute...

I have the problem with the code, when I input 0xef1e4 0x5 3, it doesn't execute anything.

I get the code from https://www.chegg.com/homework-help/programming-in-c-4th-edition-chapter-11-problem-6e-solution-9780321776419

but it seems error

#include <stdio.h>
int intSize(void)
{
unsigned int n= ~0;
int counter = 4;
while(n>0)
{
n=n>>1;
counter++;
}
return counter-1;//account for sign bit
}
//function to calculate the min number of bits needed to represent a value
int numberOfBits(unsigned int x)
{
int sizeOfInt=intSize();
int counter = 0;
unsigned int temp = 1<<(sizeOfInt - 1);
while((temp | x) != x)
{
temp = temp >>1;
counter++;
}
return sizeOfInt - counter;
}

//function to return the n right most bits
int rightMost(int pattern, int n)
{
int temp, rightMost;
temp = ~0;
temp = temp<<n;
temp = ~temp;
rightMost = pattern &temp;
return rightMost;

}

int bitpat_search(int source, int pattern, int n)
{
int pRightMost, sourceBits, inder, res, index;
pRightMost = rightMost(pattern, n);
sourceBits = numberOfBits(source);
index=(sourceBits - n);
res=-1;
while(index>=0)
{
if((rightMost(source,n)^pRightMost)==0)
res = index;
source =source>>1;
index=index-1;
}
return res;
}
int main(void)
{
int index, source, pattern, n;
printf("\nEnter 3 arguments(source pattern n):");
scanf("%i %i %i, &source,&pattern, &n");
printf("The %i most bits of %i is %i\n", n, pattern, rightMost(pattern, n));
index = bitpat_search(source, pattern, n);
printf("Pattern is at index %i\n", index);
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code provided above with slight modification while taking input now it executes. just modified the scanf statements look for bold text in the code for changed lines

C CODE

#include <stdio.h>
int intSize(void)
{
unsigned int n= ~0;
int counter = 4;
while(n>0)
{
n=n>>1;
counter++;
}
return counter-1;//account for sign bit
}
//function to calculate the min number of bits needed to represent a value
int numberOfBits(unsigned int x)
{
int sizeOfInt=intSize();
int counter = 0;
unsigned int temp = 1<<(sizeOfInt - 1);
while((temp | x) != x)
{
temp = temp >>1;
counter++;
}
return sizeOfInt - counter;
}

//function to return the n right most bits
int rightMost(int pattern, int n)
{
int temp, rightMost;
temp = ~0;
temp = temp<<n;
temp = ~temp;
rightMost = pattern &temp;
return rightMost;

}

int bitpat_search(int source, int pattern, int n)
{
int pRightMost, sourceBits, inder, res, index;
pRightMost = rightMost(pattern, n);
sourceBits = numberOfBits(source);
index=(sourceBits - n);
res=-1;
while(index>=0)
{
if((rightMost(source,n)^pRightMost)==0)
res = index;
source =source>>1;
index=index-1;
}
return res;
}
int main(void)
{
int index, source, pattern, n;
printf("\nEnter source (hexadecimal):");
scanf("%i, &source");
printf("\nEnter pattern (hexadecimal):");
scanf("%i,&pattern");
printf("\nEnter n (hexadecimal):");
scanf("%i,&n");

printf("The %i most bits of %i is %i\n", n, pattern, rightMost(pattern, n));
index = bitpat_search(source, pattern, n);
printf("Pattern is at index %i\n", index);
return 0;
}

OUTPUT OF SCREENSHOT

Enter source (hexadecimal):Oxefle4 Enter pattern (hexadecimal):0x5 Enter n (hexadecimal):0x3 The 1 most bits of is Pattern is

Enter source (hexadecimal):Oxefle4 Enter pattern (hexadecimal):0x5 Enter n (hexadecimal):0x3 The 1 most bits of is Pattern is at index 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - Process exited after 12.38 seconds with return value o Press any key to continue...

Add a comment
Know the answer?
Add Answer to:
I have the problem with the code, when I input 0xef1e4 0x5 3, it doesn't execute...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info...

    /* I want to fix void make_flight(int counter, flight_t flights[]) Enter flight code> Enter departure info for the flight leaving SYD. Enter month, date, hour and minute separated by spaces> Enter arrival city code> Enter arrival info. Enter month, date, hour and minute separated by spaces> It should do all of this: 1-Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most. 2-City - left aligned, MAX_CITYCODE_LEN 3 chars at most . For example( VA1 or LAX ) 3- Month,...

  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT