/*2014. 01 .11 by Arby*/ /*c++ primer 7.12 编程练习*/ #include <iostream> using namespace std; void give_value(int arr[], int size, int value); void show_array(int arr[], int size); int main() { /*初始化*/ const int arr_size = 5; int array_give[arr_size]; int value; /*操作*/ cout <<"give the value of you want:"; cin >> value; give_value(array_give, arr_size, value); show_array(array_give, arr_size); cin.get(); cin.get(); return 0; } /*因为数组传递的是原数组,因此返回类型不用管*/ void give_value(int arr[], int size, int value) { for(int i = 0; i < size; i++) { arr[i] = value; } } void show_array(int arr[], int size) { for(int i = 0; i < size; i++) { cout << arr[i] << " "; } }
4.
/*2014. 01 .11 by Arby*/ /*c++ primer 7.12 编程练习*/ #include <iostream> using namespace std; void give_value(int *begin, int *end, int value); void show_array(int arr[], int size); int main() { /*初始化*/ const int arr_size = 5; int array_give[arr_size]; int value; /*操作*/ cout <<"give the value of you want:"; cin >> value; give_value(array_give, array_give+arr_size, value); show_array(array_give, arr_size); cin.get(); cin.get(); return 0; } /*因为数组传递的是原数组,因此返回类型不用管, 因为这里要进行赋值,因此不能设置为const*/ void give_value(int *begin, int *end, int value) { int *ps; for(ps = begin; ps != end; ps++) { *ps = value; } } void show_array(int arr[], int size) { for(int i = 0; i < size; i++) { cout << arr[i] << " "; } }
5.
/*2014. 01 .11 by Arby*/ /*c++ primer 7.12 编程练习*/ #include <iostream> using namespace std; double give_max(double arr_size[], int size); int main() { /*初始化*/ const int arr_size = 4; double arr[arr_size] = {54, 23, 34, 59}; double max_value; /*操作*/ max_value = give_max(arr, arr_size); cout << "the max value is: " << max_value << endl; cin.get(); cin.get(); return 0; } double give_max(double arr_size[], int size) { if(size < 0) cout << "the size is wrong"; double max = arr_size[0]; for(int i = 0; i < size; i++) { if(arr_size[i] > max) { max = arr_size[i]; } } return max; }
8.
/*2014. 01 .11 by Arby*/ /*c++ primer 7.12 编程练习*/ /*字符串中字符的替换*/ #include <iostream> using namespace std; int replace(char *str, char c1, char c2); int main() { /*初始化*/ const int char_size = 20; char str[20] = "asfllksddsdhg"; char c1 = 'd'; char c2 = 'z'; int number = 0; /*操作*/ number = replace(str, c1, c2); cout << str << endl; cout << "the number is: " << number << endl; cin.get(); cin.get(); return 0; } int replace(char *str, char c1, char c2) { int count = 0; int i = 0; while(*(str+i) != '\0') { if(*(str+i) == c1) { *(str+i) = c2; count++; } i++; } return count; }
1
/*2014. 01 .11 by Arby*/ /*c++ primer 7.12 编程练习*/ /*条件检测*/ #include <iostream> using namespace std; double soft_average(double x, double y); int main() { /*初始化*/ double x, y; double result; /*操作*/ cout << "please enter two number of you want to accumulate: "; cin >> x >> y; while(x != 0 && y != 0) { result = soft_average(x, y); cout << "the result is: " << result << endl; cin>>x >> y; } cout << "enter find 0 causing quit." << endl; cin.get(); cin.get(); return 0; } double soft_average(double x, double y) { double result = 2.0 * x * y / (x + y); return result; }