[code language=”cpp”]
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
int input;
printf("Please input the radius! \n");
scanf("%d",&input);
const float PI=3.14159;
printf("The area=%f\n",PI*input*input);
system("pause");
return 0;
}<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]
陣列元素表示方法
索引值由0開始,直到(n-1)
Ex:
[code language=”cpp”]
#include <iostream>
using namespace std;
int main(){
int arr[10];
arr[2]=arr[0]*arr[1]; //將索引值0乘以索引值1
//並指定給索引值2的元素
}<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]
陣列初值設定
可以在陣列宣告時
即設定初值
資料型態 陣列名稱[個數]={初值1,初值2,…..,初值(n-1)};
Ex:
[code language=”cpp”]
#include <iostream>
using namespace std;
int main(){
int arr[5]={1,2,3,4,5};
}
[/code]
[code language=”cpp”]
#include <iostream>
using namespace std;
int main(){
int arr[5]={0}; //將所有元素都設為0
}<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]
[code language=”cpp”]
#include <iostream>
using namespace std;
int main(){
int arr[]={1,2,3,4,5}; //編譯器將會自動將陣列大小設為5
}
<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>
[/code]
- 元素個數>初值數目:將其餘元素設為0
- 元素個數<初值數目:編譯器顯示錯誤訊息