数组
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //数据类型 数组名[数组长度] 6 //数据类型 数组名[数组长度] = {值1,值2....} 7 //数据类型 数组名[ ] = {值1,值2.... } 8 9 //1.数据类型 数组名[数组长度 10 int arr[5]; 11 arr[0]= 10; 12 arr[1]= 20; 13 arr[2]= 30; 14 arr[3]= 40; 15 arr[4]= 50; 16 17 //2.数据类型 数组名[数组长度] = {值1,值2....} 18 //在初始化数据时候,没有全部填写完,会用0来填补剩余的数据 19 int arr2[5]={10,20,30}; 20 for(int i=0;i<5;i++){ 21 cout << arr2[i] << endl; 22 } 23 24 //3.数据类型 数组名[ ] = {值1,值2.... } 25 int arr3[]={90,80,70,60,50,40,30,20,10,0}; 26 for(int j=0;j<6;j++){ 27 cout << arr3[j] << endl; 28 } 29 system("pause"); 30 31 return 0; 32 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //一维数组名称用途 6 //1.可以统计整个数组在内存中的长度 7 8 9 int arr[5]={1,2,3,4,5}; 10 cout << "每个元素所占用的空间为:" << sizeof(arr[0]) << endl; 11 cout << "整个数组所占用的空间为:" << sizeof(arr) << endl; 12 cout << "数组中的元素个数为:" << sizeof(arr)/sizeof(arr[0]) << endl; 13 //2.可以获取数组在内存中的首地址 14 cout << "数组的首地址为" << arr << endl; 15 cout << "数组中第一个元素地址为:" << (long long)&arr[0] << endl; 16 cout << "数组中第二个元素地址为:" << (long long)&arr[1] << endl; 17 18 //数组名是常量,不能赋值 19 20 system("pause"); 21 22 return 0; 23 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 int arr[5]={300,350,200,400,250}; 6 for(int a=0;a<5;a++){ 7 cout << arr[a] << " "; 8 } 9 cout << endl; 10 11 int max=0; 12 for(int i = 0;i<5;i++){ 13 if(arr[i]>max){ 14 max = arr[i]; 15 } 16 } 17 cout << "最大值:" << max << endl; 18 19 system("pause"); 20 21 return 0; 22 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //元素逆置 6 7 int arr[5]={1,2,3,4,5}; 8 cout << "元素逆置前:" << endl; 9 for(int a=0;a<5;a++){ 10 cout << arr[a] << endl; 11 } 12 13 int start = 0; 14 int end = sizeof(arr)/sizeof(arr[0])- 1; 15 16 while(start < end){ 17 18 int temp = arr[start]; 19 arr[start] = arr[end]; 20 arr[end] = temp; 21 start++; 22 end--; 23 } 24 25 cout << "元素逆置后:" << endl; 26 for(int j=0;j<5;j++){ 27 cout << arr[j] << endl; 28 } 29 30 31 system("pause"); 32 33 return 0; 34 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 int arr[9] = {1,8,5,4,6,7,9,12,2}; 6 for(int a = 0;a<9;a++){ 7 cout << arr[a] << " "; 8 } 9 cout << endl; 10 11 //总共排序轮数为元素个数-1 12 for(int i = 0;i<9-1;i++){ 13 14 //内层循环对比次数 = 元素个数-当前轮数-1 15 for(int j =0;j<9-i-1;j++) { 16 //如果第一个数字比第二个数字大,交换两个数字 17 if(arr[j]>arr[j+1]) { 18 int temp = arr[j]; 19 arr[j] = arr[j+1]; 20 arr[j+1] = temp; 21 } 22 } 23 } 24 25 //排序后输出 26 cout <<"排序后:" << endl; 27 for(int a = 0;a<9;a++){ 28 cout << arr[a] << " "; 29 } 30 cout << endl; 31 32 system("pause"); 33 34 return 0; 35 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 // 数据类型 数组名[ 行数 ][ 列数 ]; 6 // 数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } }; 7 // 数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4}; 8 // 数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4}; 9 10 11 //1.数据类型 数组名[ 行数 ][ 列数 ]; 12 int arr[2][3]; 13 arr[0][0]=1; 14 arr[0][1]=2; 15 arr[0][2]=3; 16 arr[1][0]=4; 17 arr[1][1]=5; 18 arr[1][2]=6; 19 20 // for(int i=0;i<2;i++){ 21 // for(int j=0;j<3;j++){ 22 // cout << arr[i][j] << endl; 23 // } 24 // } 25 26 //2. 数据类型 数组名[ 行数 ][ 列数 ] = { {数据1,数据2 } ,{数据3,数据4 } }; 27 int arr2[2][3] = 28 { 29 {1,2,3}, 30 {4,5,6} 31 32 }; 33 // for(int i=0;i<2;i++){ 34 // for(int j=0;j<3;j++){ 35 // cout << arr2[i][j] << " "; 36 // } 37 // cout << endl; 38 // } 39 40 //3.数据类型 数组名[ 行数 ][ 列数 ] = { 数据1,数据2,数据3,数据4}; 41 int arr3[2][3] = {1,2,3,4,5,6}; 42 // for(int i=0;i<2;i++){ 43 // for(int j=0;j<3;j++){ 44 // cout << arr3[i][j] << " "; 45 // } 46 // cout << endl; 47 // } 48 49 //4.数据类型 数组名[ ][ 列数 ] = { 数据1,数据2,数据3,数据4}; 50 51 int arr4[][3]={1,2,3,4,5,6}; 52 53 for(int i=0;i<2;i++){ 54 for(int j=0;j<3;j++){ 55 cout << arr4[i][j] << " "; 56 } 57 cout << endl; 58 } 59 60 61 system("pause"); 62 63 return 0; 64 }
1 #include<iostream> 2 using namespace std; 3 int main(){ 4 5 //查看二维数组所占内存空间 6 //获取二维数组的首地址 7 8 int arr[2][3]= 9 { 10 {1,2,3}, 11 {4,5,6} 12 }; 13 14 cout << "二维数组所占用内存空间为:" << sizeof(arr) << endl; 15 cout << "二维数组第一行占用内存为:" << sizeof(arr[0]) << endl; 16 cout << "二维数组第一个元素占用内存空间为" << sizeof(arr[0][0]) << endl; 17 cout << "二维数组行数:" << sizeof(arr)/sizeof(arr[0]) << endl; 18 cout << "二维数组列数:" << sizeof(arr[0])/sizeof(arr[0][0]) << endl; 19 20 //二维数组首地址 21 cout << "二维数组首地址为:" << (long long)arr << endl; 22 cout << "二维数组第一行首地址为:" << (long long)arr[0] << endl; 23 cout << "二维数组第二行首地址为:" << (long long)arr[1] << endl; 24 25 cout << "二维数组第一个元素首地址为:" << (long long)&arr[0][0] << endl; 26 cout << "二维数组第二个元素首地址为:" << (long long)&arr[0][1] << endl; 27 system("pause"); 28 return 0; 29 }
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main(){ 5 6 //张三 100 100 100 7 //李四 90 50 100 8 //王五 60 70 80 9 10 //创建二维数组 11 int scores[3][3] = 12 { 13 {100,100,100}, 14 {90,50,100}, 15 {60,70,80} 16 }; 17 string name[3]={"张三","李四","王五"} ; 18 //统计每个人的总和分数 19 for(int i =0;i<3;i++){ 20 int sum = 0; //统计分数总和变量 21 for(int j=0;j<3;j++){ 22 sum+= scores[i][j]; 23 } 24 cout << name[i] << "的成绩总和为:" << sum << endl; 25 } 26 27 system("pause"); 28 29 return 0; 30 }
函数
1 #include<iostream> 2 using namespace std; 3 // 函数的定义一般主要有5个步骤: 4 // 1、返回值类型 5 // 2、函数名 6 // 3、参数表列 7 // 4、函数体语句 8 // 5、return 表达式 9 10 //加法函数 11 12 int add(int num1,int num2){ 13 //num1 num2为形参 14 int sum =num1+num2; 15 return sum; 16 } 17 18 19 int main(){ 20 21 //main 中调用add函数 22 23 int a =10; 24 int b =20; 25 int c = add(a,b); 26 //a,b为实际参数,简称实参 27 cout << "c = " << c << endl; 28 29 system("pause"); 30 31 return 0; 32 }
1 #include<iostream> 2 using namespace std; 3 //函数常见样式 4 //无参无返 5 void test01(){ 6 cout << "this is test 01 " << endl; 7 } 8 //有参无返 9 void test02(int a){ 10 cout << "this is test 02 a = " << a << endl; 11 } 12 //无参有返 13 int test03(){ 14 cout << "this is test 03 " << endl; 15 return 1000; 16 } 17 //有参有返 18 int test04(int a){ 19 cout << "this is test 04 " << endl; 20 return a; 21 } 22 23 int main(){ 24 25 //调用 26 test01(); 27 28 test02(100); 29 30 int num1 = test03(); 31 cout << "num1 = " << num1 << endl; 32 33 int num2 = test04(10000); 34 35 cout << "num2 = " << num2 << endl; 36 37 system("pause"); 38 39 return 0; 40 }
1 #include<iostream> 2 using namespace std; 3 4 //函数的声明 5 //比较函数,实现两个整型数字进行比较,返回较大的值 6 7 //提前告知函数的存在,使用函数声明 8 //声明可以多次,但定义只有一次 9 int max(int a,int b); 10 11 12 int main(){ 13 int a =10; 14 int b =20; 15 cout << max(a,b) << endl; 16 17 system("pause"); 18 19 return 0; 20 } 21 int max(int a,int b){ 22 return a>b?a:b; 23 }
1 #include<iostream> 2 using namespace std; 3 4 //值传递 5 //定义函数,实现两个数字进行函数交换 6 //如果幻术不需要返回值,声明的时候可以写void 7 void swap(int num1,int num2){ 8 9 cout << "交换前:" << endl; 10 cout << "num1 = " << num1 << endl; 11 cout << "num2 = " << num2 << endl; 12 13 int temp = num1; 14 num1 = num2; 15 num2 = temp; 16 17 cout << "交换后:" << endl; 18 cout << "num1 = " << num1 << endl; 19 cout << "num2 = " << num2 << endl; 20 21 //return,返回值不需要的时候,可以不写 22 } 23 24 int main(){ 25 26 int a = 10; 27 int b = 20; 28 //当值传递时,函数的形参发生改变,并不影响实参 29 swap(a,b); 30 31 system("pause"); 32 33 return 0; 34 }
*函数的分文件编写
1 #include "swap.h" 2 3 //函数的分文件编写 4 //实现两个数字交换的函数 5 6 7 int main(){ 8 9 int a = 10; 10 int b = 20; 11 swap(a,b); 12 13 14 system("pause"); 15 16 return 0; 17 }
1 #include "swap.h" 2 //函数的定义 3 void swap(int a,int b){ 4 int temp = a; 5 a = b; 6 b = temp; 7 8 cout << "a= " << a << endl; 9 cout << "b= " << b << endl; 10 }
1 #include<iostream> 2 using namespace std; 3 4 //函数的声明 5 void swap(int a,int b);
备注:dev c++中头文件重复会导致编译后文件链接失败,为了避免同一个头文件被包含(include)多次,可以使用#pragma once
原文:https://www.cnblogs.com/stratus/p/14379285.html