0

I can't detect the error in the following code:

int add(int x[], int size, int y[]=0) {
int sum = 0;
for (int i = 0; i < size; i++) {
    sum += x[i];
}
for (int i = 0; i < size; i++) {
    sum += y[i];
}
return sum;
}

int main() {

   int a[] = { 1,2,3,4,5 };
   int b[] = { 6,7,8,9,10 };
   int c = add(a, 5);
   int d = add(a, 5, b);
   cout << c << endl;
   cout << d << endl;
}
Francisco
  • 10,918
  • 6
  • 34
  • 45
  • 1
    Just because the command prompt closes doesn't mean anything is broken. – Cory Kramer Oct 25 '17 at 16:41
  • Why would you expect it to do anything else? After `cout << d << endl;`, your code is finished, and the application exits. It's working exactly as you've written it, and nothing is broken at all. – Ken White Oct 25 '17 at 16:41
  • You really shouldn't use so many exclamation marks. We would help you anyway, you know. (Or we wouldn't, but the exclamation marks won't change that) – Neo Oct 25 '17 at 16:43

2 Answers2

0

It's completely normal, as far as I understand.
The program is executed, and once finished it's closed.
If you want to see the results execute the program from an existing CMD.

C:/location> program.exe
Neo
  • 3,534
  • 2
  • 20
  • 32
0

Did you try running without debugging? Try hitting ctrl + f5 if you're in visual studio and the command should stay opened when your code finishes.