I was wondering if someone could help me answer these 4 coding in C problems? Pl
ID: 3805197 • Letter: I
Question
I was wondering if someone could help me answer these 4 coding in C problems? Please.
1. Concatenate Array
Create a function that takes in two arrays and their respective sizes as inputs and concatenates
the second array onto the end of the first array. In particular, you'll need to create a new array
that's size of the two original arrays added together. Make sure to update the first array to hold
the address of the newly concatenated array as well as its size variable to hold the new size.
Write a program that gets two array sizes from the user, makes an array of each size, filling
each array with random integers less than 100, prints the arrays, then concatenates the arrays,
and prints the resulting array. Use the following function header for the concatenate function:
void concat_array(int ** array1, int * size1, int * array2, int size2)
Sample Run:
Enter array1 Size: 2
array1 (0x7fb423c02780):
array[0] = 91
array[1] = 39
Enter array2 Size: 3
array2 (0x7fb423c02790):
array[0] = 68
array[1] = 41
array[2] = 77
array1 (0x7fb423c027a0):
array[0] = 91
array[1] = 39
array[2] = 68
array[3] = 41
array[4] = 77
2. Print Matrix
Trying to have a 2D array parameter in a function in C isn't as simple as in Java. Like with single
dimensional arrays, it's common to actually make the parameter a pointer to a pointer type
rather than an actual array type. Create a function to print a 2D array of characters. Write a
program that asks the user for a height and width and dynamically allocates a 2D array of those
dimensions. Fill the array with dots and then randomly put asterisks in some elements. For the
number of asterisks, it should be 10% of the width times the height cast to an int (for example,
10 * 10 * 0.1 = 10). Then, use the print function on the array. Here's the function header to get
you started:
void print_matrix(char ** matrix, int height, int width)
Sample Run:
Enter Height: 10
Enter Width: 10
Asterisk Count: 10
array (0x7f939d600000):
. . . * . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . * . . . . . .
. . . * . . . * . .
. . . . . . . . . .
. * . . . * . . . .
. . . . . . . * . *
. . * . . . . * . .
. . . . . . . . . .
3. Resize Array
An actual array cannot be resized. However, a dynamically allocated array could seem to be
resized by creating a new array and copying the elements from the original array into the new
array. Your task is to write a function to "resize" an array of integers. Write a program to get an
array size from the user and dynamically allocate an array of that size and fill it with random
integers less than 100 and print the array. Then, get a new size from the user and use the resize
function and print the array. Here's a function header to get you started:
void resize_array(int ** array, int size, int new_size)
Sample Runs:
Enter Size: 3
array (pointing to 0x7fd15fd00000):
array[0] = 37
array[1] = 65
array[2] = 44
Enter New Size: 5
array (pointing to 0x7fd15fd00010):
array[0] = 37
array[1] = 65
array[2] = 44
array[3] = 0
array[4] = 0
Enter Size: 5
array (pointing to 0x7fe766500000):
array[0] = 84
array[1] = 53
array[2] = 25
array[3] = 92
array[4] = 51
Enter New Size: 3
array (pointing to 0x7fe766600000):
array[0] = 84
array[1] = 53
array[2] = 25
4. Print Array
Often a function that's intended to have a parameter of an array type make the parameter of a
pointer type for the array because array variables and pointer variables both hold memory
addresses. Write a function that takes in an array of integers and prints the array. Since arrays
in C don't have a length property like in Java, you'll need to pass in the size of the array as well.
Write a program that gets a number from the user, dynamically allocates an array of doubles of
that size, fills it with random doubles between 0.0 and 1.0 (rounded to five decimal places), and
then uses the print function on that array. Use the following function header for the print
function:
void print_array(double * array, int size)
Note that INT_MAX in limits.h may prove useful in generating random doubles.
Sample Run:
Enter Size: 10
array (0x7fae71c02780):
array[0] = 0.02862
array[1] = 0.03461
array[2] = 0.63858
array[3] = 0.68936
array[4] = 0.99185
array[5] = 0.06751
array[6] = 0.66039
array[7] = 0.11249
array[8] = 0.61413
array[9] = 0.64960
I need help writing these in C code.
Explanation / Answer
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
var arr1 = ['a', 'b', 'c'];
var arr2 = ['d', 'e', 'f'];
var arr3 = arr1.concat(arr2);
Concatenating two arrays:
The following code concatenates two arrays:
var alpha = ['a', 'b', 'c'];
var numeric = [1, 2, 3];
alpha.concat(numeric);
Concatenating three arrays:
The following code concatenates three arrays:
var num1 = [1, 2, 3],
num2 = [4, 5, 6],
num3 = [7, 8, 9];
var nums = num1.concat(num2, num3);
console.log(nums);
Concatenating nested arrays:
The following code concatenates nested arrays and demonstrates retention of references:
var num1 = [[1]];
var num2 = [2, [3]];
var nums = num1.concat(num2);
console.log(nums);
num1[0].push(4);
console.log(nums);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.