0

I still have the same problem of wierd number in my arrays, but this time in a different function:

#include <iostream>

using namespace std;

//First Array
int n[20];
int i, a;
//Second Array
int n2[20];
int i2, a2;

void arraySelection();
void printArrays();
void unionArray();
void intersectionArray();

int main(){ 

    arraySelection();

    printArrays();

    intersectionArray();

    unionArray();

    return 0;
}

void arraySelection(){

    cout << "First array size: ";
    cin >> a;

    cout << "Array elements: " << endl;;

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

    cout << "\nSecond array size: ";
    cin >> a2;

    cout << "Array elements: " << endl;;

    for (i2 = 0; i2 < a2; i2++){
        cin >> n2[i2];
    }

}

void unionArray(){

    const int riemp = 40;       
    int unionNums[riemp];
    int j, x, y, z;
    bool t = false;

    for (j = 0; j <= riemp; j++){
        unionNums[j] = n[j];
        cout << unionNums[j] << " ";
    }

}

Basically I am trying to copy the numbers of my first array n[20] into my unionNums[40] array. It actually does that but it also outputs a series of 0s and other large wierd numbers. 2 Days in the go and still no idea. (PS If i try to gife fixed numbers to the array, so getting rid of the user input, it goes without any problem whatsoever) I also did not bother copyng my intersectionArray and printArray functions since they do things that I don't need anymore at this point

Simon
  • 107
  • 9
  • 2
    Why is there in this loop for (i = 0; i < a+1; i++){ used a + 1 ? – Vlad from Moscow Nov 08 '19 at 09:56
  • Apart from that `i < a+1` is wrong in given case, with *integrals* you can instead write `i <= a`, which which results in exactly the same *unless* `a + 1` overflows. – Aconcagua Nov 08 '19 at 10:03
  • Make it a good habit right from the start *always* to check user input! What, if user entered 77 for `a`??? What, if user didn't enter a valid value at all (e. g. 'xyz' – unless base was 26)? The latter you catch by checking stream state, the former by appropriate range check: `if(std::cin && a < sizeof(n)/sizeof(*n)) { /* valid */ }`; actually you'd need to check for negative values, too, but you could avoid if using `unsigned int` as input type. If you had used `std::array` instead of raw array, you wouldn't need that divide-size-by-size-of-element trick either, there's `std::array::size()`. – Aconcagua Nov 08 '19 at 10:05
  • Alternatively you could use `std::vector` to allow arbitrary number of values: `std::vector v; v.reserve(a); for(...) { int value; std::cin >> value; v.push_back(value); }`. That would then even allow using a range based for loop for outputting: `for(auto n : v) { std::cout << n; }`, which would at the same time avoid the error you had already (bad indexing: `i < a + 1`). – Aconcagua Nov 08 '19 at 10:13
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Nov 08 '19 at 10:14

1 Answers1

4

Here you read in elements up to n[a-1]:

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

So all elements up to n[a-1] are initialized. Then you print all elements up to n[a], however.

for (i = 0; i < a+1; i++){ // note that you have "a+1" here, not just "a"
    cout << n[i] << " - ";
}

n[a] wasn't initialized. When you read that out, it's undefined behavior. That often manifests as getting a garbage value, which is what you're seeing in your output. The same thing happens in your second array/loop where you write into the array for i2 < a2 but get output for i2 < a2+1.

Blaze
  • 16,736
  • 2
  • 25
  • 44