A Solution to Some C Simple Questions

Introduction

A friend asked me about some questions of C language, which are typical calculations and typesetting usages, from a so-called experiment. I am trying to solve all of them.

Questions & Solutions

Q1. Program to find the perimeter and area of a rectangle from inputting the length and width of the rectangle

Analysis to Q1

This question is intended for training the usages of input and output. For seeing difference, a calculation is required.

Solution to Q1

The code is a solution on Visual C++ 6.0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* This is an annotation of C style. */
/* The code is a solution on Visual C++ 6.0. */
#include <stdio.h>

int main(void) {
float length, width, area, perimeter;
// Note that if your data are very big, you may need "long" instead of "float", which has decimal.
printf("Please input the length and width of a rectangle:\n");
printf("Length: ");
scanf("%f", &length);
printf("Width: ");
scanf("%f", &width);
// We can completely show the results without an assignment statement, but the original experiment requirements included it.
perimeter = 2 * (length + width);
area = length * width;
printf("The perimeter of the rectangle is %f while the area of it is %f.\n", perimeter, area);
return 0;
}

Q2. Write a program to “rotate” the capital A to the lowercase a

Analysis to Q2

First we should comprehend what “rotate” means. We suppose that it means transform. Then, we know that it should be about the process of a sentence, or a string. Last, it is enough for a computer to find all capital "A"s one by one.

Solution to Q2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main(int argc, const char* argv[]) { // It is also OK to use int main(void)
char sentence[101]; // We suppose a sentence has max 100 characters.
printf("Please input a sentence with pressing enter key to finish: \n");
scanf("%s", sentence);
for (int i = 0; sentence[i] != '\0' && i < 100; i++) {
if (sentence[i] == 'A')
sentence[i] = 'a';
}
printf("OK. All capital As has transformed into lowercase: \n");
printf("%s\n", sentence);
return 0;
}

Q3. Given the radius of the circle r = 2.5 and the height of the cylinder h = 1.8, calculate the surface area and volume of the cylinder

Analysis to Q3

It is easy to know the surface area of a cylinder is derived from S=2×(πr2)+2πrh=2πr(r+h)S=2\times(\pi r^{2})+2\pi rh=2\pi r(r+h) and the volume of it is from V=πr2h.V=\pi r^{2}h. Hence, it is very easy to program.

Solution to Q3

1
2
3
4
5
6
7
#include <stdio.h>

int main(void) {
float radius = 2.5, height = 1.8;
printf("Given the radius of the circle r = 2.5 and the height of the cylinder h = 1.8, we know that the surface area of it is %.2f and the volume of it is %.2f.\n", 2 * 3.1416 * radius * (radius + height), 3.1416 * radius * radius * height);
return 0;
}

A Solution to Some C Simple Questions
https://lucisurbe.pages.dev/2020/04/25/A-Solution-to-Some-C-Simple-Questions/
Author
Lucis Urbe
Posted on
April 25, 2020
Licensed under