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 it to determine whether it can form a triangle. If it can form a triangle, 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.
ubuntu@ubuntu:~/zy5$ gcc 1.c ubuntu@ubuntu:~/zy5$ ./a.out Please enter the values of the three sides: 6 6 6 Equilateral triangle ubuntu@ubuntu:~/zy5$ l ubuntu@ubuntu:~/zy5$ ./a.out Please enter the values of the three sides: 1 2 6 cannot form a triangle ubuntu@ubuntu:~/zy5$ ./a.out Please enter the values of the three sides: 6 6 6 Equilateral triangle ubuntu@ubuntu:~/zy5$ ./a.out Please enter the values of the three sides: 3 4 5 general triangle ubuntu@ubuntu:~/zy5$ cat 1.c #include<string.h> #include<stdio.h> #include<stdlib.h> int jiao(int i,int j,int k) { if(i+j<k||i+k<j||k+j<i){ printf("cannot form a triangle\n"); }else if(i==j&&i==k){ printf("Equilateral triangle\n"); }else if(i==j||i==k||j==k){ printf("isosceles triangle\n"); }else{ printf("general triangle\n"); } } int main(int argc, const char *argv[]) { int a,b,c; printf("Please enter values for the three sides:"); scanf("%d%d%d",&a,&b,&c); jiao(a,b,c); return 0; }
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
ubuntu@ubuntu:~/zy5$ ./a.out Please enter power: 100 Electricity fee: 35.00 ubuntu@ubuntu:~/zy5$ ./a.out Please enter power: 200 Electricity fee: 81.00 ubuntu@ubuntu:~/zy5$ ./a.out Please enter power: 0 Electricity fee: 0.00 ubuntu@ubuntu:~/zy5$ ./a.out Please enter power: 170 Electricity fee: 67.20 ubuntu@ubuntu:~/zy5$ cat 2.c #include<string.h> #include<stdio.h> #include<stdlib.h> float fei(int i) { float f=0; if(i>200){ f=100*0.35+100*0.46+(i-200)*0.68; }else if(i>100){ f=35+(i-100)*0.46; }else if(i>0){ f=i*0.35; }else{ f=0; } printf("Electricity charges are:%.2f\n",f); } int main(int argc, const char *argv[]) { float a=0; printf("Please enter power:"); scanf("%f",&a); fei(a); return 0; }
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.
ubuntu@ubuntu:~/zy5$ gcc 3.c 3.c: In function 'main': 3.c:38:2: warning: implicit declaration of function 'gets'; did you mean 'fgets'? [-Wimplicit-function-declaration] gets(z); ^~~~ fgets /tmp/cclCIgjv.o: in function'main'middle: 3.c:(.text+0x170): warn: the `gets' function is dangerous and should not be used. ubuntu@ubuntu:~/zy5$ ./a.out Please enter a string: 23 erf56x 45 The number of numeric characters is an even number Sum of number arrays: 313 Mean: 52.166667 ubuntu@ubuntu:~/zy5$ cat 3.c #include<string.h> #include<stdio.h> #include<stdlib.h> char zifu(char arr[],int n) { char brr[50]; int i=0; int j=0; int sum=0;//and int len=strlen(arr); for(i=0;i<len;i++) { if(arr[i]>='0'&&arr[i]<='9') { brr[j]=arr[i]; sum+=brr[j]; j++; } } if(j%2==0){ printf("The number of numeric characters is an even number\n"); }else{ printf("Numeric characters are odd\n"); } printf("Sum of arrays of numbers:%d\n",sum); if(j>0){ double avg=sum*1.0/j; printf("mean:%lf\n",avg); } } int main(int argc, const char *argv[]) { char z[50]; printf("Please enter a string:"); gets(z); zifu(z,50); return 0; }
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
ubuntu@ubuntu:~/zy5$ gcc 4.c ubuntu@ubuntu:~/zy5$ ./a.out Please enter the grade of the first student: 50 Please enter the grade of the second student: 60 Please enter the grade of the third student: 40 Please enter the grade of the 4th student: 30 Please enter the grade of the fifth student: 90 Please enter the grade of the 6th student: 50 1st student's grade: 50 2nd student's grade: 60 3rd student's grade: 40 4th student's grade: 30 5th student's grade: 90 6th student's grade: 50 Scores in ascending order: 30 40 50 50 60 90 320 ubuntu@ubuntu:~/zy5$ cat 4.c #include<string.h> #include<stdio.h> #include<stdlib.h> int school(int brr[],int n) { int i=0; for(i=0;i<6;i++){ printf("Please enter the No.%d students' grades:",i+1); scanf("%d",&brr[i]); } for(i=0;i<6;i++){ printf("No.%d students' grades:%d\n",i+1,brr[i]); } int temp=0; int j=0; for(i=1;i<6;i++){ for(j=0;j<6-i;j++){ if(brr[j]>brr[j+1]){ temp=brr[j]; brr[j]=brr[j+1]; brr[j+1]=temp; } } } printf("Ranked from smallest to largest:"); for(i=0;i<6;i++){ printf("%d\t",brr[i]); } printf("\n"); } int zong(int crr[],int n) { int sum=0; for(int a=0;a<6;a++){ sum+=crr[a]; } return sum; } int main(int argc, const char *argv[]) { int arr[6]; school(arr,6); int z=zong(arr,6); printf("%d\n",z); return 0; }