0

I am trying to take two input and save it to two different arrays but the first array is input is getting stored perfectly but the second input value is arbitrary I don't get why is it happing

#include<iostream>
using namespace std;
int main()
{
    int n;
    int points = 20, fouls = 10;
    cin>>n;
    int arr[n], fls[n], pt[n], fl[n];
    for(int i = 0; i < n; i++){
        cin >> arr[i] >>fls[i];
    }
 
    for(int i = 0; i< n;i++){
        pt[i]= arr[i] * points;
        fl[i] = fls[i] * fouls;

        // arr[i] = arr[i] - fls[i];
        // cout<<pt[i]<<" ";
        cout<<fl[i]<<" ";

    }

    // int max = arr[0];
    // for(int i =1; i <n;i++){
    //     if(arr[i]>max){
    //         max = arr[i];
    //     }
    // }
    // cout<<max;
    return 0;
}

Output Output of the code see how I am multiple the second array with 10 but the answer is wrong

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Raj Gupta
  • 3
  • 4

1 Answers1

0

What you write is what you get.

In this for loop

for(int i = 0; i < n; i++){
    cin >> arr[i] >>fls[i];
}

you are entering sequentially the following values

40 30 50 2 4 20

So

arr[0] = 40 fls[0] = 30  // first iteration of the loop
arr[1] = 50 fls[1] = 2   // second iteration of the loop
arr[2] = 4  fls[2] = 20  // third iteration of the loop

Then in this for loop

for(int i = 0; i< n;i++){
    pt[i]= arr[i] * points;
    fl[i] = fls[i] * fouls;

    // arr[i] = arr[i] - fls[i];
    // cout<<pt[i]<<" ";
    cout<<fl[i]<<" ";

}

you will get

fl[0] = 300, fl[1] = 20 fl[2] = 200

This output is shown on the screenshot.

Pay attention to that variable length arrays is not a standard C++ feature. You should use std::vector<int>.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335