# [Solved] CH-230-A-Assignment 4 Loops and Strings, Dynamic Memory Allocation, Multidimensional Arrays

Click And Check all our Assignments

10 USD \$

Category: Tag:

# Problem 4.1 A table for circles

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: input1.530.5 Testcase 4.1: output1.500000 7.068583 9.4247782.000000 12.566371 12.5663712.500000 19.634954 15.7079633.000000 28.274334 18.849556 Problem 4.2 Word as zig zag

## 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: inputHello world Testcase 4.2: outputH

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.

# Problem 4.4 Determine consonants in string I

Write a function int count_consonants(char str[]) that determines and returns the number of consonants in a given string.

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 ’
’ 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: input312345 Testcase 4.7: outputThe entered matrix is:1 2 34 5 67 8 9Under 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: input3123456789 Testcase 4.8: outputThe entered matrix is:1 2 34 5 67 8 9Under the secondary diagonal:6 8 9 Problem 4.9 Dynamic Array

Write a function int prodminmax(int arr[], int n) that determines and returns the product of the smallest and largest elements of an array of integers.

Then write a program where you first read an integer n and then n integers that are stored in an array called arr. Test the function above and print on the screen the results from the main() function. Use dynamic memory allocation and deallocation. You can safely assume that the input will be valid.

 Problem 4.10 Passing by reference

### Language: C

Write a function void proddivpowinv(float a, float b, float *prod, float *div, float *pwr, float *invb) that computes and “returns” by reference the product, the division of the two floats as well as the result of ab and 1/b.

Also write a simple test program to check that your function works correctly (by printing on the screen the results from the main() function).

You can safely assume that the input will be valid and that b will not be 0.

## Problem 4.11 Walk the string

Write a function int countinsensitive(char *str, char c) that counts the occurrences of the character c in the string str in the case insensitive manner (i.e., no difference between uppercase and lowercase letters). Write a program that tests this function. You should be able to process a string of arbitrary length. First you can dynamically allocate a string of maximal length of 50 characters, then you read the string from the keyboard, then you allocate memory for another string of the correct size, copy the original string into the new string and then deallocate the memory occupied by the first string. You may use the functions tolower() or toupper() which are in ctype.h. Use the man page to see how these functions work.

You can safely assume that the input will be valid.

Determine the occurrence of the characters ’b’, ’H’, ’8’, ’u’, and ’\$’. For each character the output should be as follows, for example:

The character ’b’ occurs 3 times.

## Problem 4.12 String functions

Write a function void replaceAll(char *str, char c, char e) that replaces all occurrences of the character c by the character e.

Write a program to test the function. The program should repeatedly read a string (up to 80 chars), a character to be replaced and then the replacing character until you enter “stop” for the string. Your program should repeatedly print the character to be replaced, the replacing character and the strings on the screen from the main function before and after the replacement. You can safely assume that the input will be valid.

## Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart  [Solved] CH-230-A-Assignment 4 Loops and Strings, Dynamic Memory Allocation, Multidimensional Arrays
10 USD \$