请教一个C语言的问题,关于random函数的。

已知二维数组 a[5)(5]完成下列要求。
(1)输入数据a[i][j]=random(100);
(2) 显示数组各元素,要求整齐排列。
(3)将第1与第5行对调后,再显示;
(4)求出每行元素的最大值,并指出行号和列号。

请给出具体的程序代码,最好有解释。谢谢!

本程序已运行成功
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[5][5];
int i,j,t,row,col,max;
randomize();/*初始化随机数发生器*/
for(i=0;i<5;i++)
for(j=0;j<5;j++)
a[i][j]=random(100);/*产生随机数*/

for(i=0;i<5;i++)/*输出数组*/
{
for(j=0;j<5;j++)
printf("%-5d",a[i][j]);
putchar('\n');
}
for(i=0;i<5;i++)/*第一行和第五行互换*/
{
t=a[0][i];
a[0][i]=a[4][i];
a[4][i]=t;
}
putchar('\n');
for(i=0;i<5;i++)/*输出互换后的数组*/
{
for(j=0;j<5;j++)
printf("%-5d",a[i][j]);
putchar('\n');
}
for(i=0;i<5;i++)/*求每行最大值*/
{
row=i;
col=0;
max=a[i][0];
for(j=0;j<5;j++)
{
if(max<a[i][j])
{
max=a[i][j];
col=j;
}
}
printf("max=%-d row=%-5d colum=%-5d\n",max,row,col);/*输出行、列号,和最大值*/
}
getch();
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-01
/*已知二维数组 a[5)(5]完成下列要求。
(1)输入数据a[i][j]=random(100);
(2) 显示数组各元素,要求整齐排列。
(3)将第1与第5行对调后,再显示;
(4)求出每行元素的最大值,并指出行号和列号。
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define RANGE 100 //宏定义范围
#define random(x) (rand()%x) //宏定义random来产生一个0-x-1范围内的随机数
#define randomize() srand((unsigned int)time(NULL))//宏定义randomize产生随机数rand用的种子,使每次运行结果不一样,不然rand种子默认为1,每次运行结果都一样
void main()
{
int i,j,index,temp;
int a[5][5];
randomize(); //设置seed
printf("print the whole matrix:\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j]=random(RANGE);
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("exchange of row one and row fine:\n");
for(j=0;j<5;j++) //交换两个数,没什么可说的
{
temp=a[0][j];
a[0][j]=a[4][j];
a[4][j]=temp;
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("now print the bigest num of the row:\n");
for(i=0;i<5;i++)
{
index=0;
for(j=1;j<5;j++)
{
if(a[i][index]<a[i][j]) //只要记住找下标就可以了
index=j;
}
printf("the bigest num of the row %d is:a[%d][%d]=%d\n",i+1,i,index,a[i][index]);
}
}

解释:自己花了一个小时才搞定。。。。这里重点说下random函数问题,楼主看自己去看stdlib头文件,random函数c中是通过rand函数实现的,他只是一个壳子而已,所以我的方法才是最佳答案,楼主给份给我吧,哈哈
第2个回答  2009-04-22
C语言没有random函数,只有rand(),是产生任意正整数的,你可用rand()%100来产生0-99之间的随机数
第3个回答  2009-04-22
#include <stdio.h>
#include <stdlib.h> //定义rand()和srand()函数
#include <time.h> ////定义time()

void printArray(int a[][5]) //输出数组元素
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
printf(" %d ",a[i][j]);
printf("\n");
}
}
void max(int a[][5]) // 求出每行元素的最大值,并指出行号和列号。
{
int temp,m;
for(int i=0;i<5;i++)
{
m=0;
temp=a[i][0];
printf("max of Line %d is:",i);
for(int j=1;j<5;j++)
{
if(a[i][j]>temp)
{ temp=a[i][j];m=j;} //m记录最大值的列号
}
printf("%d,at column %d\n",temp,m);
}
}

int main()
{
int temp;
int a[5][5];

srand(time(NULL)); //产生随机种子
for(int i=0;i<5;i++) //随机初始化数组a[i][j]
{
for(int j=0;j<5;j++)
a[i][j]=rand()%100; //rand()%100来产生0-99之间的随机数
}
printf("elements of a[5][5]\n");
printArray(a);
max(a);//求出每行元素的最大值,并指出行号和列号。
for(int m=0;m<5;m++) //将第1与第5行对调后
{
temp=a[0][m];
a[0][m]=a[4][m];
a[4][m]=temp;
}
printf("line5 and line1 changed of a[5][5]\n");
printArray(a);

return 0;
}
第4个回答  2009-04-22
#include <stdio.h>
#include <conio.h>

int a[5][5]={0,0};
int s;
void show(void);
int max(int);

void show(){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
printf("%5d ",a[i][j]);
if(j==4)printf("\n");

}
}
}

int max(int i){
int j,max;
max=a[i][0];
for(j=1;j<5;j++){
if(max<a[i][j]){
max=a[i][j];
s=j+1;
}
}
return(max);
}

main(){
int i,n;

clrscr();
for(i=0;i<5;i++){
for(n=0;n<5;n++){
a[i][n]=rand(100);

}
}

show();
for(i=0;i<5;i++){
n=a[4][i];
a[4][i]=a[0][i];
a[0][i]=n;
}
printf("\n");
show();

for(i=0;i<5;i++){

printf("\n");
printf("max in line"); printf("%d ",i+1);printf("\n");printf("%d",n=max(i));
printf("\n");
printf("It's ");printf("%s" "%d" "%s" "%d" "%s","a[",i+1,"][",s,"]");
printf("\n");

}

}

好久没写了 有些用法比较弱,请大家指点