Question: In this homework, we re going to implement control structures using goto statements. Even though goto is not something you should use normally in C
In this homework, were going to implement control structures using goto statements. Even though goto is not something you should use normally in C programming, we use it here as a tool to get familiar with unstructured
programs, which will benefit our upcoming assembly language learning.
Copy a String
In this task, you will write a C code to implement a function called copystr that copies all the characters in the src string to dst string. The prototype of the function is declared as follows:
void copystrchar src char dst;
You can assume dst has enough space to store all the characters from src Note, you cannot use any type of structured loops, such as for while and dowhile meaning the only option you can use is goto statement. Of course you can use ifelse structure when needed; however, no statement is allowed in if or else if or else blocks other than a single goto
Requirements
You must not use structured loops mentioned above;
You must not include any other headers;
You must not use any functions provided in string.h especially not strcpy and strlen Note that your code does not need to calculate string length.
Task Calculate Dot Product
In this task, you need to write a C function to calculate the dot product between two vectors. The prototype of the function is declared as follows:
int dotprodchar veca char vecb int length, int sizeelem;
where both veca and vecb are integer vectors that contain length number of integers, and the function will return a single integer as the dot product. sizeelem is the number of bytes of each element in the vectors.
Note that even though both vectors contain integer values, we do not pass int In your implementation, you must not cast the entire vector back to int However, casting one element to int is allowed. As in the previous task, you are not allowed to use loops, so youd have to use goto statements.
Similarly, no statement is allowed in if or else if or else blocks other than a single goto
Requirements
You must not use structured loops mentioned above;
You must not cast the entire array into another type; only casting one address to int at a time is allowed
Task Sorting Nibbles
In this task, youll write a C function to sort all the nibbles bits in an integer array. The prototype of the function is declared as follows:
void sortnibint arr, int length;
where arr is the integer array, while length is the number of integers in that array.
For example, say we have an integer array: int arrxBFDAxCDBA, x One nibble has bits, so each hexadecimal digit represents a nibble. If we treat them as individual numbers and sort them from smallest to largest and print them out as integers, we have: xxxAABBCDDF
You can use any sorting algorithm you like, but do not use functions provided by existing libraries such as qsort
Hints
Take a deep breath before you start
The first step you want to do in your code is to separate all the nibbles in that array. Even though each nibble takes only four bits, you can still store one nibble in a char variable. If the array is like this:
int arrxx you can use bitwise operations and shifting to create an array like this: char nibs Note how many are there in nibs : each integer takes four bytes which is eight nibbles, so you need to make sure leading zeros are also considered in the array;
After sorting array nibs you just need to regroup nibbles back into integers, and replace them back to arr Note the function performs an inplace sorting;
bonus points if you use goto in your code instead of structured loops.
Requirements
You must not use existing libraries to sort;
Write down the sorting algorithm you chose in the comments;
In the comments state if youd like to be graded for bonus points. Without the statement no bonus points will be given.
Starter Code & Tester
You are also provided with a starter code where you can see how to call and test the functions. However, note that passing the demo test in the starter code does not mean your program is entirely correct. You must come up with your own tests, especially edge cases.
Note either you use loops or goto Theres no partial bonus points for using mixed goto and loops.
To help you with testing, we also provided a tester file testermor testerx depending on your machine type Put this tester file in the same directory as your C code, and use the following command to generate an executable:
$ gcc main.c testerm assuming the C file you wrote is called main.c If theres no errors reported, you can go ahead and run the tester:
$ aout h where h is to show you how to use the tester. To test your code, use the following command:
$ aout t copystrdotprodsortnibsall where the name indicates the task you want to test,or all
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
