1- Write a function float computeArea (int *v1, int *v2, int shape ) that computes the area of four basic geometric entities. The geometric entity will be identified by the argument shape which is 1 for triangle, 2 for square/rectangle and 3 for circle. The arguments v1 and v2 are pointers to the two arguments defining the shape. For circle v1 is a pointer to the diameter of the circle, v2 is null, for rectangle/square, v1 and v2 are pointers to the height and width for triangle v1 is a pointer to the the base and v2 points to the height of the triangle.
2- Write a function that accepts a string and a character as input and returns the number of times the character given as the second argument appears in the string. int numTimesAppears(char *mystring, char ch) “Yusuf Ozturk” and letter u is passed as arguments the function should return 3.
#include <stdio.h> float computeArea(int *v1, int *v2, int shape) { if (shape == 1) { return 0.5 * (*v1) * (*v2); } else if (shape == 2) { return (*v1) * (*v2); } else { return (3.14 * (*v1) * (*v1)) / 4; } } int numTimesAppears(char *mystring, char ch) { int count = 0, i = 0; while (mystring[i]) { if (mystring[i] == ch) { ++count; } i++; } return count; } int main(void) { char mystring[] = "Yusuf Ozturk"; char ch; float area; int length, height, diameter, base; int count; length = 10; height = 5; base = 20; diameter = 6; area = computeArea(&base, &height, 1); printf("\nThe area of the triangle is %f", area); area = computeArea(&length, &height, 2); printf("\nThe area of the square or rectangle is %f", area); area = computeArea(&diameter, &diameter, 3); printf("\nThe area of a circle is %f", area); ch = 'u'; count = numTimesAppears(mystring, ch); printf("\nNumber of times %c appears in string is %d", ch, count); return 0; }
Get Answers For Free
Most questions answered within 1 hours.