This is a formula I'm supposed to use to create a function that will populate a an array with random doubles
lower bound + ( random integer / (maximum possible random number / (upper bound - lower bound)))
the lower bound being 0 and upper being 100 and max being RAND_MAX
the function being a double ___() function that takes no arguments
for the life of me I cannot figure out how to make this formula work as a function or how to populate the array with its results (it just gives me the error "cannot convert a double ()() to a double")
I have a "kind of" working version of the program here
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <iomanip>
using namespace std;
int buildArray(double[]);
void printArray (double[],int, int);
double randDouble();
const int LOWER=0;
const int UPPER=100;
const int ARRAY_SIZE=100;
int main(){
int numberElements = 0;
int line;
double MainArray [ARRAY_SIZE];
numberElements=buildArray(MainArray);
cout<< "how many values per line? ";
cin>>line;
cout<<"unsorted Values"<<endl<<endl;
printArray(MainArray, numberElements, line);
cout<<endl<<"sorted values"<<endl<<endl;
sort(MainArray, MainArray + numberElements);
cout<<endl<<endl;
printArray(MainArray, numberElements, line);
return 0;
//the function would normally go right here
}
int buildArray(double MainArray[]){
int i=0;
int arrayTotal;
srand(time(NULL));
while(arrayTotal <20){
arrayTotal=rand()%100;
}
while(i<arrayTotal){
//and I would insert it below here instead of this jerry-rigged thing
MainArray[i]=rand()%100/((double)RAND_MAX/10000);
i++;
}
return arrayTotal;
}
void printArray(double MainArray[],int numOfValues,int line){
int lineBreak = 0;
for(int i = 0; i < numOfValues; i++){
if (lineBreak==line){
cout<<endl;
lineBreak=0;
}
lineBreak++;
cout<<setprecision(5)<<MainArray[i]<<" ";
}
}
sorry my code is sloppy and inefficient. Also sorry the question is probably stupid with an obvious solution. I'm not actually taking any classes right now, I'm just trying to teach myself C++ so I have no one else to ask.
Aucun commentaire:
Enregistrer un commentaire