我是靠谱客的博主 忧虑小馒头,这篇文章主要介绍C++ primer函数:按值传递,修改数组,const double array[],现在分享给大家,希望可以做个参考。

//第七章:函数
//函数基本知识
//函数原型
//按值传递函数参数
//设计处理数组的函数
//使用const指针参数
//设计处理文版字符串的函数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream> using namespace std; const int Max = 5; int fill_array(double ar[], int limit); void show_array(const double ar[], int n); void revalue(double r, double ar[], int n); int main() { double properties[Max]; int size = fill_array(properties,Max); show_array(properties,size); if(size >0) { cout << "Enter revaluation factor: "; double factor; while(!(cin >> factor)) { cin.clear(); while(cin.get() != 'n') continue; cout << "Bad input: Please enter a number: "; } revalue(factor, properties,size); show_array(properties,size); } cout << "Done.n"; cin.get(); cin.get(); return 0; } //数组接受有效的非负数值,输入的有效值越多返回的有效值个数越大 int fill_array(double ar[], int limit) { using namespace std; double temp; int i; for(i = 0; i<limit; i++) { cout << "Enter value #" << (i+1) << " : "; cin>>temp; if(!cin) { cin.clear(); while(cin.get() != 'n') continue; cout << "bad input input process terminated.n"; break; } else if(temp<0) break; ar[i] = temp; } return i; } //显示值 void show_array(const double ar[], int n) { using namespace std; for(int i=0;i<n;i++) { cout << "property #"<<(i+1) <<": "; cout << ar[i] << endl; } } //改变数组的值 void revalue(double r, double ar[], int n) { for(int i =0; i< n; i++) { ar[i] *=r; } } 实验结果: Enter value #1 : 1 Enter value #2 : 2 Enter value #3 : 3 Enter value #4 : 4 Enter value #5 : 5 property #1: 1 property #2: 2 property #3: 3 property #4: 4 property #5: 5 Enter revaluation factor: 2 property #1: 2 property #2: 4 property #3: 6 property #4: 8 property #5: 10 Done.

在这里插入图片描述
在这里插入图片描述

最后

以上就是忧虑小馒头最近收集整理的关于C++ primer函数:按值传递,修改数组,const double array[]的全部内容,更多相关C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部