1.数组定义方式
A.
B.
2.一维数组命名
#include<iostream> using namespace std; int main() { //int arr[] = { 12,23,69,89,78 }; int arr[10] = { 12,59,8,788,55,656 }; int num = sizeof(arr) / sizeof(arr[0]); cout << num << endl; cout <<(int) & arr << endl; cout << (int)&arr[0] << endl; //在这里每个地址都会相差4 cout << (int)&arr[1] << endl; return 0; }
3.冒泡排序
#include<iostream> using namespace std; int main() { int arr[10] = { 12,23,56,56,87,51,29,69,98,78 }; for (int i = 0; i < 10 - 1; i++) { for (int j = 0; j < 10 - 1 - j;j++) { if (arr[i] <arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (int num = 0; num < 10; num++) { cout << arr[num] << " "; } }
4.二维数组定义
5.二维数组组名
6.二维数组案例
#include<iostream> using namespace std; int main() { int arr[3][3] = { {12,23,69}, {52,89,54}, {12,23,69}, }; string name[3] = { "劳志驰","红威","宝宝" }; for (int i = 0; i < 3; i++) { int sum = 0; for (int j = 0; j < 3; j++) { sum += arr[i][j]; } cout << "name is" << name[i] << "grade is" << sum << endl; } return 0; }
7.二维数组的相关概念
#include<iostream> using namespace std; int main() { int arr[2][3] = { {12,23,69}, {52,89,54} }; cout << sizeof(arr) << endl; cout << sizeof(arr[0]) << endl; cout << sizeof arr[0][0] << endl; cout << sizeof(arr) / sizeof arr[0][0] << endl;//求数组数字个数 cout << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;//求列数 cout << sizeof(arr) / sizeof(arr[0]) << endl;//求行数 return 0; }
8.冒泡排序代码
#include<iostream> using namespace std; int main() { int arr[] = { 12,39,55,65,556,62,566,5662,22,22 }; int length = sizeof(arr) / sizeof(arr[0]); for (int i = 0; i < length-1; i++) { for (int j = 0; j < length - 1 - i; j++) { if (arr[j] <arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for (int i = 0; i < length; i++) { cout << arr[i] << endl; } return 0; }