C++ 我可以计算这个数组中的元素数吗?
所以基本上我想做的是,不要让int Randnum=rand%6;,我想要一个随数组变化的变量。类似于int Randnum rand%totalwords;。我尝试使用sizeofwords,但只有在我知道每个单词占用多少字节的情况下,它才会起作用。你们有没有办法让我把每个单词都算为一个项目,这样当数组增长时,随机数范围也会增加?使用以下方法:C++ 我可以计算这个数组中的元素数吗?,c++,arrays,C++,Arrays,所以基本上我想做的是,不要让int Randnum=rand%6;,我想要一个随数组变化的变量。类似于int Randnum rand%totalwords;。我尝试使用sizeofwords,但只有在我知道每个单词占用多少字节的情况下,它才会起作用。你们有没有办法让我把每个单词都算为一个项目,这样当数组增长时,随机数范围也会增加?使用以下方法: #include <iostream> #include <string> #include <time.h>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int main(){
srand (time( NULL));
const char* words[] = {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};
int Randnum = rand() % 6;
cout << words[Randnum] << endl;
}
这会给你7分
int Randnum=rand%7给你一个介于0和6之间的随机数,单词[Randnum]是一周中的一个随机日 使用以下命令:
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int main(){
srand (time( NULL));
const char* words[] = {"Sunday", "Monday", "Tuesday", "Wedensday", "Thursday", "Friday", "Saturday"};
int Randnum = rand() % 6;
cout << words[Randnum] << endl;
}
这会给你7分
int Randnum=rand%7给你一个介于0和6之间的随机数,单词[Randnum]是一周中的一个随机日 试试看
sizeof(words)/sizeof(words[0])
由于words中的条目是char*,而words的总大小将是存储7个字符指针的字节数请尝试
sizeof(words)/sizeof(words[0])
因为words中的条目是char*,而words的总大小将是存储7个字符指针的字节数