数组数据
3.1数组数据类型
数组名和下标惟一地标识一个数组中的一个元素。同一数组中的每一个元素都必须属于同一数据类型。一个数组在内存中占一片连续的存储单元。
3.1.1一维数组
定义一维数组的一般格为:类型标识符 数组名[常量表达式];
int a[10];它表示整型数组数组名为a且数组有10个元素。
int a[]={1,2,3,4,5};int a[10]={0,1,2,3,4,5,6,7,8,9};
3.1.2一维数组举例
- //数组赋值倒序输出
#include <iostream>
using namespace std;
int main(){
int a,arr[10];
for(a=0;a<10;a++){
arr[a]=a;
}
for(a=9;a>=0;a--){
cout<<arr[a]<<' ';
}
return 0;
}
- //用数组来处理求Fibonacci数列问题。可以用20个元素代表数列中的20个数,从第3个数开始,可以直接用表达式f[i]=f[i-2]+f[i-1]求出各数。
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int i;
//F[0]=1,F[1]=1;
int F[20] = {1,1};
for(i=2;i<20;i++){
//F[2]=F[1]+F[1];以此类推
F[i]=F[i-2]+F[i-1];
}
for(i=0;i<20;i++){
//每五个数据进行换行
if(i%5==0) cout<<'\n';
//每个数据输出时占8列宽度
cout<<setw(8)<<F[i];
}
return 0;
}
- //把数字从小到大排序输出
#include <iostream>
using namespace std;
int main( ){
int a[10];
int i,j,t;
cout<<"input 10 numbers :"<<endl;
//输入a[1]~a[10]
for (i=1;i<11;i++)
cin>>a[i];
cout<<endl;
for (j=1;j<=9;j++)
for(i=1;i<=10-j;i++)
if(a[i]>a[i+1]){
//交换两个数的位置,使小数上浮
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
cout<<"the sorted numbers :"<<endl;
for(i=1;i<11;i++){
cout<<a[i]<<' ';
}
return 0;
}
3.1.3二维数组
类型标识符 数组名[常量表达式][常量表达式]
float a[3][4];定义a为3×4(3行4列)的单精度数组
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
3.1.4二维数组举例
- //有一个3×4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号。
#include <iostream>
using namespace std;
int main(){
int i,j;
int arr[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int max = arr[0][0];
for(i=0;i<3;i++){
for(j=0;j<4;j++){
if(max<arr[i][j]){
max=arr[i][j];
}
}
}
cout<<max<<' '<<i<<' '<<j<<'\n';
return 0;
}
#include <iostream>
using namespace std;
int main()
{ int i,j,row=0,colum=0,max;
int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}};
max=a[0][0];
for (i=0;i<=2;i++)
for (j=0;j<=3;j++)
if (a[i][j]>max)
{max=a[i][j];
row=i;
colum=j;
}
cout<<"max="<<max<<",row="<<row<<",colum="<<colum<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{ int max_value(int x,int max);
int i,j,row=0,colum=0,max;
int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}};
max=a[0][0];
for (i=0;i<=2;i++)
for (j=0;j<=3;j++)
{
max=max_value(a[i][j],max);
if(max==a[i][j])
{ row=i;
colum=j;
}
}
cout<<"max="<<max<<",row="<<row<<",colum="<<colum<<endl;
return 0;
}
int max_value(int x,int max)
{ if(x>max) return x;
else return max;
}
#include <iostream>
using namespace std;
int main()
{void select_sort(int array[],int n);
int a[10],i;
cout<<"enter the originl array:"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
cout<<endl;
select_sort(a,10);
cout<<"the sorted array:"<<endl;
for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void select_sort(int array[],int n)
{int i,j,k,t;
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(array[j]<array[k]) k=j;
t=array[k];array[k]=array[i];array[i]=t;
}
}
#include <iostream>
using namespace std;
int main()
{int max_value(int array[][4]);
int a[3][4]={{11,32,45,67},{22,44,66,88},{15,72,43,37}};
cout<<"max value is "<<max_value(a)<<endl;
return 0;
}
int max_value(int array[][4])
{int i,j,max;
max=array[0][0];
for( i=0;i<3;i++)
for(j=0;j<4;j++)
if(array[i][j]>max) max=array[i][j];
return max;
}
#include <iostream>
using namespace std;
int main()
{char diamond[][5]={{' ',' ','*'},{' ','*',' ','*'},{'*',' ',' ',' ','*'},{' ','*',' ','*'},{' ',' ','*'}};
int i,j;
for (i=0;i<5;i++)
{for (j=0;j<5;j++)
cout<<diamond[i][j];
cout<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2],i,j;
cout<<"array a:"<<endl;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
{ cout<<a[i][j]<<" ";
b[j][i]=a[i][j];
}
cout<<endl;
}
cout<<"array b:"<<endl;
for (i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{ void max_string(char str[][30],int i);
int i;
char country_name[3][30];
for(i=0;i<3;i++)
cin>>country_name[i];
max_string(country_name,3);
return 0;
}
void max_string(char str[][30],int n)
{
int i;
char string[30];
strcpy(string,str[0]);
for(i=0;i<n;i++)
if(strcmp(str[i],string)>0) strcpy(string,str[i]);
cout<<endl<<"the largest string is¡Ã"<<string<<endl;
}
3.1.5数组字符串处理函数
1. 字符串连接函数 strcat
#include <iostream>
#include <string.h>
using namespace std;
int main(){
char str1 [30]="People′s Republic of ";
char str2 []="China";
cout<<strcat(str1,str2)<<endl;
return 0;
}
2. 字符串复制函数strcpy
#include <iostream>
#include <string.h>
using namespace std;
int main(){
char str1 []="China";
char str2 [10];
strcpy(str2,str1);
cout<<str2<<endl;
cout<<strcat(str1,str2)<<endl;
return 0;
}
3. 字符串比较函数strcmp
#include <iostream>
#include <string.h>
using namespace std;
int main(){
char str1 []="China";
char str2 []="Chin";
if(strcmp(str1,str2)>0) cout<<"yes!";
return 0;
}
4. 字符串长度函数strlen
#include <iostream>
#include <string.h>
using namespace std;
int main(){
char str1 []="China";
char str2 []="Chin";
cout<<strlen(str1)<<' '<<strlen(str2)<<'\n';
return 0;
}
3.1.6 数组字符串函数举例
- //有3个字符串,要求找出其中最大者。要求用函数调用。
#include <iostream>
#include <string.h>
using namespace std;
int main( ){
void max_string(char str[][30],int i);
int i;
char country_name [3][30];
//输入3个国家名
for(i=0;i<3;i++){
cin>>country_name [i];
}
//调用max_string函数
max_string(country_name,3);
return 0;
}
void max_string(char str [][30],int n){
int i;
char string [30];
//使string的值为str[0]的值
strcpy(string,str[0]);
for(i=0;i<n;i++){
if(strcmp(str[i],string)>0)
cout<<endl<<"the largest string is:"<<string<<endl;
}
}
运行结果如下:CHINA↙GERMANY↙FRANCH↙
the largest string is: GERMANY