I'm trying to read an unknown number of elements into the array, when my size reaches the current capacity, I call a function to double the size and copy the contents of the old array into a new array. I got my '&' but it seems it's still passing the array by value.
#include <iostream> #include <fstream> using namespace std; void resize(int*&, int&); int main() { ifstream in("numbers.input"); int cap = 10; double avg; int total = 0; int size = 0; int *arr = new int [cap]; int temp; for(int i =0; i <cap; i++){ in >> temp; if(size >= cap) { resize(arr,cap); } arr[i]=temp; total += arr[i]; size++; } avg = (double) total/cap; cout << cap <<endl; cout << size <<endl; cout << total <<endl; cout << avg; return 0; } void resize(int *&arr,int &cap) { cap*=2; int* newArr = new int[cap]; for(int i = 0; i < cap; i++){ newArr[i] = arr[i]; } delete [] arr; arr = newArr; }
resize. Can you spot it?