Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

c++ - Trying to save different values in 2 arrays using one FOR loop but the first array is been attributed the value of the second input

I am testing the ability to use functions to input values in 2 arrays and also to perform addition of the values. I wish to input the different values in the 2 arrays using a function inputArray(A1, A2, size) using a For loop. I also used a function sumArray(A1, A2, size) to perform the addition of the 2 values in the arrays.

But the issue is with the input function as when i am running the program to input different values in the 2 arrays, the first array is also been attributed the value of the second array.

void inputArray(int Array1[], int Array2[], int n)
{
    for(int i=0; i<n; i++)
    {
        cout<<"Array 1:"<<endl;

        cin>>Array1[i];

        cout<<" Array 2: "<<endl;

        cin>>Array2[i];
        cout<<endl;

        //test
        cout<<"
 A1[0]= "<<Array1[0]<<endl;
        cout<<"
 A2[0]="<<Array2[0]<<endl;
    }
}

I tried to use 2 different functions to input the values and this worked.But then again when i used a FOR loop for the Addition function sum = sumArray(A1, A2, size), both arrays A1 and A2 were being attributed the value of the second array.

int sumArray(int Array1[], int Array2[], int n)
{
    int sum= 0;
    for(int i=0; i<n; i++)
    {
        sum+= ( Array1[i] + Array2[i] );
        cout<<"The total sum so far for values till position "<<i<<" is "<<sum<<endl;  
    }

    //to monitor if the program is performing well with the addition

    //test
    cout<<"
 A1[0]= "<<Array1[0]<<endl;
    cout<<"
 A2[1]="<<Array2[2]<<endl;

    return sum;
}
#include<iostream>

using namespace std;


void inputArray(int Array1[], int Array2[], int n)
{
    for(int i=0; i<n; i++)
    {
        cout<<"Array 1:"<<endl;

        cin>>Array1[i];

        cout<<" Array 2: "<<endl;

        cin>>Array2[i];
        cout<<endl;

        //test
        cout<<"
 A1[0]= "<<Array1[0]<<endl;
        cout<<"
 A2[0]="<<Array2[0]<<endl;
    }
}

//To sum all the values in the 2 arrays//not asked in the question

int sumArray(int Array1[], int Array2[], int n)

{
    int sum= 0;
    for(int i=0; i<n; i++)
    {
        sum+= ( Array1[i] + Array2[i] );
        cout<<"The total sum so far for values till position "<<i<<" is "<<sum<<endl;  
    }
    //to monitor if the program is performing well with the addition


    //test
    cout<<"
 A1[0]= "<<Array1[0]<<endl;
    cout<<"
 A2[1]="<<Array2[2]<<endl;

    return sum;
}
int main()
{
    int size;

    int A1[size];
    int A2[size];

    size= 3;

    cout<<"Input "<<size<<" values in the first  and second array: "<<endl;

    inputArray( A1, A2, size);  //do not write square bracket when calling a function

    int sum = sumArray(A1, A2, size);
    cout<<"The sum of the total values is : "<<sum<<endl;


    //test
    cout<<"
 A1[0]= "<<A1[0]<<endl;
    cout<<"
 A2[1]="<<A2[2]<<endl;
    return 0;
}
question from:https://stackoverflow.com/questions/65560479/trying-to-save-different-values-in-2-arrays-using-one-for-loop-but-the-first-arr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here you use a non-initialised variable for the size of the two arrays.

int size;
int A1[size];
int A2[size];

This is in C++ wrong in more than one way.
You need to define C-like arrays with a constant size.
Safer would be to use C++ containers, e.g. std::vector for anything without predictable size.

Even in C, where VLAs are possible, creating them like you did and LATER reading in a new value for size will not change anything, especially not the size of the arrays.

Also, but that is already inside undefined behaviour and purely speculative,
if the compiler understands that as A1 and A2of size 0, then the start of both arrays is the same and writing to one writes also to the other - AND be totally forbidden because it will access beyond any arrays size.

To demonstrate that, try

int A1[5];
int A2[5];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...