C++ arrays and array parameter - BUG! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C++ arrays and array parameter - BUG!

This code has a bug! (Bugs on lines 4 and 21 according to online compiler C++ shell). Purpose of this program: I am trying to cin inside an array and pass in that array to another function to do some multiplication and then print out the values. . Please take a look at this code. #include <iostream> using namespace std; void tripler (int n[]){ for (int i = 0; i < 3; i++){ n[i] = n[i] * i; cout << n[i]; } } int main (){ int a[3]; for (int i = 0; i < 3; i++){ cin >> a[i]; int size = 0; size += i; tripler (a[size]); } return 0; } c++ shell link: cpp.sh/96ohq7

16th Jan 2017, 8:14 AM
Kourosh Azizi
Kourosh Azizi - avatar
2 Answers
+ 1
#include <iostream> using namespace std; void tripler (int n[]){ for (int j = 1; j <= 3; j++){ for (int i = 0; i < 3; i++){ n[i] *= j; cout << n[i]<<","; } cout<<endl; } } int main (){ int a[3]; for (int i = 0; i < 3; i++){ cin >> a[i]; } tripler (a); return 0; }
16th Jan 2017, 8:23 AM
ASNM
ASNM - avatar
+ 1
Nice one my friend! :)
16th Jan 2017, 8:32 AM
Kourosh Azizi
Kourosh Azizi - avatar