C++ 生成随机数组 无输出

屏幕上没有输出数组的内容。


#include 
#include 
#include 
using namespace std;

int main() {
    //决定数组长度
    unsigned short length;
    srand((unsigned)time(NULL));
    length = (unsigned)rand() % 128;
    cout << "数组长度:" << length << "\n" << endl;
    //创建数组
    int *array = (int *)malloc(sizeof(int) * length);
    //发牌
    {
        int *operating;
        operating = array;
        int i;
        cout << "数组:" << endl;
        for (i = 0; i++; i < length) {
            srand((unsigned)time(NULL));
            *operating = (unsigned)rand();
            cout << "\b" << *operating << "\u0020" << endl;
            operating++;
        }
    }
    return 0;
}

(1)for循环的第二个表达式和第三个表达式写反了。i<lengh在中间作为循环条件,i++在最后。

for (i = 0; i<length; i++)

(2)for循环中没必要在执行一次srand(),如果不改变随机数种子,一个代码中只需要调用一次srand()就可以了

代码:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    //决定数组长度
    unsigned short length;
    srand((unsigned)time(NULL));
    length = (unsigned)rand() % 128;
    cout << "数组长度:" << length << "\n" << endl;
    //创建数组
    int* array = (int*)malloc(sizeof(int) * length);
    //发牌
    {
        int* operating;
        operating = array;
        int i;
        cout << "数组:" << endl;
        for (i = 0; i<length; i++) { //修改1 
            //srand((unsigned)time(NULL));  修改2,这一句不需要,一个代码中,随机数种子设置一次就可以了
            *operating = (unsigned)rand();
            cout << "\b" << *operating << "\u0020" << endl;
            operating++;
        }
    }
    return 0;
}

循环条件和递增写反了吧


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
    //决定数组长度
    unsigned short length;
    srand((unsigned)time(NULL));
    length = (unsigned)rand() % 128;
    cout << "数组长度:" << length << "\n" << endl;
    //创建数组
    int *array = (int *)malloc(sizeof(int) * length);
    //发牌
    {
        int *operating;
        operating = array;
        int i;
        cout << "数组:" << endl;
        for (i = 0; i < length; i++) {
            srand((unsigned)time(NULL));
            *operating = (unsigned)rand();
            cout << "\b" << *operating << "\u0020" << endl;
            operating++;
        }
    }
    return 0;
}
  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/259860
  • 你也可以参考下这篇文章:C++学习之路—— C++异常处理机制
  • 除此之外, 这篇博客: C++ 内置 二分查找用法中的 某个元素的位置。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
    1. 函数模板:
      lower_bound(arr[],arr[]+size ,  indx)
      
    2. 参数说明:
      arr[]: 数组首地址
      size:数组元素个数
      indx:需要查找的值
    3. 函数功能: 函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置(注意是地址)。如果所有元素都小于val,则返回last的位置
    4. 举例如下:
        一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标,(注意因为返回值是一个指针,所以减去数组的指针就是int变量了 )则
    • pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。
    • pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。
    • pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。
    1. 注意:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!

    返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632