Starting from:

$25

CH-230-A-Assignment 4 Solved

Assignment 4 - More with Loops and Strings, Dynamic Memory Allocation, Multidimensional Arrays

Problem 4.1 A table for circles                                                                                
 Language: C
Write a program that prints a table where each line consists of a value x, the area of the circle with radius x, and the perimeter of the circle with radius x (i.e., three values separated by space). Ask the user to input from the keyboard the lower and upper limits as well as a step size for the table (i.e., at which value to start and where to stop, and the increment from one line to the next).

You can safely assume that the upper and lower limits, and the step size will be valid.

Use a for loop for solving this problem.

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 4.1: input

1.5

3

0.5
Testcase 4.1: output

1.500000 7.068583 9.424778

2.000000 12.566371 12.566371

2.500000 19.634954 15.707963

3.000000 28.274334 18.849556
Problem 4.2 Word as zig zag
(1 point)

Language: C
Write a program where you read a string (which may contain spaces) from the standard input. You can safely assume that the string will not be longer than 50 characters. Print the string on the screen as in the following testcase.

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 4.2: input

Hello world
Testcase 4.2: output

H
 e l l o

w o r l d

Problem 4.3 Using switch and calling functions                                                   
Write a program where you can read up to 15 floats from the keyboard into an array. A negative value ends the input loop and the negative value is not part of the array. You can assume that no more than 15 integers will be read.

Then by using a switch statement, pressing m (and ’Enter’) computes the geometric mean of the array (and prints the result), h determines and prints the highest number in the array, l determines and prints the smallest number in the array and s determines and prints the sum of all elements in the array. Write functions for each task. The result of these functions must be printed from the main() function.

The prototype to compute the geometric mean should have the following form:

float geometric_mean(float arr[], int num);

The formula for computing the geometric mean of an array of positive values (xi)i=1,...,n with n elements is:1

 !n

gmean =

You can safely assume that the input will be valid.

Then write a simple main() function where you can repeatedly enter a string and then the number of consonants is determined and printed on the screen (from the main() function). If the entered string is empty (it will contain only a ’\n’ then) the program should stop its execution. You can safely assume that the entered string will be not longer than 100 characters and 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 4.4: inputTestcase 4.4: output
Hello worldNumber of consonants=7 thisNumber of consonants=3 lastNumber of consonants=3

Problem 4.5 Determine consonants in string II                                                      
Rewrite the function int count_consonants(char str[]) from your solution of the previous problem to walk through the string using a pointer and address arithmetic. Reuse your main() function from the previous problem’s solution.

Problem 4.6 Dynamic memory allocation                                                        

  Write a program which allocates memory for an array of integers entered from the keyboard having n elements (value read also from the keyboard). Write and call a function which determines and prints on the screen the two greatest values within this array. At the end of the program make sure that the memory will be released.

You can safely assume that the input will be valid. Solve the problem without sorting the array or without iterating through the array more than one time.

Problem 4.7 Under the main diagonal of matrix                                                    
Write a program which declares a two dimensional array of integers having at most 30 rows and 30 columns. Read from the keyboard an integer n representing the number of rows and the number of columns of the matrix.

Then read the values of the matrix row by row and column by column. Then write and call a function for printing the values of the matrix in its form. Also write and call a function which prints on the screen the elements of the matrix which are under the main diagonal.

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 4.7: input

3

1

2

3

4

5
Testcase 4.7: output

The entered matrix is:

1 2 3

4 5 6

7 8 9

Under the main diagonal:

4 7 8
6

7

8

9

Problem 4.8 Under the secondary diagonal of matrix                                           
Modify your solution for the previous problem such that the elements under the secondary diagonal (i.e., the other diagonal) are printed on the screen.

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 4.8: input

3

1

2

3

4

5

6

7

8

9
Testcase 4.8: output

The entered matrix is:

1 2 3

4 5 6

7 8 9

Under the secondary diagonal:

6 8 9
Problem 4.9 Dynamic Array
(1 point)

More products