二维数组训练

文章介绍了如何使用C语言中的二维数组来存储和处理模拟的三年生活费支出数据。通过srand和rand函数生成300-800之间的随机数,填充二维数组,并提供了打印、统计平均月支出、每年总支出及最高消费月份的方法。示例代码展示了如何实现这些功能。
摘要由CSDN通过智能技术生成

二维数组的训练

参考:随机数函数:srand和rand

  • rand函数调用
    rand()函数每次调用前都会查询是否调用过srand(seed),是否给seed设定了一个值,如果有那么它会自动调用srand(seed)一次来初始化它的起始值
    若之前没有调用srand(seed),那么系统会自动给seed赋初始值,即srand(1)自动调用它一次

  • rand()产生随机数时,如果用srand(seed)播下种子之后,一旦种子相同(下面的getpid方法),产生的随机数将是相同的。当然很多时候刻意让rand()产生的随机数随机化,用时间作种子
    srand(time(NULL)),这样每次运行程序的时间肯定是不相同的,产生的随机数肯定就不一样了

  • 我们常常使用系统时间来初始化,使用time函数来获取系统时间,得到的值是一个时间戳,即从1970年1月1日0点到现在时间的秒数,然后将得到的time_t类型数据转化为(unsigned
    int)的数,然后再传给srand函数,用法如下:

  • srand((unsigned int)time(NULL));//我们在使用rand和srand时,主要使用的就是这一种初始化方法!!
    如果仍然觉得时间间隔太小,可以在(unsigned)time(0)或者(unsigned)time(NULL)后面乘上某个合适的整数。

   例如,srand((unsigned)time(NULL)*10)

time的参数传NULL表示不需要经过参数获得到的time_t数据,

time函数原型如下time_t time(time_t *tloc);//time_t类型被定义为一个长整型

还有另外一种初始化种子的方式如下,用进程的pid作为种子值seed,在同一个程序中,这样的种子的值是相同的

srand((unsigned int)getpid())
  • rand函数的使用 如果想要表示一个数是从0开始到最大值的,比如说,想要产生一个0-99之间的随机数,那么用法如下
int num = rand() % 100
  • 如果想要产生一个数是从1开始到最大值的,比如说,想要产生一个1-100之间的随机数,那么用法如下
int num = rand() % 100 + 1;
  • 需要注意最后+1和不+1的区别,+1的最小值是1,不+1的最小值是0
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
	//int n = rand(); //rand()产生一个随机数的函数 	最大值	0 到 RAND_MAX (32767) 的一个伪随机整数
	
	srand(time(NULL));	//time(NULL)每一秒改一次
	rand();		//第一个随机数滤掉
	int n = 100;
	while (n--)
	{
		printf("%d ", rand());
	}
}


前言

1、用二维统计大学3年内的每月生活费支出:

double as[3][12] = {0};
a)用随机数函数对以上数组的每个元素赋值,随机数的范围在300-800之间;
b)在生成所有二维数组的数据后,按照3行12列输出每年每月的费用支出;
c)统计3年中所有36个月平均每月的生活费支出;
d)循环统计第1年,第2年,第3年的每年生活费的总支出和当年平均月支出;
e)循环统计1、2、3、4…12每个月份3年的平均开支;
并选出平均消费最大的月份,也就是找出3年中平均哪个月份是消费最高的。

2、使用二维数组做参数来实现以上3种统计运算。

a)创建数据的函数:void CreateData(double as[3][12], int nLines)//输出型参数
(注意创建数据时不做任何统计和打印工作)
b)在生成所有二维数组的数据后,按照3行12列输出每年每月的费用支出;
c)统计打印36个月的平均月支出:void AverageMonth(double as[][12], int nLines)
d)统计打印每年的总支出和当年平均月支出:void SumYear(double as[][12], int nLines)
e)统计打印每个月份的平均值,并求出几月份消费最高:void MaxMonth(double as[][12], int nLines)


一、示例代码如下:

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void CreateData(double as[3][12], int nLines)
{
	double* p = *as;//as[0]
	int i =  nLines * _countof(*as);	//_countof(as)-->3  _countof(*as)--->12
	while (i--)
		*p++ = ((rand() + rand()) % 50000 + 30000) / 100.0;
}
/*
void CreateData(double as[3][12], int nLines)
{
	int i = -1;
	while (++i < nLines)  //行
	{
		int j = -1;
		while (++j < _countof(*as)) //_countof(*as) 或者_countof(*as) //列
		{
			//as[i][j] = rand()%500+300; //生成的数是0-799		rand()的范围为0-32767
			//as[i][j] += rand() % 100 / 100.0;
			int d = rand() + rand(); //65535
			as[i][j] = (d % 50000 + 30000) / 100.0;
		}
	}
}
*/
/*
void Print(double as[3][12], int nLines)
{
	int i = -1;
	while (++i < nLines)
	{
		int j = -1;
		while (++j < _countof(*as))
		{
			printf("%0.2f", as[i][j]);
		}
		putchar(10);
	}
}
*/
void Print(double as[3][12], int nLines)
{
	double* p = *as;//as[0]
	int i = nLines * _countof(*as);
	while (i--)
	{
		printf("%-6.2f ",*p++);  //-左对齐 不带- 右对齐
		if (i%_countof(*as)==0)
			putchar(10);
	}
}
void Sum(double as[3][12], int nLines)
{
	double* p = *as;//as[0]
	int n = nLines * _countof(*as),i=-1;
	double sum = 0;
	while (++i<n)
		sum += *p++;
	printf("%d个月的平均指出是:%0.2f\n", n, sum / n);
}

void SumYear(double as[3][12], int nLines)
{
	int i = -1;
	double* p = *as;
	while (++i<nLines)
	{
		int j = -1;
		double sum = 0.0;

		while (++j < _countof(*as))
		{
			sum += *p++;
		}
		printf("第%d年的总支出是:%0.2f,平均每月:%0.2f\n", i+1, sum ,sum/ j);
	}
}
void MaxMonth(double as[][12], int nLines)
{
	int i = -1;
	int n = 0;		//代表月份
	double max = 0.0; //代表最大值
	while (++i < _countof(*as))
	{
		int j = -1;
		double sum = 0.0;

		while (++j < nLines)
		{
			sum += as[j][i]; 
		}
		if (max<sum)
		{
			n = i;
			max = sum;
		}
		printf("%d月份 平均支出:%0.2f\n", i + 1, sum / nLines);
	}
	printf("支出最大的月份是:%d月支出%0.2f\n", n + 1,max/nLines);

}
int main()
{
	srand(time(NULL));
	rand();
	double as[3][12] = { 0 };
	CreateData(as, _countof(as));
	Print(as, _countof(as));	
	Sum(as, _countof(as));
	SumYear(as, _countof(as));
	MaxMonth(as, _countof(as));
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jcrry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值