In: Computer Science
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
void fillList(int *ary,ifstream &in,int size) {
int lowLim, highLim;
// read lower limit and upper limit
in >> lowLim;
in >> highLim;
// store random number
for (int i = 0;i < size;i++) {
ary[i] = rand() % (highLim -
lowLim) + lowLim;
}
}
int main() {
// for filename
char filename[20];
// array
int *ary=NULL;
// size of array
int size;
// for reading file
ifstream in;
// read input
cout << "Enter the file name : ";
fgets(filename,30,stdin);
// new character to null character
filename[strlen(filename) - 1] = '\0';
cout << "Enter the array size : ";
cin >> size;
// array create
ary = new int[size];
// read file
in.open(filename);
// if file not found
if (in.fail() == true) {
cout << "File not
found.";
}
else {
// fill array
fillList(ary,in,size);
}
for (int i = 0;i < size;i++) {
cout << ary[i] << "
";
}
in.close();
cout << endl;
system("pause");
return 0;
}