Homework 1: Define the implementation of a function with parameters and no return value. The calling function passes the three sides of the triangle, and the called function judges them to determine whether a triangle can be formed. If a triangle can be formed, determine whether the triangle is an equilateral triangle or an isosceles triangle. A triangle is still a half triangle. If a triangle cannot be formed, the output cannot be triangulated.
#include <stdio.h>
#include <math.h>
void IsTriangle(double a, double b, double c);
int main(int argc, char const *argv[])
{
double a,b,c;
printf("Please enter the side lengths of the three sides of the triangle:");
scanf("%lf%lf%lf",&a,&b,&c);
IsTriangle(a,b,c);
return 0;
}
void IsTriangle(double a, double b, double c)
{
//Whether the triangle is established or not
if(a>0&&b>0&&c>0&&(a+b>c)&&(b+c>a)&&(a+c>b))
{
//Equilateral Triangle Judgment Conditions
if(a==b&&b==c)
{
printf("The triangle is an equilateral triangle!\n");
}
//Isosceles triangle judgment condition
else if (a==b||b==c||a==c)
{
printf("The triangle is an isosceles triangle!\n");
}
//Conditions for judging a right triangle
else if((pow(a,2)+pow(b,2)==pow(c,2))||(pow(b,2)+pow(c,2)==pow(a,2))||(pow(a,2)+pow(c,2)==pow(b,2)))
{
printf("The triangle is a right triangle!\n");
}
else
{
printf("This triangle is a normal triangle!\n");
}
}
else
{
printf("Cannot form a triangle! ! !\n");
}
}
The running result is shown in the figure below:
Homework 2: Use the function with parameters and no return value to realize the following application problems
An electric heater is a product that converts electrical energy into heat. At present, domestic electric heating methods are mainly divided into heating cable floor radiant heating, electric heating film heating and electric heating.
Programming realizes the function of inputting electricity consumption and calculating electricity bills.
Note: The electricity fee is based on the stage electricity fee:
If the electricity consumption is between 1-100 degrees, the unit price of electricity is 0.35 yuan
If the degree of electricity consumption is 101---200, the unit price of electricity bill is 0.46 yuan
If it exceeds 200 degrees, the unit price of electricity is 0.68 yuan
For example: if the electricity consumption is 120 degrees, the first 100 degrees will be charged according to the first step, and the remaining 20 degrees will be charged according to the second step.
Electricity fee=100*0.35+(120-100)*0.46;
It is required to input the electricity consumption in the calling function, and output the electricity fee to be paid in the called function
#include <stdio.h>
void Caculate_Electric(double a);
int main(int argc, char const *argv[])
{
double electri_fee;
printf("Please enter your electricity consumption:");
scanf("%lf",&electri_fee);
Caculate_Electric(electri_fee);
return 0;
}
void Caculate_Electric(double a)
{
double money;
if(a >= 1 && a <= 100)
{
money = a*0.35;
}
else if (a > 100 && a <= 200)
{
money = 100*0.35+(a-100)*0.46;
}
else
{
money = 100*0.35+100*0.46+(a-200)*0.68;
}
printf("Your electricity bill is:%.1lf\n",money);
}
The running result is shown in the figure below:
Homework 3: Define a function with parameters and no return value, complete the input string in the main function, pass the array name as a function parameter, and store the numeric characters in the string into a new array in the called function, and count the odd and even numbers respectively Number, sum, average output.
#include <stdio.h>
void character(char A[],int n);
int main(int argc, char const *argv[])
{
char str[80];
gets(str);
character(str,strlen(str));
return 0;
}
void character(char A[],int n)
{
char B[n];
int j = 0; //number of numbers
int odd = 0,even = 0; //odd number even number
int sum = 0;
printf("The numbers in this string are:\n");
for(int i = 0; i < n; i++)
{
if(A[i]>='0'&&A[i]<='9')
{
B[j] = A[i];
printf("%c\t",B[j]);
sum += B[j]-'0';
if((B[j]-'0')%2==0)
even++;
else
odd++;
j++;
}
}
printf("\n where odd numbers have%d indivual,Even number has%d indivual\n",odd,even);
printf("\n and:%d,average value:%lf\n",sum,(double)sum/j);
}
The running result is shown in the figure below:
Homework 4: Define the function implementation:
An array is defined in the calling function to store the grades of 6 students.
Call the custom function with parameters and no return value to complete the record entry of 6 students.
Call the custom function with parameters and no return value to complete the output of grades.
Call the function with parameters and no return value to complete the ascending sorting of grades.
Call the custom function with parameters and return value, and after calculating the total score, output the total score in the calling function
#include <stdio.h>
void Data_Entry(double A[],int n); //Data Entry
void Data_Output(double A[],int n); //data printing
void BubbleSort(double A[],int n); //Bubble Sort
double Sum_Grade(double A[],int n); //Calculate total grade
int main(int argc, char const *argv[])
{
double grade[6];
Data_Entry(grade,6);
printf("\n<-------------------delimiter------------------->\n");
printf("\n The entered student grades are as follows:\n");
Data_Output(grade,6);
printf("\n<-------------------delimiter------------------->\n");
BubbleSort(grade,6);
printf("\n<-------------------delimiter------------------->\n");
printf("\n The total score is:%.1lf\n",Sum_Grade(grade,6));
return 0;
}
//Data Entry
void Data_Entry(double A[],int n)
{
for(int i = 0; i < n;)
{
printf("Please enter the No.%d grades of students(Percentage system): ",i+1);
scanf("%lf",&A[i]);
if(A[i]>=0 && A[i]<=100){
i++;
}else{
printf("Input error, please re-enter! ! !\n");
}
}
}
//data printing
void Data_Output(double A[],int n)
{
for (int i = 0; i < n; i++)
{
printf("%.1lf\t",A[i]);
}
printf("\n");
}
//Bubble Sort
void BubbleSort(double A[],int n)
{
double temp;
for (int i = 1; i <= n-1; i++) //External passes
{
int flag = 0; //A flag indicating whether the current bubbling has changed
for (int j = 0; j < n-i; j++)
{
if(A[j] > A[j+1]) //Reverse order of each other
{
//exchange
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = 1; //Indicates that a transformation has occurred
}
}
if(flag == 0)
break;
}
printf("\n The sorted grades are:\n");
Data_Output(A,6);
}
//Calculate total grade
double Sum_Grade(double A[],int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += A[i];
}
return sum;
}
The running result is shown in the figure below: