CMPT125 Homework Assignment 1 solution

$29.99

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

Description

5/5 - (4 votes)

Question 1 [10 points].
Write a function that gets 2 positive ints each between 1 and 999, and outputs the number
obtained by their concatenation.
int concat_ints(int n1, int n2)
For example:
concat_ints(1, 25)should return 125.
concat_ints(123, 456)should return 123456.
concat_ints(999, 9)should return 9999.
Question 2 [10 points].
Implement the function that gets a char and converts it to lower case.
void upper2lowercase(char* c)
If c* is an uppercase letter, the function changes it to the corresponding lowercase.
Otherwise, makes no changes.
For example:
if *c==’D’, the function should change it to ’d’.
if *c==’f’, the function should keep it as ’f’.
if *c==’5’, the function should keep it as ’5’.
if *c==’+’, the function should keep it as ’+’.
[Hint: you can check if the input is an upper case letter by using its numerical value.
The numerical values of the uppercase letters are consecutive.
For example, we have e.g. ‘A’ + 2 == ‘C’ and ‘D’ < ‘E’]
[Fun fact: a similar function tolower(char c)is implemented in the library <ctype.h> ]
https://www.programiz.com/c-programming/library-function/ctype.h/tolower
Question 3 [25 points].
Write a function that gets a string and a non-negative integer n, and performs left rotation on
the string by n symbols.
void left_rotate(char* str, unsigned int n)
Examples,
if str contains the string ”abcdef” and n=2, then after the call str will contain ”cdefab”.
if str contains the string ”abcdeefg123” and n=8, then after the call str will contain
”123abcdeefg”.
if str contains the string ”abcd” and n=0, then after the call str will contain ”abcd”.
if str contains the string ”abc” and n=6, then after the call str will contain ”abc”.
Question 4 [25 points].
Write a function that gets a 2-d array of given dimensions of ints.
It returns true if the array contains two columns with exactly the same values in the same
order, and returns false otherwise.
bool contains_equal_columns(int height, int width, const int
ar[height][width])
Examples:
On input
{{1,2,3,2},
{2,3,4,3},
{1,2,3,2}}
it returns true. (the column (2,3,2) appears twice)
On input
{{1,2,3,4,5,6},
{2,3,4,5,1,2},
{2,3,4,5,0,4},
{3,4,5,6,1,1}}
it returns false.
On input
{{1,1,1,2,4},
{2,2,2,2,1},
{3,2,2,2,1},
{4,4,4,6,3}}
it returns true (the column (1,2,2,4) appears twice)
Question 5 [30 points].
Write a function that gets two strings containing positive integers, and outputs a new string
containing their sum.
char* str_compute_sum(const char* num1, const char* num2)
For example, str_compute_sum(“123456789”,”987654321”)returns “1111111110”.
1. You may assume that the input is always legal, i.e., both strings are positive numbers
2. Note that the numbers may be larger than the maximum of int or long.
3. Also make sure that the returned string is created on the heap (and not as a local variable).