| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 625 人关注过本帖
标题:[求助]随即函数。
取消只看楼主 加入收藏
初心者1号
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2007-3-31
收藏
 问题点数:0 回复次数:2 
[求助]随即函数。

我想用随即函数输出1个9*9的矩阵,但是现在矩阵里的数字都是同1个数字。
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int temp=0;
int count=0;
#define N 9
int input(int x)
{
// int i,j;
srand((unsigned)time(NULL));
x=rand()%9+1;
return x;
}
/*int jiancha(int *p)
{
if(temp==*p)
{
input(p);
}
return ;
}*/
void output(int a[][N])
{
int i,j,count=0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%-3d",a[i][j]);
count++;
if(count%9==0)
printf("\n");
}

}
void main()
{
int row,col,ary[N][N];
int p;
// p=&ary[0][0];
for(row=0;row<N;row++)
for(col=0;col<N;col++)
{
p=0;
ary[row][col]=input(p);
// ary[row][col]=jiancha(p);
// p++;
}
output(ary);
}

搜索更多相关主题的帖子: int 函数 include input count 
2007-04-24 20:42
初心者1号
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2007-3-31
收藏
得分:0 
结果还是一样
2007-04-24 22:08
初心者1号
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2007-3-31
收藏
得分:0 

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int temp=0;
int count=0;
#define N 9
int input(int x) //输入函数
{
x=rand()%9+1;
return x;
}
/*int jiancha(int *p)
{
if(temp==*p)
{
input(p);
}
return ;
}*/
void output(int a[][N])
{
int i,j,count=0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%-3d",a[i][j]);
count++;
if(count%9==0)
printf("\n");
}

}
void main()
{
int row,col,ary[N][N];
int p;
srand((unsigned)time(NULL));//应该放在着。
for(row=0;row<N;row++)
for(col=0;col<N;col++)
{
ary[row][col]=input(p);
}
output(ary);
}
问题解决了,错在生成随即函数放错了地方。

2007-04-25 20:26
快速回复:[求助]随即函数。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016189 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved