【问题标题】:random doubles for an array数组的随机双打
【发布时间】:2015-11-06 07:54:49
【问题描述】:

这是我应该用来创建一个函数的公式,该函数将用随机双精度数填充一个数组

lower bound + ( random integer / (maximum possible random number / (upper bound - lower bound)))

下限为 0,上限为 100,最大值为 RAND_MAX

该函数是一个不带参数的 double ___() 函数

在我的一生中,我无法弄清楚如何让这个公式作为一个函数工作,或者如何用它的结果填充数组

它只是给了我错误:

无法将双精度 ()() 转换为双精度

我在这里有一个“某种”工作版本的程序

#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]<<" "; 

}

}

抱歉,我的代码草率且效率低下。也很抱歉,这个问题可能很愚蠢,有一个明显的解决方案。我现在实际上并没有上任何课程,我只是想自学 C++,所以我没有其他人可以问。

【问题讨论】:

  • 只调用一次srand。你能写一个函数来做 1 个浮点数并以此为基础吗?

标签: c++ arrays random


【解决方案1】:

我知道从 C/C++ 开始很难 :)

尝试用以下内容替换您的buildArray

int buildArray(double MainArray[]){
    int i=0;
    srand(time(NULL));
    while(i<ARRAY_SIZE){
        MainArray[i]= LOWER + double(rand()) * (UPPER - LOWER) / RAND_MAX;  
        i++;
    }
    return ARRAY_SIZE;
}

一切都像魔术一样运作......

但是请不要对 C++ 有错误的看法。 “现代”代码应如下所示:

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <iomanip>

void buildArray(std::vector<double> & myVector);
void printArray(std::vector<double> & myVector, int line);
const int LOWER=0;
const int UPPER=100;

int main()
{
    const size_t arraySize = 100;

    std::vector<double> myVector;
    myVector.resize(arraySize);

    srand(time(nullptr));  // Call this only once
    buildArray(myVector);

    std::cout << "how many values per line? ";
    int line = 0;
    std::cin >> line;

    std::cout << "unsorted Values" << std::endl << std::endl;
    printArray(myVector, line);

    std::cout << std::endl<<"sorted values" << std::endl << std::endl;
    std::sort(myVector.begin(), myVector.end());

    std::cout << std::endl << std::endl;
    printArray(myVector, line);

    return 0;
}


void buildArray(std::vector<double> & myVector)
{
    for (size_t i = 0; i < myVector.size(); ++i) {
        myVector[i] = LOWER + double(rand()) * (UPPER - LOWER) / RAND_MAX;
    }
}

void printArray(std::vector<double> & myVector, int line)
{
    size_t lineBreak = 0;
    for (size_t i = 0; i < myVector.size(); i++){
        if (lineBreak == line){
            std::cout << std::endl;
            lineBreak=0;
        }
        lineBreak++;
        std::cout << std::setprecision(5) << myVector[i] << " "; 
    }
}

了解如何使用 std::vector 代替 C 样式数组...:)

【讨论】:

  • 谢谢,我搞定了。您的回答非常有帮助,而且比我预期的更彻底。我会记住你所说的关于使用向量的内容。
猜你喜欢
  • 1970-01-01
  • 2016-08-27
  • 2015-10-29
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多