roberthsu2003 / cAndC-

51 stars 18 forks source link

請依下列圖片建立陣列和function,最後輸出陣列內的值 #44

Closed roberthsu2003 closed 1 month ago

roberthsu2003 commented 3 months ago
截圖 2024-06-18 晚上9 53 34
Norri2 commented 3 months ago
#include <iostream>
using namespace std;

void map_val_to_arr(int *p_arr, int elem, int val)
{
    for (int i = 0 ; i < elem ; i++)
    {
        *(p_arr + i) += val;
    }
}

void output_arr_to_console(int *p_arr, int elem)
{
  cout << "array = { ";
  for (int i = 0 ; i < elem ; i++)
  {
      cout << *(p_arr + i) << " ";
  }
  cout << "}" << endl;
}

int main(void) 
{
  int arr[3]           = {10, 20, 30};
  int elements_of_arr  = sizeof(arr) / sizeof(arr[0]);
  int val_to_add       = 0;

  cout << "Before: " << endl;
  output_arr_to_console(arr, elements_of_arr);
  cout << endl;

  cout << "Typing in a value to add to the array: ";
  cin >> val_to_add;

  map_val_to_arr(arr, elements_of_arr, val_to_add);

  cout << endl;
  cout << "After: " << endl;
  output_arr_to_console(arr, elements_of_arr);

  return 0;
}

Before: array = { 10 20 30 }

Typing in a value to add to the array: 1000

After: array = { 1010 1020 1030 }

fatpao commented 3 months ago
#include <iostream>
using namespace std;

void map(int a[], int num, int value) {
  for(int i=0; i<num; i++){
    a[i] = a[i] + value;
  }
}

int main() {
  int num = 3;
  int n[num];
  int value;

  n[0] = 10;
  n[1] = 20;
  n[2] = 30;

  for (int i = 0; i < num; i++) {
    cout << "原本的n[" << i << "]=" << n[i] << endl;
  }
  cout << "----------" << endl;
  cout << "請輸入要增加的數值:" ;
  cin >> value;
  cout << "----------" << endl;

  map(n, num, value);

  for (int i = 0; i < num; i++) {
    cout << "後來的n[" << i << "]=" << n[i] << endl;
  }

}

image

charlywang11 commented 3 months ago

請依下列圖片建立陣列和function,最後輸出陣列內的值

#include <iostream>
using namespace std;

void array(int a[], int n) {
  srandom(time(NULL));
  int max = 100;
  int min = 1;
  for (int i = 0; i < n; i++) {
    a[i] = random() % (max - min + 1) + min;
  }
}

void map(int a[],int num, int value) {
  for(int i=0; i<num; i++){
    a[i] = a[i] + value;
  }
}

int main() {
  int num;
  cout << "請輸入想建立的陣列個數:";
  cin >> num;
  int a[num];
  int value;

  array(a, num);
  cout << num << "個數的隨機陣列:";
  for (int i = 0; i < num; i++) {
    cout << a[i] << ", ";
  }
  cout << endl << endl;

  cout << "請輸入想要加的整數:";
  cin >> value;

  map(a, num, value);
  cout << "+" << value << "後的陣列:";
  for (int i = 0; i < num; i++){
    cout << a[i] << ", ";
  }
  cout << endl;

  return 0;
}

image

createcube commented 3 months ago
#include <iostream>
using namespace std;

void map(int a[], int num, int value){
  for(int i = 0; i < num; i++){
    a[i] = a[i] + value;
  }
}

int main() {
  int n[] = {10, 20, 30};
  int value;
  for(int i = 0; i < 3; i++){
    cout << "原來的矩陣n["<< i << "]="  << n[i] << endl;
  }

  cout << "請輸入要加上的數字";
  cin >> value;
  map(n, 3, value);
  for(int i = 0; i < 3; i++){
    cout <<  "矩陣變為n[" << i << "]=" << n[i] << endl;
  }

  return 0;
}
jordon1321 commented 2 months ago
#include <iostream>
using namespace std;

void map(int a[], int num, int value){
  for(int i=0; i<num; i++){
    a[i] = a[i] + value;
  } 
}

int main(){
  int num = 3;
  int value;
  int a[num];

  a[0] = 10;
  a[1] = 20;
  a[2] = 30;

  for(int i=0; i<3; i++){
    cout << "a[" << i <<"] = " << a[i] << endl;
  }
  cout << "====================" << endl;
  cout << "value: ";
  cin >> value;
  cout << "====================" << endl;

  map(a, num, value);
  for(int i=0; i<3; i++){
    cout << "a[" << i <<"] = " << a[i] << endl;
  }
   return 0;
}