首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

【新手】随机数赋值有关问题

2012-04-20 
【新手】随机数赋值问题#includeiostream.h #includestdlib.h structstud{charname[8]intxuehao,cj[5],

【新手】随机数赋值问题
#include   "iostream.h "
#include   "stdlib.h "
struct   stud
{
  char   name[8];
  int   xuehao,cj[5],aver,total;
};
void   main()
{
  stud   stu[5];
  int   i,j;
  for(i=0;i <5;i++)   //用随机数赋值
  for(j=0;j <5;j++)
            stu[i].cj[i]=rand()%61+40;

  for(i=0;i <5;i++)   //   验证随机数
  {
      for(j=0;j <5;j++)
      cout < <stu[i].cj[j] < < "     ";
      endl;
  }
}     //程序目的是给   stu中的cj数组赋五个在40~100的随机数

[解决办法]
换成:
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct stud {
char name[8];
int xuehao, cj[5], aver, total;
};
int main() {
stud stu[5];
for (int j = 0; j < 5; j++)
for (int i = 0; i < 5; i++) {
stu[j].cj[i] = rand()%61+40;
}

for (int j = 0; j < 5; j++) {
for (int i = 0; i < 5; i++) {
cout < < stu[j].cj[i] < < "\t ";
}
cout < < endl;
}
}


stu[i].cj[i]=rand()%61+40; // 这里用了两次i
cout < <stu[i].cj[j] < < " ";
endl; // 这里也出错了。

热点排行