Hello, thanks ahead of time for those of you trying to help me. I have a C++ assignment that I have basically done, but there are some tweaks I can't figure out. I have to write a program that generates a series of random numbers and stores them into a C-style array. My teacher made us use a 'bool' function for this, but I've figured that part out. I'm trying to figure out how to use a printData function to [1] print the data to the screen, and [2]print the data to a file called "randomArray.txt".
Here's my function so far:
void printValues(ostream& fout)
{
if(!randomArray(variance, mean, numPoints, x))
{
fout<<randomArray(variance, mean, numPoints, x)<<endl;
fout<<"\n"<<endl;
}
}
NOTE: It also says that my "ostream& fout" object was not declared or can't be used this way etc.
Copyright © 2024 1QUIZZ.COM - All rights reserved.
Answers & Comments
Verified answer
Ok. It's very hard to help you without the full code or the interfaces (at least), because (1) we don't know what randomArray() does (does it return a pointer), (2) I see no reference to an array in your function (is it global?). Here's something that may or may not help you.
////////////////
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int* getrandomlist(int);
void print(ostream&, int*, int);
int main()
{
int size = 5;
int* list = getrandomlist(size);
print(cout, list, size); //console
fstream fout("data.dat");
print(fout, list, size);//file
fout.close();
free(list);
}
int* getrandomlist(int size)
{
int* buf = (int*)malloc(sizeof(int)*size);
srand(time(NULL));
for(int i = 0; i < size; i++)
{
buf[i] = rand() % 10 + 1; //between 1-10
}
return buf;
}
void print(ostream& out, int* list, int size)
{
for(int i = 0; i < size; i++)
{
out << list[i] << endl;
}
}
/////////////