C Programming

profilejamescortez69420
assignment2.pdf

CSE 1320: Intermediate Programming University of Texas at Arlington Fall 2021 Alex Dillhoff

Assignment 2

1. (10%) Create a function swap_pointers which returns void and accepts two point- ers to int. The function should swap the contents of the pointers.

In main, read two integers from the user and call the function using those inte- gers as input. Print the contents of both integers before AND after the call to swap_pointers.

Requirements

(a) Implement swap_pointers as detailed above.

(b) Read integer input from the user in main.

(c) Call the function and print the output as requested.

(d) Format your code consistently.

Example Run

> 32 54 32 54 54 32

2. (30%) Create a program that reads up to 4 strings into individual character arrays. Then, create an array of char * to store the strings. Write a function that, given the char * array and an integer, rotates the strings by that number.

Requirements

(a) Create 4 character array variables of size 128 in main. This value should not be hard coded.

(b) Read up to 4 strings using fgets for each one. If the user enters all 4 or enters an empty string, stop reading.

(c) Store each string that is read into the array of char *.

(d) Create a function which accepts the array of char * and an integer representing the number of places to rotate the strings by. If there are less than 4 strings in the array, do not use the empty indices when rotating. For example, if the user enters 2 strings and rotates by 1, the first string will go from index 0 to index 1 and the second string will go from index 1 back around to index 0.

(e) In main, print the strings in their new order after the call to the rotate function.

(f) Format your code consistently.

Example Run

CSE 1320: Assignment 2 Dillhoff

Enter a string: Hello Enter a string: there Enter a string: everyone Enter a string: 3 strings read. Enter a number: 2 there everyone Hello

3. (30%) Write a function that, given a string and an array of strings, will insert the string based on its length. The strings should be stored in descending order.

In main, read in up to 10 strings from the user, inserting each string into the array. Once the user enters 10 strings or enters an empty string, print the contents of the array.

Requirements

(a) Create a function as described above. The longer strings will come before the shorter ones.

(b) In main, create a 2D array of char which will store the sorted strings.

(c) Read in a string using a single char array.

(d) The maximum size of the strings should be 128. This value should not be hard coded.

(e) In the function, you may need to shift the contents of your array to make room for the new array. Use the strcpy function from string.h to place new strings into the array as well as shift existing strings.

(f) Format your code consistently.

Example Run

Enter a string: first Enter a string: second Enter a string: test Enter a string: rocinante Enter a string: 4 strings read. rocinante second first test

4. (30%) Create a program that converts a value in base 10 to base 7 and vice versa.

Requirements

(a) Create a function short b10_to_b7(short) which converts the input short in base 10 to base 7. The function should return a short representing the base 7 value of the input.

2

CSE 1320: Assignment 2 Dillhoff

(b) Create a function short b7_to_b10(short) which converts the input short in base 7 to base 10. The function should return a short representing the base 10 value of the input.

(c) Your code should be properly formatted to reflect the scope of each statement.

(d) Save your code as base7_convert.c.

Example Run

Enter a number in base 10: 97 Converted to base 7: 166 Converted back to base 10: 97

Create a zip file using the name template LASTNAME_MAVID_A2.zip which includes the all required code files. Submit the zip file through Canvas.

3