![c++課件第五章數(shù)組_第1頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/1/1c04a13d-30e1-4303-9ada-970c887c23e4/1c04a13d-30e1-4303-9ada-970c887c23e41.gif)
![c++課件第五章數(shù)組_第2頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/1/1c04a13d-30e1-4303-9ada-970c887c23e4/1c04a13d-30e1-4303-9ada-970c887c23e42.gif)
![c++課件第五章數(shù)組_第3頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/1/1c04a13d-30e1-4303-9ada-970c887c23e4/1c04a13d-30e1-4303-9ada-970c887c23e43.gif)
![c++課件第五章數(shù)組_第4頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/1/1c04a13d-30e1-4303-9ada-970c887c23e4/1c04a13d-30e1-4303-9ada-970c887c23e44.gif)
![c++課件第五章數(shù)組_第5頁](http://file1.renrendoc.com/fileroot_temp2/2020-10/1/1c04a13d-30e1-4303-9ada-970c887c23e4/1c04a13d-30e1-4303-9ada-970c887c23e45.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 第五章 數(shù) 組5.1 數(shù)組的概念5.2 一維數(shù)組的定義和引用5.2.1 定義一維數(shù)組類型標識符 數(shù)組名常量表達式int a10;數(shù)組從零開始;常量表達式中不能有變量,即C+不允許對數(shù)組的大小作動態(tài)定義。數(shù)組變量占用內(nèi)存的大小可用數(shù)組的大小乘上其元素的大小計算。5.2.2 引用一維數(shù)組的元素數(shù)組名下標a0=a5+a7-a2*3;例5.1 數(shù)組元素的引用#include using namespace std;void main()int i,a10;for(i=0;i=0;i-)coutai ;coutendl;9 8 7 6 5 4 3 2 1 05.2.3 一維數(shù)組的初始化(1) int
2、a10=0,1,2,3,4,5,6,7,8,9;(2) int a10=0,1,2,3,4;則后5個默認為0(3) int a=0,1,2,3,4;則a是有五個元素的數(shù)組5.2.4 一維數(shù)組程序舉例例5.2 用數(shù)組來處理求Fibonacci數(shù)列問題.#include #include using namespace std;void main()int i,f20=1,1;for(i=2;i20;i+)fi=fi-2+fi-1;for(i=0;i20;i+)if(i%5=0) coutendl; coutsetw(8)fi;coutendl; 1 1 2 3 5 8 13 21 34 55 8
3、9 144 233 377 610 987 1597 2584 4181 67655.3 二維數(shù)組的定義和使用5.3.1 定義二維數(shù)組類型標識符 數(shù)組名常量表達式 常量表達式int a34;數(shù)組元素是按行存放的。5.3.2 二維數(shù)組的引用a12=6;5.3.3 二維數(shù)組的初始化(1) int a34=1,2,3,4,5,6,7,8,9,10,11,12;(2) int a34=1,2,3,4,5,6,7,8,9,10,11,12;(3) int a34=1,5,9;(4) int a4=2,3,4,0,10;則數(shù)組共有三行。 5.3.4 二維數(shù)組程序舉例例5.4 將一個二維數(shù)組行和列元素互換,
4、存到另一個二維數(shù)組中。#include using namespace std;void main()int a23=1,2,3,4,5,6;int b32,i,j;coutarray a:endl;for(i=0;i2;i+)for(j=0;j3;j+)coutaij ;coutendl;coutarray b:endl;for(i=0;i3;i+)for(j=0;j2;j+)cout(bij=aji) ;coutendl;array a:1 2 34 5 6array b:1 42 53 6例5.5 有一個3X4的矩陣,要求編程序求出最大值,以及其所在的行號和列號。#include usi
5、ng namespace std;void main()int i,j,row=0,colum=0,max;int a34=5,12,23,56,19,28,37,46,-12,-34,6,8;max=a00;for(i=0;i3;i+)for(j=0;jmax)max=aij;row=i;colum=j;coutmax=max,row=row,colum=columendl;max=56,row=0,colum=35.4 用數(shù)組名作函數(shù)參數(shù)1. 用數(shù)組元素作函數(shù)實參例5.6 用函數(shù)處理例5.5#include using namespace std;void main()bool if_ma
6、x(int x,int max);int i,j,row=0,colum=0,max;int a34=5,12,23,56,19,28,37,46,-12,-34,6,8;max=a00;for(i=0;i3;i+)for(j=0;j4;j+)if(if_max(aij,max)max=aij;row=i;colum=j;coutmax=max,row=row,colum=colummax) return true;else return false;數(shù)組元素可以用在該類型變量可用的任何地方。2. 用數(shù)組名作函數(shù)參數(shù)例5.7 用選擇法對數(shù)組中10個整數(shù)按由小到大排序。3,6,1,9,4#inc
7、lude using namespace std;void main()void select_sort(int array,int n);int a10,i;coutenter the original array:endl;for(i=0;iai;select_sort(a,10);coutthe sorted array:endl;for(i=0;i10;i+) coutai ;coutendl;void select_sort(int array,int n)int i,j,k,t;for(i=0;in-1;i+)k=i;for(j=i+1;jn;j+)if(arrayjarrayk)
8、k=j;t=arrayk;arrayk=arrayi;arrayi=t;enter the original array:6 9 -2 56 87 11 -54 3 0 77the sorted array:-54 -2 0 3 6 9 11 56 77 87關(guān)于數(shù)組名作函數(shù)參數(shù)有兩點要說明:(1) 函數(shù)形參是數(shù)組名,實參也是數(shù)組名。(2) 數(shù)組名代表數(shù)組首元素的地址,并不代表數(shù)組中的全部元素。3. 用多維數(shù)組名作函數(shù)參數(shù)例5.8 有一個3X4的矩陣,求矩陣中所有元素中的最大值。要求用函數(shù)處理。#include using namespace std;void main()int max_va
9、lue(int array4);int a34=11,32,45,67,22,44,66,88,15,72,43,37;coutmax value is max_value(a,3)endl;int max_value(int array4,int n)int i,j,max;max=array00;for(i=0;in;i+)for(j=0;jmax) max=arrayij;return max;max value is 885.5 字符數(shù)組5.5.1 字符數(shù)組的定義和初始化char c10;c0=I;c1= ;char c10=I, ,a,m, ,h,a,p,p,y;5.5.2 字符數(shù)組
10、的賦值和引用例5.9 設(shè)計和輸出一個鉆石圖形。#include using namespace std;void main()char diamond5= , ,*, ,*, ,*,*, , , ,*, ,*, ,*, , ,*;int i,j;for(i=0;i5;i+)for(j=0;j5;j+)coutdiamondij;coutendl; * * * * * * *5.5.3 字符串和字符串結(jié)束標志 0char str=I am happy;與char str11=I, ,a,m, ,h,a,p,p,y;相同char str11=I, ,a,m, ,h,a,p,p,y,0;5.5.4
11、字符數(shù)組的輸入與輸出(1) 逐個字符輸入輸出(2) 將整個字符串一次輸入或輸出#include using namespace std;void main()char str=I am happy;char str111=I, ,a,m, ,h,a,p,p,y;char str211=I, ,a,m, ,h,a, p,p,y,0;char str310=I, ,a,m, ,h,a,p,p,y;cinstr;coutstr1endl;coutstr2endl;coutstr3endl;I am happyI am happyI am happyI am happy5.5.5 字符串處理函數(shù)1.
12、字符串連接函數(shù)strcatstrcat(char,const char);例#include using namespace std;void main()char str130=Peoples Republic of ;char str2=china;strcat(str1,str2);coutstr1endl;Peoples Republic of china2. 字符串復(fù)制函數(shù)strcpystrcpy(char,const char); 例#include using namespace std;void main()char str130=Peoples Republic of;cha
13、r str2=china;strcpy(str1,str2);coutstr10) coutyes;4. 字符串的長度strlen(const char);#include using namespace std;void main()char str10=china;coutstrlen(str)endl;55.5.6 字符串應(yīng)用舉例例5.10 有3個字符串,要求找出其中最大者,要求用函數(shù)調(diào)用。#include using namespace std;void main()void max_string(char str30,int i);int i;char country_name330
14、;for(i=0;icountry_namei;max_string(country_name,3);void max_string(char str30,int n)char string30;strcpy(string,str0);if(strcmp(str1,string)0) strcpy(string,str1);if(strcmp(str2,string)0) strcpy(string,str2);coutthe largest string is:stringendl;CHINAGERMANYFRANCHthe largest string is:GERMANY5.6 C+處理
15、字符串的方法-字符串類與字符串變量string不是C+語言的基本數(shù)據(jù)類型,它是C+標準庫中聲明的一個字符串類。使用時應(yīng)加 #include 5.6.1 字符串變量的定義和引用1 定義字符串變量string string1;string string2=China;2. 對字符串變量的賦值string1=Canada;char str10;str=Hello!; /錯誤string1=string2;string word= Then;word2=a;3. 字符串變量的輸入和輸出cinstring1;coutstring2;例#include #include using namespace s
16、td;void main()string string1;string string2=China;string1=Canada;char str10;/str=Hello!; /錯誤string1=string2;string word= Then;word2=a;cinstring1;coutstring2endl;coutword = =#include #include using namespace std;void main()string string1=C+ ;string string2=Language;string1+=string2;coutstring1endl;5.
17、6.3 字符串?dāng)?shù)組#include #include using namespace std;void main()string name5=Zhang,Wang,Li,Zhao,Tan;coutname1.length()endl;45.6.4 字符串運算舉例例5.11 輸入3個字符串,要求將字母按由小到大的順序輸出。#include #include using namespace std;void main()string string1,string2,string3,temp;coutstring1string2string3;if(string2string3)temp=string2;string2=string3;string3=temp;if(string1=string2) coutstring1 string2 string3endl;else if(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年春八年級歷史下冊 第11課 城鄉(xiāng)人民生存狀態(tài)的滄桑巨變說課稿1(pdf) 川教版
- Unit 2 Understanding each other Project 說課稿-2023-2024學(xué)年高中英語牛津譯林版(2020)選擇性必修第四冊
- Unit 6 Meet my family Part B Let's talk Let's learn大單元整體說課稿表格式-2024-2025學(xué)年人教PEP版英語四年級上冊
- 2024年秋七年級生物上冊 3.5.2 綠色植物的呼吸作用說課稿 (新版)新人教版001
- 葡萄園立柱施工方案
- 2023三年級數(shù)學(xué)下冊 三 美麗的街景-兩位數(shù)乘兩位數(shù)信息窗1 美麗的街燈第2課時說課稿 青島版六三制
- 預(yù)制水泥臨時圍墻施工方案
- 臨時合同范例復(fù)制
- 西安電動推拉雨棚施工方案
- 2024秋一年級語文上冊 漢語拼音 11 ie üe er說課稿 新人教版
- 2024美團簡化版商家合作合同標準文本一
- 2025年貴州黔源電力股份有限公司招聘筆試參考題庫含答案解析
- 《休閑食品加工技術(shù)》 課件 1 休閑食品生產(chǎn)與職業(yè)生活
- 春季開學(xué)安全第一課
- 2025年新生兒黃疸診斷與治療研究進展
- 廣東大灣區(qū)2024-2025學(xué)年度高一上學(xué)期期末統(tǒng)一測試英語試題(無答案)
- 2025年四川中煙工業(yè)限責(zé)任公司招聘110人高頻重點提升(共500題)附帶答案詳解
- 課題申報書:數(shù)智賦能高職院校思想政治理論課“金課”實踐路徑研究
- 公司安全生產(chǎn)事故隱患內(nèi)部報告獎勵工作制度
- H3CNE認證考試題庫官網(wǎng)2022版
- 感統(tǒng)訓(xùn)練培訓(xùn)手冊(適合3-13歲兒童)
評論
0/150
提交評論