Starting from:

$25

CH-230-A-Assignment 5 Solved


Assignment 5 - Arrays, Dynamic Memory Allocation, Recursion

•    The problems of this assignment must be solved in C or C++ (instruction in each problem).

 Language: C
Write a function that takes two arguments: an integer n and a character ch. The function should print the character ch in a triangle form as below.

Write a simple program that reads the appropriate variables and prints the result to screen by calling the function.

You can safely assume that the input will be valid.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

 Testcase 5.1: inputTestcase 5.1: output
4$$$$

$$$$

$$

$

5.5,6.5,7.75,8.0,9.6,10.36.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

 Testcase 5.2: inputTestcase 5.2: output
Before:

5.500 6.500 7.750 8.000 9.600 10.360 After:

1.100 1.300 1.550 1.600 1.9206 2.072


You can assume that the string will be not longer than 50 characters and will be valid.

Problem 5.4 Divide with input data

Language: C
Modify your solution for Divide I such that you first read an integer n, and then elements of an array with n components. Therefore you will need to dynamically allocate your array. Then divide by 5 the elements using the divby5() function and print the result from the main() function. Do not forget to release the allocated memory when not needed anymore. You can safely assume that the input will be valid.

Problem 5.5 Computing the scalar product of two vectors                                   

n

v · w=Xvi · wi

i=1

Use the function to compute the scalar product of the two vectors you read. From the main() function print the value of the scalar product on the screen. Additionally write functions for determining and printing on the screen the smallest and largest components of the vector v, and the position in the vector where they occur. You can safely assume that the input will be valid.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

Testcase 5.5: input
Testcase 5.5: output

Scalar product=6.600000

The smallest = 1.100000

Position of smallest = 0

The largest = 3.000000

Position of largest = 2

The smallest = 1.000000

Position of smallest = 0

The largest = 1.000000

Position of largest = 0
3

1.1

2.5

3.0

1.0

1.0

1.0
 
Problem 5.6
Pointer arithmetic


Language: C
Write a program that counts the number of elements in an array until encountering the first negative value without the usage of any integer counter variables (except for the loop for reading the elements of the array), but with the usage of pointers and pointer arithmetic.

Your program should read an int for the length of the array and an array of floats (containing at least one negative value) from the standard input. The number of non-negative values before the first negative value should be printed on the standard output.

You can assume that the input will be valid and the array will always contain at least one negative value. To pass the testcases your output has to be identical with the provided ones.

Testcase 5.6: input

5
Testcase 5.6: output

Before the first negative value: 2 elements
 1.2

3.4

-5.86

4

5.45

Problem 5.7 Concatenating two strings                                           

Language: C
Write a program to concatenate two strings (with the length of at most 100 characters) putting the result in a dynamically allocated array of chars with exact size.

Your program should read from the standard input two strings which may be statically allocated. The dynamically allocated string containing the concatenation result of the two strings should be printed on the standard output.

 Testcase 5.7: input

one
Testcase 5.7: output

Result of concatenation: onetwo
You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones. two

Problem 5.8 Dynamically allocated matrix multiplication                                   
Write a program that computes the multiplication of two dynamically allocated matrices.

Your program should dynamically allocate the memory for the three matrices (two input matrices and the result matrix). You should write functions for reading a matrix from the standard input, printing a matrix to the standard output, and finally a function for computing the multiplication of two matrices. At the end, do not forget to deallocate the memory used by the three matrices. The product of two matrices A and B of dimensions n × m and m × p can be calculated as:

 , with i =1,...,n and k =1,...,p.

Your program should read three integers (the dimensions n, m and p) and the elements of two integer matrices from the standard input. The result of the matrix multiplication should be printed on the standard output.

You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones.

 Testcase 5.8: inputTestcase 5.8: output
2Matrix A:

21 2

23 4

1Matrix B:

21 0

30 1

4The multiplication result AxB:

11 2

03 4

0

1

Problem 5.9 Printing dynamically allocated 3D-array sections                            
Write a program that dynamically allocates memory for a 3D-array of integers and prints the 2Dsections parallel to the “XOY axis” (considering the array dimensions row-dimension, columndimension and depth-dimension similar to the geometrical X, Y and Z dimensions) of a 3Darray.

Your program should read three integer values corresponding to the dimensions of a 3D-array (in the order of rows, columns, depth) and should dynamically allocate the memory for this 3Darray. You should write functions for reading the elements of the 3D-array from standard input (first iterating through rows, then columns and then the depth) and finally a function for printing the 2D-sections of the 3D-array which are parallel to the “XOY axis”. Do not forget about the allocation and deallocation of the memory.

You can assume that the input will be valid. To pass the testcases your output has to be identical with the provided ones.

 Testcase 5.9: inputTestcase 5.9: output
2Section 1:

21 1

31 1

1Section 2:

22 2

32 2

1Section 3:

23 3

33 3

1

2

3

1

2

3

Problem 5.10 Print numbers counting down                                                          
Write a program which reads a positive integer n from the keyboard. Then write and call a

recursive function for printing the numbers n,n − 1,...,1. You can safely assume that the input will be valid.

Problem 5.11 Determine if a number prime                                                           
Write a program which reads a positive integer x. Then write a recursive function for determining if x is a prime number or not. The function should return 1 if the number is prime and 0 if not.

Print a corresponding message from the main() function.

You can safely assume that the input will be valid.

Your solution has to satisfy the requirements from the problem description and has to pass the following testcase and potentially other testcases which are uploaded. All characters are relevant for passing testcases including newlines and spaces.

 Testcase 5.11: inputTestcase 5.11: output

2626 is not prime

More products