CMPT 125  Assignment 1 solved

$30.00

Original Work ?
Categories: , Tags: , You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

Question 1 [5 marks]
Write a function that replaces a specific digit in an int number with another digit and returns the result as
an int. Return the same number if the target/replacement character is not a digit. Use this function header:
int replaceDigits(int number, char target, char replacement)
For example:
replaceDigits(1, ‘1’, ‘2’) should return 2
replaceDigits(-232, ‘3’, ‘0’) should return -202
replaceDigits(123, ‘4’, ‘5’) should return 123
replaceDigits(1223, ‘!’, ‘?’) should return 1223
replaceDigits(5670, ‘5’, ‘0’) should return 670 (leading zero will not be part of the resulting number)
replaceDigits(30400, ‘0’, ‘9’) should return 39499

You can assume the number does not have leading zeros (e.g., we will not call replaceDigits(01, ‘1’, ‘2’)),
except when the number is actually zero (i.e., we might call replaceDigits(0, ‘1’, ‘1’)).
Only include the a1_question.h header file and the function definition (and your helper functions, if any)
in the source file and name it as a1_question1.c. Do not use recursion in your answer.

Question 2 [5 marks]
Write a function that takes in 4 parameters: an int array, its size, the left index, and the right index; and
checks if the elements between the left and right index (inclusive) in the int array are sorted in ascending
order.

If the left index is larger than the right index, swap them first. Then, if the left index is invalid (e.g.,
negative, larger than the size), use the leftmost valid index of the array; if the right index is invalid, use
the rightmost valid index of the array. Use this function header:

bool rangedCheckforSorted(int array[], unsigned int size, int leftIndex, int rightIndex)
For example (suppose there is a myIntArray created as [-4, 3, -12, 0, 5, 72, 88, 128, 1, 64]):
rangedCheckforSorted(myIntArray, 10, -2, 3) will check indexes between 0 and 3, and return false
rangedCheckforSorted(myIntArray, 10, 7, 2) will check indexes between 2 and 7, and return true
rangedCheckforSorted(myIntArray, 10, 5, 5) will check indexes between 5 and 5, and return true
rangedCheckforSorted(myIntArray, 10, 0, 9) will check indexes between 0 and 9, and return false
You can assume the array has one or more elements and the size is always correct. Content of the int
array does not change after calling this function.

Only include the a1_question2.h header file, <stdbool.h>, and the function definition (and your helper
functions, if any) in the source file and name it as a1_question2.c. Do not use recursion in your answer.

Question 3 [6 marks]
In C arrays can be accessed using pointers. As an exercise, write a function that reverses the polarity of
every element in a 2D int array (i.e., -1 becomes 1, 46 becomes -46, …etc.) using only pointers. That is,
your code must not use any [ ], which is the non-pointer version of array access. Use this function header:
void reversePolarity(unsigned int row, unsigned int col, int** array)

For example, suppose we have a 2-by-3 array my2DIntArray with the content [[0, 1, -2], [-999, 2345, 678]].
Then the function will be called as (the array needs to be created differently, see the test file):
reversePolarity(2, 3, my2DIntArray)
And when the function returns, the content of my2DIntArray becomes [[0, -1, 2], [999, -2345, -678]].
You can assume that the row & col parameters always correctly indicate the number of rows & columns
of the array. Do not use [ ] anywhere in your answer (if you do you get 0 for this question).
Only include the a1_question3.h header file and the function definition (and your helper functions, if any)
in the source file and name it as a1_question3.c.

Coding Style [4 marks]
Your program should be properly indented, have clear and meaningful variable names (e.g., no singleletter variable names except loop iterators) and enough white space and comments to make it easy to
read. Named constants should be used where appropriate. Each line of code should not exceed 80
characters. White space should be used in a consistent manner. Remember to include your information.
To help you to get into the habit of good coding style, we will read your code and marks will be deducted
if your code is not styled properly.

Before You Submit
Before submitting your assignment, make sure you have test-run your code in our CSIL machines using
the steps covered in class: open your assignment folder in VS Code, use the gcc command with options
in the terminal to compile your code, and run the executable. We will use these steps when marking
your submission and will deduct marks if your code does not compile/run – to maintain fairness we do
not accept reasons like “but it worked on my computer” or “but it ran just fine in my IDE”.

The Makefile provided in this assignment is used by a command in the CSIL machines called “make” to
quickly compile your code. It is especially useful if you have multiple source files. To use it, type the
following command in the prompt (make sure you are in the directory with all the files of Assignment 1):
$ make test1

The example above illustrates how Question 1 is compiled into an executable called “test1” when using
the Makefile. Replace the “test1” with “test2”, “test3”, …etc. for other questions. You can then run the
executable by typing ./test1 to test your code for Question 1. If you make changes to your code, use the
make command again. You can also use make all if you want to compile all your code at once.
The test files (test1.c, test2.c, …etc.) are provided in this assignment for you to test your code. Each
typically contains a main function along with other tester functions and/or calls. You can modify them to
further test your code, but do not submit these test files because we will be using our test files that are
similar but not identical to grade your assignment. This makes sure that your code is not written to
produce hard-coded output.

The header files (question1.h, question2.h, …etc.) are there to make the compilation work. You can look
at them but do not modify them. You also do not have to submit them.

Submission
Submit only the 3 source files indicated above (a1_question1.c, a1_question2.c, a1_question3.c) to
CourSys. Refer to the corresponding Canvas assignment entry for details.
Assignment late penalty: 10% per calendar day (each 0 to 24 hour period past due), max 2 days late.