C语言课本实例
1. 将一维数组的内容倒顺
#include <stdio.h>
void func(int *s,int n)
{
int i,temp;
for(i=0;i<n/2;i++)
{
temp=s[i];
s[i]=s[n-1-i];
s[n-1-i]=temp;
}
}
void main()
{
int i,a[10];
for(i=0;i<10;i++)
a[i]=i;
func(a,10);
for(i=0;i<10;i++)
printf("%d\n",a[i]);
}
2. 数组遍历
#include <stdio.h>
#define N 5
int search(int *s,int x)//s[N]
{
int i;
for(i=0;i<N;i++) if(s[i]==x) break;
if(i<N) return 1;
else return 0;
}
void main()
{
int a[N],i,e;
printf("Enter the array:\n");
for(i=0;i<N;i++) scanf("%d",&a[i]);
printf("Enter the Element:\n");
scanf("%d",&e);
if(search(a,e)==1) printf("The element %d exists in the array.\n",e);
else printf("Can not find the element %d.\n",e);
}
3. 用选择法对数组排序
#include <stdio.h>
#define N 6
void sort(int *s,int n);
void main()
{
int a[N],i;
printf("enter the array:\n");
for(i=0;i<N;i++) scanf("%d",&a[i]);
sort(a,N);
printf("the sorted array:\n");
for(i=0;i<N;i++) printf("%d ",a[i]);
printf("\n");
}
void sort(int *s,int n)
{
int i,j,p,t;
for(i=0;i<n;i++)
{
p=i;
for(j=i+1;j<n;j++)
if(s[j]<s[p])
p=j;
t=s[p];
s[p]=s[i];
s[i]=t;
}
}
4.
C语言课本实例的更多相关文章
- MATLAB与C语言对比实例:随机数生成
MATLAB与C语言对比实例:随机数生成 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 一.整型随机数生成函数 1.C语言程序 int intrand ...
- c语言课本及pta作业中运用到的程序思维
c语言课本运用到的程序思维 我个人觉得在写程序的时候,有很多题目会用到我们学过的解决一个程序或者一个问题的方法,把这些方法运用起来,将会使自己更加灵活地解决诸多问题,为今后打下良好地基础. (因为还没 ...
- R语言入门级实例——用igragh包分析社群
R语言入门级实例——用igragh包分析社群 引入—— 本文的主要目的是初步实现R的igraph包的基础功能,包括绘制关系网络图(social relationship).利用算法进行社群发现(com ...
- c语言字符串实例
例子:涉及字符串.字符.指针.++等 例一:字符串与字符 #include <stdio.h> void reverse(char *str) { char *end=str; print ...
- c语言编程实例——小球跳动
1.预备知识 1.1 相关头文件 "#include"是c语言中用以申明所需调用的库函数或自定义函数的头文件路径及文件名.#include ""和#includ ...
- R语言学习——实例标识符
> patientID<-c(1,2,3,4)> age<-c(25,34,28,52)> diabetes<-c("Type1"," ...
- 分离链接散列表C语言实现实例
/* hash_sep.h */ #ifndef _HASH_SEP_H #define _HASH_SEP_H #define MIN_TABLE_SIZE 5 struct list_node; ...
- 在Golang中使用C语言代码实例
转自:http://www.jb51.net/article/56720.htm cgo 使得在 Golang 中可以使用 C 代码. Hello World 为了有一个较为直观的了解,我们来看一个简 ...
- C语言——杂实例
#include <stdio.h> #include <stdlib.h> #include <string.h> void f (int **p); void ...
随机推荐
- 【问题&解决】手机网页Html代码实现(解决显示页面很小的问题)
工作需要,要做一个手机自适应的网页效果,终于搞定,先分享并记录! 其实主要就是改掉HTML页面声明: 在网页中加入以下代码,就可以正常显示了: <meta name="viewport ...
- 【转】Android APK的数字签名的作用和意义
1. 什么是数字签名? 数字签名就是为你的程序打上一种标记,来作为你自己的标识,当别人看到签名的时候会知道它是与你相关的 2. 为什么要数字签名? 最简单直接的回答: 系统要求的. Andr ...
- Java实现FTP上传下载功能
Java FTP客户端工具包很多,在此我选用的Apache的FTPClient.这个包的获取可以通过http://commons.apache.org/net/来获取,我使用的是最新的commons- ...
- Solve error: Cannot open include file: 'X11/Xlocale.h': No such file or directory
When you use FLTK with VS2010, you may get the error: fatal error C1083: Cannot open include file: ' ...
- Yii源码阅读笔记(三)
接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— public static function getRootAlias($alias)// ...
- Ruby--Array
--后面连接其它数组:[ARRAY].concat([OTHER ARRAY]) --排序:sort,进阶:sort_by{|obj| obj.[VALUE]} --随机获取:[ARRAY].samp ...
- lvs简单配置
负载均衡服务器将会用到两块网卡,eth0为公网地址(此处将局域网ip作为公网地址),IP地址为192.168.1.104,eth0:1,IP地址为192.168.2.1在负载均衡器上添加一个ip别名, ...
- 学习之道-从求和起-求和曲线面积瞬时速率极限微积分---求和由高解低已知到未知高阶到低阶连续自然数的K次方之和
数学分析 张筑生
- breadth-first depth-first best-first
Computer Science An Overview _J. Glenn Brookshear _11th Edition For our example in Figure 11.7, we c ...
- 浏览器指纹 - HTTP cookie
http://www.iefans.net/ruhe-fangfan-xielu-shangwang-yinsi/ http://www.iefans.net/cookie-yinsi-guanxi/ ...