Closed roberthsu2003 closed 5 months ago
// 請將陣列變數,傳到別的function,並將所有元素*10 #33
#include <iostream>
using namespace std;
void change_value(int* nums){
for(int i=0; i<3 ; i++){
nums[i] *= 10;
}
}
int main(){
int nums[] = {10, 20, 30};
cout << "導入void change_value前" << endl;
for(int i=0; i<3 ; i++){
cout << "nums[" + to_string(i) + "]的值為:" << nums[i] << endl;
}
change_value(nums);
cout << "導入void change_value後" << endl;
for(int i=0; i<3 ; i++){
cout << "nums[" + to_string(i) + "]的值為:" << nums[i] << endl;
}
}
#include <iostream>
using namespace std;
void change_value(int *ptr_nums) {
*ptr_nums *= 10;
}
int main() {
int nums[] = {10, 20, 30};
for (int i = 0; i < 3; i++) {
change_value(nums+i);
cout << nums[i] << endl;
}
}
#include <iostream>
using namespace std;
void change_value(int* ptrn){ //宣告指標變數取得記憶體位址
*ptrn*=10;
}
int main() {
int nums[]={10,20,30};
int i;
for(i=0;i<3;i++){
change_value(&nums[i]);
}
for(i=0;i<3;i++){
cout<<"ptrn更改後的值"<<i+1<<"====="<<nums[i]<<endl;
}
}
#include <iostream>
using namespace std;
void chang(int* p){ //宣告指標變數
*p *= 10;
}
int main() {
int nums[] = {10,20,30};
int i;
for (i=0; i<3; i++){
chang(&nums[i]);
}
for (i=0; i<3; i++){
cout << "乘以10後的元素為:"<< i+1 << "=" << nums[i] << endl;
}
}
using namespace std;
void change_p(int p1 , int p2){
p1+=20; p2-=15; }
int main() {
int apple=55; int guava=100;
change_p(&apple, &guava); cout<<apple<<endl; cout<<guava<<endl;
return 0; }