例:二维数组的定义;
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int main()
{
int b[2][3]={
{1,2,3},{4,5,6}};
cout<<“数据元素b[0][0]值为”<<b[0][0]<<endl;
cout<<"数据元素b[0][3]值为"<<b[0][3]<<endl;
cout<<"数据元素b[1][1]值为"<<b[1][1]<<endl;
cout<<"数据元素b[2][3]值为"<<b[2][3]<<endl;
b[2][3]=2.8;
cout<<"数据元素b[2][3]值为"<<b[2][3]<<endl;
return 0;
}
数据元素b[0][0]值为1
数据元素b[0][3]值为4
数据元素b[1][1]值为5
数据元素b[2][3]值为0
数据元素b[2][3]值为2
Process exited after 0.01683 seconds with return value 0
请按任意键继续. . .
一:C++流程控制语句:
1.流程语句用于控制程序中各语句的执行功能,C++提供的流程控制语句有选择语句(if,switch),循环语句(for,while,do-while)和跳转语句(continue,break,goto,return)等三种语句;
例:输出十道减法题(for循环);
#include <iostream.h>
#include<stdlib.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int main()
{
int a,b,t,i;
for(i=1;i<=10;i++)
{
a=rand();
b=rand();
if(a<b)
{
t=a;a=b;b=t;
}
cout<<a<<’-’<<b<<’=’<<endl;
}
return 0;
}
18467-41=
26500-6334=
19169-15724=
29358-11478=
26962-24464=
28145-5705=
23281-16827=
9961-491=
11942-2995=
5436-4827=
Process exited after 0.02377 seconds