2

The result I want to get is Multiply the index value with other index value. e.g in the code below: i want to update arr[0] value by arr[0]*arr[1], arr[9] value by arr[9]*arr[9-8] and for remaining indexes it'll be arr[i-1] * arr[i] * arr[i+1]. It is working fine on first index but at other indexes I am getting unexpected result.

#include<iostream>
using namespace std;

int main(){
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, j, k;
    
    for(j = 0; j < 10; j++){
        if(j == 0){
            arr[j] = arr[j] * arr[j + 1];
        }else if(j == 9){
            arr[j] = arr[j] * arr[j - 1];
        }else{
            arr[j] = arr[j - 1] * arr[j] * arr[j + 1];
        }
    }
    
    for(k = 0; k < 10; k++){
        cout<<arr[k]<<" ";
    }
}

Output: 2 12 144 2880 86400 3628800 203212800 1746419712 -1736015872 -180289536

Expected Result: 2 6 24 60 120 210 336 504 720 90

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
Shakir
  • 33
  • 4
  • 1
    The numbers get bigger and bigger and overflow at one point. If you print the whole array in each iteration you will see it. – Lukas-T Feb 19 '21 at 08:14
  • 1
    What would be your *expected* result – florestan Feb 19 '21 at 08:15
  • Please describe why the output is unexpected. 2 = 1*2, 12 = 2 * 2 * 3, 144 = 12 * 3 * 4, 2880 = 12 * 144 * 5, .... That's what I expect. – Thomas Sablik Feb 19 '21 at 08:19
  • Expected Result: 2 6 24 60 120 210 336 504 720 90 – Shakir Feb 19 '21 at 08:20
  • 1
    You're changing the values in the array and using the new values to calculate the next values. You need a copy of your array. – Thomas Sablik Feb 19 '21 at 08:21
  • How can i do that? Kindly explain what is happening now? And what to do for expected result? – Shakir Feb 19 '21 at 08:23
  • In the first step you change the first element from 1 to 2. In the next step you use this new value. Your array at the beginning is {1, 2, 3, ...}. After the first step the array is {2, 2, 3, ...} and 2 * 2 * 3 = 12 is the second result. You can solve the problem by storing the results in a separate array. – Thomas Sablik Feb 19 '21 at 08:24

1 Answers1

5

You need to store the results in a separate array or else you overwrite the values in the same array you use for future calculations.

In your case, when j == 0 you do:

arr[0] = arr[0] * arr[1]; // 1 * 2 = 2

So arr[0] is now 2. You then do

arr[1] = arr[0] * arr[1] * arr[2]; // 2 * 2 * 3 = 12, not 1 * 2 * 3 = 6

...and so on. If you instead store the result in a separate array, that problem goes away. Example:

#include <iostream>
// using namespace std; // Don't use this.

int main(){
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int res[10] = {0};
    
    for(int j = 0; j < 10; j++) {
        if (j == 0)
            res[j] = arr[j] * arr[j + 1];
        else if (j == 9)
            res[j] = arr[j] * arr[j - 1];
        else
            res[j] = arr[j - 1] * arr[j] * arr[j + 1];
    }
    
    for(int k = 0; k < 10; k++)
        std::cout << res[k] << ' ';
}

Output:

2 6 24 60 120 210 336 504 720 90 
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Let me know why should we not use using (namespace std). It occurs any problem? – Shakir Feb 19 '21 at 08:39
  • 2
    @Shakir Sure, here's a collection of reasons: [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Ted Lyngmo Feb 19 '21 at 08:40
  • @Shakir Did you understand the reason for what is happening in your program? Please ask if I need to clarify it further. – Ted Lyngmo Feb 19 '21 at 08:42
  • I just got it. Thank you! – Shakir Feb 19 '21 at 08:43
  • Yes. I've got my answer that why we should not use **using namespace std**. But I don't think it'll create problem in such a small program. What do you say? – Shakir Feb 19 '21 at 08:47
  • @Shakir It only creates problems for people reading the program. If I see `std::cout` I rarely have to worry. If I see `cout` it could be just about any `cout`. Also, in this small program the benefit one gets from `using namespace std;` is completely lost, since you only do one `std::cout` - so you actually typed in more characters to avoid having to type `std::` later. It's best just to forget about `using namespace std;` - at least for now. – Ted Lyngmo Feb 19 '21 at 08:55
  • 1
    @Shakir I've already seen about 20 questions on Stack Overflow describing different problems caused by `using namespace std;`. If you don't know all the names that are imported by that namespace you shouldn't use it. After `using namespace std;` you shouldn't use names like `begin`, `end`, `data`, `swap`, `array`, `vector`, `list`, ... for your functions or variables because they are already in use. – Thomas Sablik Feb 19 '21 at 09:24