当前位置:网站首页>C language function program example (super complete)
C language function program example (super complete)
2022-07-28 21:00:00 【Small digital media members】
Catalog
6. Connect two strings with a function , Not allowed strcat function .( Use for Cycle to achieve )
19. Write function fun seek 1 Of K Second party to N Of K Cumulative sum of powers
1. Write function fun, The function is : Calculation n Average of courses , The result of the calculation is returned as a function value . for example , If you have any 5 The result of this course is :92,76,69,58,88, Then the value of the function is 76.599998.
#include "stdio.h"
float fun(int a[],int n)
{
int i,sum=0;
float ave;
for(i=0;i<n;i++)
sum=sum+a[i];
ave=sum/n;
return ave;
}
int main()
{
int a[]={92,76,69,58,88};
printf("y=%f\n",fun(a,5));
}
Running results :

2. Use a function to put a N Transpose the order square matrix , Input and output are implemented in the main function .( Use for Cycle to achieve )
#include "stdio.h"
#include <stdlib.h>
#define N 4
void inv(int a[][N])
{
int i,j,k;
for(i=0;i<N;i++)
for(j=0;j<=i;j++)
{
k=a[i][j];
a[i][j]=a[j][i];
a[j][i]=k;
}
}
int main()
{
int a[N][N],i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
a[i][j]=rand()%10+20;// Randomly generated 0~20 The integer of
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("=============\n");
inv(a);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%3d",a[i][j]);
printf("\n");
}
}
Running results :

3. Write function fun(str,i,n), From a string str Delete the i Consecutive starting characters n Characters . notes :str[0] Represents the first character of a string ).( Use while Cycle to achieve
#include "stdio.h"
#include<string.h>
void fun(char str[],int i,int n)
{
while(str[i+n-1])
{
str[i-1]=str[i+n-1];
i++;
}
str[i-1]='\0';
}
int main()
{
char str[81];
int i,n;
printf(" Please enter the string str Value :\n");
scanf("%s",str);
printf(" The string you entered str yes :%s\n",str);
printf(" Please enter the deletion location i And the number of characters to be deleted n Value :\n");
scanf("%d%d",&i,&n);
while (i+n-1>strlen(str))
{
printf(" Delete location i And the number of characters to be deleted n The value of is wrong ! Please re-enter i and n Value \n");
scanf("%d%d",&i,&n);
}
fun(str,i,n);
printf(" The deleted string str yes :%s\n",str);
}
Running results :

4. Find the greatest common divisor of two integers by rolling division .( Use while Cycle to achieve )
#include<stdio.h>
int gcd(int n,int m)
{
int r,t;
if(n<m) { t=n;n=m;m=t;}
r=n%m;
while(r!=0)
{ n=m;m=r;r=n%m;}
return(m);
}
int main()
{
int n,m,result;
scanf("%d%d",&n,&m);
result=gcd(n,m);
printf("the gcd is %d\n",result);
}Running results :

5. Write function fun(n), The function is to find n!. The function of the main function is to calculate : x!+y!+z! Value .( Questions of the same type :2!+4!+6!+8!+10! perhaps 3!+5!+7!+9!+11!)
#include "stdio.h"
long fun(int n)
{
int i;
long f=1;
for(i=1;i<=n;i++)
f=i*f;
return f;
}
int main()
{
int x,y,z;
long sum=0.0;
printf("Enter x,y,z:");
scanf("%d%d%d",&x,&y,&z);
sum=fun(x)+fun(y)+fun(z);
printf("Sum=%ld\n",sum);
return 0;
}
Running results :

6. Connect two strings with a function , Not allowed strcat function .( Use for Cycle to achieve )
#include "stdio.h"
void len_cat(char c1[],char c2[])
{
int i,j;
for(i=0;c1[i]!='\0';i++);
for(j=0;c2[j]!='\0';j++)
c1[i+j]=c2[j];
c1[i+j]='\0';
}
int main()
{char s1[80],s2[40];
gets(s1);gets(s2);
len_cat(s1,s2);
printf("string is: %s\n",s1);
return 0;
}
Running results :

7. Write function fun Ask for one not more than 5 The number of bits of a positive integer , Enter this number in the main function , And output the result in the main function .
#include "stdio.h"
int fun(int m)
{
int c=0 ;
while(m>0)
{
m/=10;
c++;
}
return c;
}
int main()
{ long int num;
int ws;
printf(" Enter a number that is no more than 5 An integer :");
scanf("%ld",&num);
ws=fun(num);
printf("%ld It's a %d digit \n",num,ws);
return 0;
}
Running results :

8. Enter from the keyboard for a one-dimensional integer array 10 It's an integer , call fun Function to find the smallest number , And in main Output in function . Please write fun function .
#include "stdio.h"
int fun(int x[],int n)
{
int i,min;
min=x[0];
for(i=1;i<n;i++)
if(x[i]<min) min=x[i];
return min;
}
int main()
{
int a[10],i,min;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
printf("%3d",a[i]);
printf("\n");
min=fun(a,10);
printf("%d\n",min);
return 0;
}
Running results :

9. Find the number of numbers less than the average in a batch .( Use for Cycle to achieve )( Questions of the same type : Find the largest even or odd number in a batch of positive integers . perhaps Find the smallest number or the largest number in a group )
#include<stdio.h>
int average_num(int a[],int n)
{
int i,sum=0,k=0;
float average;
for(i=0;i<n;i++)
sum=sum+a[i];
average=sum*1.0/n;
for(i=0;i<n;i++)
if(average<a[i]) k++;
return(k);
}
int main()
{
int n,a[100],i,num;
printf(" Please enter the specified number of a group :");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
num=average_num(a,n);
printf(" Number less than the average :%d\n",num);
return 0;
}
Running results :

10. The student's record consists of student number and grade .N The data of students has been put into the structure array in the main function s in , Please write a function fun: Put the data of students whose scores are higher than or equal to the average on b In the array referred to , The number of students with an average score higher than or equal to... Through formal parameters n Send back , The average score is returned by the function value .
#include <stdio.h>
#define N 12
typedef struct
{
char num[10];
int s;
} STREC;
double fun( STREC *a, STREC *b, int *n )
{
int i,j=0;
double sum=0.0;
double avg;
for(i=0;i<N;i++)
{ sum+=a[i].s; }
avg=sum/N;
for(i=0;i<N;i++)
{
if(a[i].s>=avg)
b[j++]=a[i];
}
*n=j;
return avg;
}
int main()
{
STREC s[N]={
{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},
{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87},
{"GA09",60},{"GA11",79},{"GA12",73},{"GA10",90}};
STREC h[N];
int i, n;
double ave;
ave=fun( s,h,&n );
printf("The %d student data which is higher than %7.3f:\n",n,ave);
for(i=0;i<n; i++)
printf("%s %d\n",h[i].num,h[i].s);
printf("\n");
return 0;
}
Running results :

11. function fun, Its function is to convert a numeric string into a long integer with the same face value . Callable len Find the string length .
#include<stdio.h>
long fun(char *s)
{
long r = 0;
while (*s != '\0')
{
r = r * 10 + (*s - '0');
s++;
}
return r;
}
int main()
{
printf("%ld\n",fun("2345210"));
return 0;
}
Running results :

12. function fun, take M That's ok N The character data in the two-dimensional array of columns is put into a string in the order of columns
#include <stdio.h>
#define M 3
#define N 4
char *fun(char s[M][N],char *t)
{
int i,j,k=0;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
t[k++]=s[j][i];
}
}
t[k]='\0';
return t;
}
int main()
{
char s[M][N]={'W','W','W','W','S','S','S','S','H','H','H','H'};
int i,j,d=M*N;
char t[d];
printf(" The contents of the two-dimensional array are :\n");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
printf("%c ",s[i][j]);
}
printf("\n");
}
printf(" The result is :%s",fun(s,t));
return 0;
}
Running results :

13. function sort: Bubbling , For the in the array 10 Data , Sort from small to large , Output... In the main function
#include "stdio.h"
void sort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
{
t=a[j];a[j]=a[j+1];a[j+1]=t;
}
}
int main()
{
int a[10]={34,67,90,43,124,87,65,99,132,26};
int i;
sort(a,10);
for(i=0;i<10;i++)
printf("%4d",a[i]);
printf("\n");
return 0;
}
Running results :

14. Converts lowercase letters in a string to corresponding uppercase letters , Other characters remain unchanged .
#include "string.h"
#include "stdio.h"
void change(char str[])
{
int i;
for(i=0;str[i]!='\0';i++)
if(str[i]>='a'&& str[i]<='z')
str[i]=str[i]-32;
}
int main()
{
char str[40];
gets(str);
change(str);
puts(str);
return 0;
}
Running results :

15. Write function fun Generate a main diagonal element as 1, All the other elements are 0 Of 3*3 Two dimensional array of
#include "stdio.h"
void fun(int arr[][3])
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(i==j) arr[i][j]=1;
else arr[i][j]=0;
}
int main()
{ int a[3][3],i,j;
fun(a);
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
Running results :

16. Write a function to find 1~100( Closed interval ) Sum of squares of odd numbers in . The result is 166650
#include "stdio.h"
float sum(int n)
{
int i;
float s=0;
for(i=1;i<=n;i+=2)
s+=i*i;
return s;
}
int main()
{
printf("sum=%f\n",sum(100));
return 0;
}
Running results :

17. Find the difference between the maximum value and the minimum value in a batch .( and 、 The product of )
#define N 30
#include "stdlib.h"
#include "stdio.h"
int max_min(int a[],int n)
{
int i,max,min;
max=min=a[0];
for(i=1;i<n;i++)
if(a[i]>max) max=a[i];
else if(a[i]<min) min=a[i];
return (max-min);
}
main()
{
int a[N],i,k;
for(i=0;i<N;i++)
a[i]=rand()%(51)+10;
for(i=0;i<N;i++)
{printf("%5d",a[i]);
if((i+1)%5==0) printf("\n");
}
k=max_min(a,N);
printf("the result is:%d\n",k);
}
Running results :

18. Given n Data , Where the minimum value appears ( If the minimum value appears more than once , Find the position of the first occurrence )
#include<stdio.h>
int station(int s[],int n)
{
int i,k;
k=0;
for(i=1;i<n;i++)
if(s[i]<s[k]) k=i;
return(k+1);
}
main()
{
int a[100],n,i,t;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
t=station(a,n);
printf("the min_value position is:%d\n",t);
}
Running results :

19. Write function fun seek 1 Of K Second party to N Of K Cumulative sum of powers
#define K 4
#define N 5
#include "stdio.h"
long fun(int n,int k)
{
long power,sum=0;
int i,j;
for(i=1;i<=n;i++)
{ power=i;
for(j=1;j<k;j++)
power *= i;
sum += power;
}
return sum;
}
main()
{ long int sum;
printf("Sum of %d powers of integers from 1 to %d = ",K,N);
sum=fun(N,K);
printf("%ld\n",sum);
}
Running results :

20. Write function fun, The function : Find a given positive integer m( Include m) Sum of prime numbers within . for example : When m=20 when , Function value is 77.
#include "stdio.h"
int fun(int m)
{
int i,k,s=0;
for(i=2;i<=m;i++)
{for(k=2;k<i;k++)
if(i%k==0)break;
if(k==i) s=s+i;}
return s;
}
int main()
{
int y;
y=fun(20);
printf("y=%d\n",y);
return 0;
}
Running results :

21. Write function fun, The function : Find the number of letters in a given string .( Use for Cycle to achieve )
#include "stdio.h"
int fun(char s[])
{
int i,k=0;
for(i=0;s[i]!='\0';i++)
if(s[i]>='a'&&s[i]<='z' || s[i]>='A'&&s[i]<='Z')
k++;
return k;
}
int main()
{
char str[]="Best wishes for you!";
int k;
k=fun(str);
printf("k=%d\n",k);
return 0;
}
Running results :

22. Write function fun, The function : Calculate and output the given integer n The sum of all the factors of ( barring 1 And myself ). Regulations n The value of is not greater than 1000. for example :n The value of is 855 when , The output should be 704.
#include "stdio.h"
int fun(int n)
{
int s=0,i;
for(i=2;i<n;i++)
if(n%i==0) s=s+i;
return s;
}
int main()
{
printf("s=%d\n",fun(855));
return 0;
}
Running results :

23. Find with function fibonacci Before number sequence 28 Sum of items . The first value of the known sequence is 1, The second value is also 1, Start with the third item , Each item is the sum of its preceding two adjacent items . Running results :832039
#include "stdio.h"
long sum(long f1,long f2)
{
long f,k=f1+f2;
int i;
for(i=3;i<=28;i++)
{f=f1+f2;
k=k+f;
f1=f2;
f2=f;
}
return(k);
}
int main()
{long int f1=1,f2=1;
printf("sum=%ld\n",sum(f1,f2));
return 0;
}
Running results :

24. Write function fun, The function : Find a fractional sequence 2/1,3/2,5/3,8/5,13/8,21/13… Before n Sum of items . for example : Seek before 20 The value of the sum of items is 32.660259
#include "stdio.h"
float fun(int n)
{
int i;
float f1=1,f2=1,f3,s=0;
for(i=1;i<=n;i++)
{f3=f1+f2;
f1=f2;
f2=f3;
s=s+f2/f1;
}
return s;
}
int main()
{float y;
y=fun(20);
printf("y=%f\n",y);
return 0;
}
Running results :

边栏推荐
- Why on earth is it not recommended to use select *?
- System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
- Explain various coordinate systems in unity in detail
- Want to draw a picture that belongs to you? AI painting, you can also
- The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
- H5 wechat shooting game source code
- Confusing knowledge points of software designer examination
- [1331. Array serial number conversion]
- What is data center? What value does the data center bring_ Light spot technology
- C # basic 5-asynchronous
猜你喜欢

prometheus配置alertmanager完整过程

Space shooting Lesson 10: score (painting and writing)

GIS数据漫谈(六)— 投影坐标系统

MobileViT:挑战MobileNet端侧霸主

What is data center? What value does the data center bring_ Light spot technology

Confusing knowledge points of software designer examination

Explain mesh Collider in unity

融合数据库生态:利用 EventBridge 构建 CDC 应用

How to use redis to realize things and locks?

"When you are no longer a programmer, many things will get out of control" -- talk to SUSE CTO, the world's largest independent open source company
随机推荐
How to build internal Wikipedia
#yyds干货盘点# 面试必刷TOP101:链表中的节点每k个一组翻转
1 Introduction to command mode
[C language brush questions] explanation of linked list application
[server data recovery] HP StorageWorks series storage RAID5 two disk failure offline data recovery case
Space shooting Lesson 10: score (painting and writing)
What is data center? What value does the data center bring_ Light spot technology
2 enjoy yuan mode
Why on earth is it not recommended to use select *?
Redis的三种删除策略以及逐出算法
什么是数据中台?数据中台带来了哪些价值?_光点科技
Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent
Cartoon JS shooting game source code
Confusing knowledge points of software designer examination
Lvs+keepalived high availability deployment practical application
H5 wechat shooting game source code
MoCo V1:视觉领域也能自监督啦
Unity foundation 6-rotation
MobileViT:挑战MobileNet端侧霸主
7/27 training log (bit operation + suffix array)