【发布时间】:2014-03-23 08:58:05
【问题描述】:
//This code gives randomly generated alphabets and if equal will
//cout the alphabet which is equal
1 #include <iostream>
2 #include <cstdlib>
3 #include <ctime>
4 using namespace std;
5 int main()
6 {
7 int a;
8 char array[10];
9 srand (time(0));
10 for (int i=0; i<10; i++)
11 {
12 a=rand() %26;
13 a=a+97;
14 array[i]=char(a);
15 }
16 for (int i=0; i<10; i++)
17 {
18 for (int j=0; j<10; j++)
19 {
20 if (array [i]==array [j]);
21 {
22 cout <<array[i] <<" " <<array[j];//if alphabet 'c' is at indecis 2,5,8
//then output should be like that of
// only 22 no statement but actually it does
// not give this answer but gives wrong output
//c c
//c c
//c c
23 }
24 cout << endl;
25 }
26 }
27 return 0;
28 } //program end
我的问题是如何检查随机生成的字母是否相等,例如在 22 个数字行中,如果它们相等,它应该输出相等的字母,但它没有给出相等的字母此代码中有什么错误提及错误的语句或帮助我我将如何得到正确的答案
其实我想做一个程序,告诉我一个生成的字母表在一个数组中出现了多少次
【问题讨论】:
-
请添加具有预期输出的示例输入。
-
对不起,我还是不明白你的意思。
-
哪种说法你看不懂
-
我想制作一个程序,告诉我哪个字母在一个数组中出现了多少次?
-
哦,所以你得到的是“c c c c”而不是“2 5 8”?
标签: visual-c++ random g++ srand