100% Guaranteed Results


Programming in C and C++
$ 24.99
Category:

Description

5/5 – (1 vote)

Assignment 4 – More with Loops and Strings, Dynamic Memory Allocation, Multidimensional Arrays
• The problems of this assignment must be solved in C or C++ (instruction in each problem).
Problem 4.1 A table for circles (1 point)
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
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 (2 points)
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 (1 point)
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 (1 point)
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 (2 points)
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 (1 point)
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 (1 point)
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)
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 (1 point)
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 (1 point)
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 (1 point)
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.
How to submit your solutions
• Your source code should be properly indented and compile with gcc or g++ depending on the problem without any errors or warnings (You can use gcc -Wall -o program program.c or g++ -Wall -o program program.cpp). Insert suitable comments (not on every line …) to explain what your program does.
• Name the programs according to the suggested filenames (they should match the description of theproblem) in Grader.
Each program must include a comment on the top like the following:
/* CH-230-A
a4p1.[c or cpp or h]
*/
If there are problems (but only then) you can submit the programs by sending mail to

Reviews

There are no reviews yet.

Be the first to review “Programming in C and C++”

Your email address will not be published. Required fields are marked *

Related products