C语言数组和函数实例练习(一)
C语言的数组和函数部分的知识,在语法上和Java语法是有所相似的,这里只通过实例总结一些自己感觉需要理解的部分知识。
1.数组
数组中的元素具有相同的数据类型;数组一旦创建,不能被改变;数组中元素在内存中是连续依次存在的;使用时需要随时注意下标越界的问题。
例1:输入数量不确定的[0,9]范围内的整数,统计每个数字出现的次数,输入-1时结束程序。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int num[]={};
scanf("%d",&i);
while(i!=-){
if(i>=&&i<=){
num[i]++;
}
scanf("%d",&i);
}
for(i=;i<;i++)
{
printf("%d出现了%d次\n",i,num[i]);
}
return ;
}
例2:在一组给定的数据中,找出某个数据是否存在。
#include <stdio.h>
#include <stdlib.h>
int search(int key,int a[],int length)
{
int ret=-;
int i;
for(i=;i<length;i++)
{
if(a[i]==key)
{
ret=i;
break;
}
}
return ret;
}
int main()
{
int a[]={,,,,,,,,,};
int x;
int loc;
printf("请输入一个数字:");
scanf("%d",&x);
loc=search(x,a,);
if(loc!=-)
{
printf("%d在第%d个位置上。\n",x,loc);
}else{
printf("%d不存在!");
}
return ;
}
二维数组:int a[i][j];//i行j列的数组
例3:将一个二维数组行和列的元素互换,存到另一个二维数组中。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[][]={{,,},{,,}};
int b[][],i,j;
printf("array a:\n");
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
printf("%5d",a[i][j]);
b[j][i]=a[i][j];
}
printf("\n");
}
printf("array b:\n");
for(i=;i<=;i++)
{
for(j=;j<=;j++)
{
printf("%5d",b[i][j]);
}
printf("\n");
}
return ;
}
2.函数
C语言中的函数和Java中的方法是一个道理的,都是一段实现某种功能的代码块,也都要注意返回值、形参实参等。
***注意:若函数有返回值,必须使用带值的return。
C语言在调用函数时,永远只能传值给函数,不能传变量。
C语言不允许函数嵌套定义。
例1:求1-10,20-30,45-60三个区间数字的总和。
#include <stdio.h>
#include <stdlib.h>
int sum(int a,int b)
{
int i;
int sum;
for(i=a;i<=b;i++)
{
sum=sum+i;
}
return sum;
}
int main()
{
int x,y,z,result;
x=sum(,);
y=sum(,);
z=sum(,);
result=x+y+z;
printf("这三组数的和为%d",result);
}
例2:swap()无法实现交换两个变量的值。
#include <stdio.h>
#include <stdlib.h>
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int a=;
int b=;
swap(a,b);
printf("a=%d,b=%d",a,b);
return ;
}
原因是:C语言在调用函数时,永远只能传值给函数,不能传变量。
C语言数组和函数实例练习(一)的更多相关文章
- C语言数组和函数实例练习(二)
1.C语言中不允许函数的嵌套定义,但可以使用函数的嵌套调用. 例1:输入4个整数,找出其中最大的数. #include <stdio.h> #include <stdlib.h> ...
- GO语言数组和切片实例详解
本文实例讲述了GO语言数组和切片的用法.分享给大家供大家参考.具体分析如下: 一.数组 与其他大多数语言类似,Go语言的数组也是一个元素类型相同的定长的序列. (1)数组的创建. 数组有3种创建方式: ...
- C语言 数组做函数参数退化为指针的技术推演
//数组做函数参数退化为指针的技术推演 #include<stdio.h> #include<stdlib.h> #include<string.h> //一维数组 ...
- php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】
本文实例总结了php常用数组array函数.分享给大家供大家参考,具体如下: array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 案例: <?php ...
- C语言 数组做函数参数不传数组个数的遍历方法
//数组做函数参数不传数组个数的遍历方法 #include<stdio.h> #include<stdlib.h> #include<string.h> void ...
- C语言数组作为函数参数
数组可以作为函数的参数使用,进行数据传送. 数组用作函数参数有两种形式,一种是把数组元素(下标变量)作为实参使用:另一种是把数组名作为函数的形参和实参使用. 数组元素作函数实参 数组元素就是下标变量, ...
- R语言数组array函数
数组是一个可以在两个以上的维度存储数据的R数据对象.例如 - 如果创建尺寸(2,3,4)的数组,那么创建4个矩形矩阵每2行3列.数组只能存储数据类型. 使用 array()函数创建数组.它需要向量作为 ...
- 小学生都看得懂的C语言入门(3): 数组与函数
#include <stdio.h> int main() { int x; ; ; scanf("%d",&x){ sum+=x; cnt++; scanf( ...
- 5_PHP数组_3_数组处理函数及其应用_5_数组遍历语言结构
以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组遍历语言结构 1. foreach ( array as $value ) 程序: <?php $int ...
随机推荐
- window.dialogArguments
弹出子窗口window.showModalDialog( url, window ); 然后在弹出的子窗口中: window.dialogArguments 即为父窗口window对象的引用.想搞什么 ...
- Hive2.1.1集群搭建
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- Deutsch lernen (01)
Was macht Martin? - Um 8.00 Uhr steht martin auf. aufstehen - aufstand - ist aufgestanden 起床 Um 6 Uh ...
- 图像局部显著性—点特征(SiftGPU)
SIFT的计算复杂度较高. SiftGpu的主页:SiftGPU: A GPU Implementation of ScaleInvariant Feature Transform (SIFT) 对S ...
- win8使用教程
win8如何关机 http://product.pconline.com.cn/itbk/software/win8/1305/3301394.html shutdown.exe -s -t 00 W ...
- 【sqli-labs】 less33 GET- Bypass AddSlashes (GET型绕过addslashes() 函数的宽字节注入)
和less32一样,对关键字符进行了添加\ 关于addslashes()函数 payload和less32一样 http://192.168.136.128/sqli-labs-master/Less ...
- Maven常见异常及解决方法
异常1: [ERROR] Failed to execute goal on project biz_zhuhai: Could not resolve dependencies for projec ...
- BZOJ 1645: [Usaco2007 Open]City Horizon 城市地平线 扫描线 + 线段树 + 离散化
Code: #include<cstdio> #include<algorithm> #include<string> #define maxn 1030000 # ...
- 移动端调试 vConsole
<head> <script src="path/to/vconsole.min.js"></script> <script> va ...
- [SDFZOJ]1069:树上统计
神题...std丑的不行. 我们可以发现i->i+1的边被覆盖过i×(n-i)次. 因为以1->i为左端点,以i+1->n的为右端点,i->i+1都将被覆盖这么多次. 然后从1 ...