-1

I am working on a project which requires certain actions be in their own functions. Right now, I must take the random values generated, and output them FROM an array. I have the values stored in numarray , but I don't know how I can call that array in the function to output the values. I have a function set up which seems like it will take the array and output it, but I don't know how to call the function without an argument to pass through it in main. Is this a possible way to output the array, or is there a completely different way this should be done.

PS: I know namespace isn't good, but it's what I have to do to pass the class.

#include <iostream> 
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <cstddef>


using namespace std;
ofstream randomData;
ifstream inputrandomData;
void randomgenerator();
void read();
void printArray(int *numarray);


void randomgenerator() {
    srand(time(0));
    randomData.open("randomData.txt");
    for (int counter = 0; counter < 100; counter++) {
        randomData << rand() % 100+1 << endl;
    }
    randomData.close();
    
}


void read() {
    inputrandomData.open("randomData.txt");
    int numarray[100] = {};
    for (int i = 0; i < 100; i++) {
        inputrandomData >> numarray[i];
    
    }
    inputrandomData.close();

}



void printArray(int *numarray) {
    
    for (int index = 0; index < 100; index++) {
        cout << numarray[index];
    }

}


int main() {

    randomgenerator();
    read();
    printArray();

    return 0;
}

The randomgenerator function is what makes the values and stores them in a file. The read function takes those values and stores them in an array. printarray is the function where I want to output the array values, and is giving me the problem.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 3
    use a `std::vector` and return it from `read()` – 463035818_is_not_an_ai Feb 03 '20 at 17:37
  • 3
    Forget C-style arrays exist. Use `std::array` or `std::vector` instead. Forget `srand` and `rand` exist. We have better options in modern C++ in the [random](https://en.cppreference.com/w/cpp/header/random) header (see also https://en.cppreference.com/w/cpp/numeric/random and https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful ). – Jesper Juhl Feb 03 '20 at 17:39
  • @jes: There's nothing fundamentally wrong with using `rand`. It's a fast pseudo-random number generator. If you don't need the particular properties of C++11's `` library (like uniform distribution), there's no reason to forget about a valuable tool. – IInspectable Feb 03 '20 at 19:49
  • @IInspectable while I agree that it is a tool that *may* be used, I'd rather point beginners who probably don't know how to apply it, at the more modern options. – Jesper Juhl Feb 03 '20 at 19:52

2 Answers2

4

You need to dynamically allocate and return numarray from read. Or even better create numarray like you did here on the stack, but do it in main and pass it as an argument to read, then pass as an argument to printArray.

As it is numarray will go out of scope when read completes, since your allocating it on the stack.

Dom G
  • 56
  • 2
  • 2
    The comments on using std::vector or std::array are spot-on. You should only use raw arrays if there is a reason to (ie. your professor requires it). – Dom G Feb 03 '20 at 17:43
  • 1
    I would add that, as others have pointed out, you're better off using std::vector (or std::array). Declare it in main, and then pass a reference in to the read and print functions (void read(std::vector& v); void printArray(const std::vector& v);). – user888379 Feb 03 '20 at 17:45
  • This worked for me. I just had it stuck in my head that I needed to create the array in the function, and I didn't. – killerkody gaming Feb 04 '20 at 04:04
  • @killerkody gaming : your approach can be improved with Class as you are using C++. Problem can be solved in multiple ways but the correct approach is necessary. – Build Succeeded Feb 04 '20 at 09:07
0

Solution (as per your code): Declare the local array variable in main. Passby pointer to function along with array size as it required to process array inside a passed function. This kind of design is not preferable.

You can design the class as there are some data members involved, on which you can call the method of that class.

You can prefer the std::vector as data member to hold data with a reserve the size as per yours requirement. Because the reserve method of std::vector avoid reallocation as vector size grow. Here, you have size 100. So you can reserve the 100 items of std::vector.

Build Succeeded
  • 1,153
  • 1
  • 10
  • 24