| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 745 人关注过本帖
标题:程序中有这样random和randomize头文件需要包含哪些?请指教
收藏  订阅  推荐  打印
lindayanglong
Rank: 2
等级:注册会员
帖子:83
积分:1036
注册:2008-3-9
程序中有这样random和randomize头文件需要包含哪些?请指教

我的程序中有这样random和randomize
头文件包含有:
#include<iomanip>
#include<cstdlib>
#include<iostream>
using namespace std;
但是总是出错:
'random' : undeclared identifier
'randomize' : undeclared identifier
请高手指点。我把完整程序也贴在下面:
#include<iomanip>
#include<cstdlib>
#include<iostream>
using namespace std;

template<class T>
int linsearch(T *v,int N, T key)
{
    for(int i=0;i<N;i++)
    {if (v[i]==key)return i;}
    return -1;
}

inline int RandI(int N)
{
    return random(N)+1;
}

int main()
{
    int index;
    int key=32;
    randomize();
    const int size=20;
    int data[size];

    for(int i=0;i<size;i++)
        data[i]=RandI(85);
    cout<<"\ndata's value are :"
        <<endl;
    for( i=0;i<size;i++)
    {
        cout<<setw(10)<<data[i];
        if(i%5==4)cout<<endl;
    }
    index=linsearch(data,size,key);
    if(index>-1)
        cout<<key<<"is the "<<index+1
        <<"th palce"<<endl;
    else
        cout<<"cannot find "<<key<<endl;
    return 0;
}
2008-7-15 10:08
zjl138
Rank: 4
等级:高级会员
帖子:764
积分:9144
威望:1
注册:2007-11-12

#include<ctime>

i like linux...
2008-7-15 20:50
lindayanglong
Rank: 2
等级:注册会员
帖子:83
积分:1036
注册:2008-3-9
加上#include<ctime>还是一样啊

加上#include<ctime>还是上面的错误,请帮帮忙
2008-7-16 10:07
zjl138
Rank: 4
等级:高级会员
帖子:764
积分:9144
威望:1
注册:2007-11-12

randomize()不是随机数生成函数,而是初始化随机数生成器的函数,而且,它不是一个C语言标准库函数.改用srand(unsigned(time(NULL)));我已在程序标明.
我把你改了一下,你看是不是你想要的结果:
引用:
#include<iomanip>
#include<cstdlib>
#include<iostream>
#include<ctime>
using namespace std;

template<class T>
int linsearch(T *v,int N, T key)
{
    for(int i=0;i<N;i++)
    {if (v[i]==key)return i;}
    return -1;
}

inline int RandI(int N)
{
    return rand()%N+1;      //你写成random(N)+1了.
}

int main()
{
    int index;
    int key=32;
    srand(unsigned(time(NULL)));         //这里改了.
    const int size=20;
    int data[size];

    for(int i=0;i<size;i++)
        data[i]=RandI(85);
    cout<<"\ndata's value are :"<<endl;
    for(int i=0;i<size;i++)
    {
        cout<<setw(10)<<data[i];
        if(i%5==4) cout<<endl;
    }
    index=linsearch(data,size,key);
    if (index>-1)
        cout<<key<<"is the "<<index+1<<"th palce"<<endl;
    else
        cout<<"cannot find "<<key<<endl;
    return 0;
}

i like linux...
2008-7-16 12:04
lindayanglong
Rank: 2
等级:注册会员
帖子:83
积分:1036
注册:2008-3-9

十分感谢楼上的
2008-7-16 13:28
liuruima
Rank: 1
等级:新手上路
帖子:1
积分:110
注册:2008-10-2

张耀仁的书在Borland C++ Complier上用的,用VC有些意外的麻烦
2008-10-2 22:18
共有 744 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.048917 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved