roberthsu2003 / cAndC-

48 stars 18 forks source link

請將陣列變數,傳到別的function,並將所有元素*10 #33

Closed roberthsu2003 closed 5 months ago

roberthsu2003 commented 7 months ago
int main(){
int nums = {10, 20, 30};
...
change_value(xxxxx);
...
nums的值改變數內的元素值是
}

void change_value(xxxxxxxx){
    xxxxxx
    xxxxxxx
}
a86685566 commented 7 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;
  }
}
cft67ujm commented 7 months ago
#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;
    }
}
jackak1003 commented 7 months ago
#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;
  }

}
Lin20020301 commented 7 months ago
#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;
  }
}
9999erin commented 7 months ago

include

include

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; }