自学C day09-二维数组

打印数组

打印一维数组的话,用一个for循环
打印二维数组的话,用两个for循环

#include <stdio.h>
//二维数组的打印
int main(void) {
	int arr[3][5] = {{2,3,45,5,6}, {5,6,4,8,9}, {6,3,4,8,9}};
	for (size_t i = 0; i < 3; i++)
	{
		for (size_t j = 0; j < 5; j++)
		{
			printf("%d ", arr[i][j]);
		}
		printf("\n");
	}

	return 0;
}

二维数组求大小

数组大小:sizeof(arr);//数组名代表的是整个数组,所以直接求就行了
P.S:sizeof的返回值是无符号的整数,%u
一行的大小:求任意行和指定行的方法都是一样的, sizeof(arr[0])
这里的arr【0】代表的就是第一行,不再是第一个了
行数:总大小/一行的大小

	printf("%u\n", sizeof(arr));		//整个数组的大小
	printf("%u\n", sizeof(arr[0]));		//一行的大小
	printf("%u\n", sizeof(arr[0][0]));	//一个元素的大小
	printf("%u\n", sizeof(arr)/sizeof(arr[0]));	//行数
	printf("%u\n", sizeof(arr[0])/ sizeof(arr[0][0]));	//列数,一行大小/一个元素的大小

二维数组的一行就是一个一维数组

二维数组地址合一

数组的首地址 == 数组的首元素地址 ==数组的首行地址

二维数组的初始化

1、常规方法:指定行数、列数,依次赋值
2、不完全初始化:int arr[3][5] = {{2,3},{4,5}},没被初始化的值默认为0,也可以直接arr[3][4] = { 0 };这是初始化初值全为0 的二维数组
arr[3][4] = {1,2,3,4,5,6}

二维数组指定的时候一定要指定列值

用二维数组求5名学生3门课的总成绩(一个学生的总成绩和一门功课的总成绩)

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*
15 69 87
13 58 79
14 15 16
36 78 89
15 36 36
*/
int main(void) {
	int score[5][3];

	int row = sizeof(score) / sizeof(score[0]);	//行数:总大小/第一行的大小
	int col = sizeof(score[0]) / sizeof(score[0][0]);

	printf("请分别输入5名学生的三科成绩:\n");
	for (int i = 0; i < row; i++)			//行
	{
		for (int j = 0; j < col; j++)		//列
		{
			scanf("%d",&score[i][j]);
		}
	}
	//分隔符
	printf("===================");
	printf("\n");

	//获取5名学生三门功课的成绩
	int sum = 0;
	for (int i = 0; i < row; i++)			//每个学生
	{
		for (int j = 0; j < col; j++)		//每个学生的成绩
		{
			sum = sum + score[i][j];
		}
		printf("第%d个学生的总成绩为:%d\n",i+1,sum);
	}
	printf("===================");
	printf("\n");

	//一门功课的总成绩
	for (int i = 0; i < col; i++)		//第几门功课
	{
		int sum = 0;
		for (int j = 0; j < row; j++)	//每门功课的第几个学生
		{
			sum = sum + score[j][i];
		}
		printf("第%d个科目的总成绩为:%d\n", i + 1, sum);
	}

	return 0;
}

代码快捷导入

VS->工具->代码片段管理器->语言选择visual C++,把文档存放的目录选中(文件后缀必须是 .snippet)
文档的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>#1</Title>
			<Shortcut>#1</Shortcut>
			<Description>c语言main函数</Description>
			<Author>Microsoft Corporation</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>	
				<Literal>
					<ID>expression</ID>
					<ToolTip>要计算的表达式</ToolTip>
					<Default>true</Default>
				</Literal>
			</Declarations>	
			<Code Language="cpp"><![CDATA[#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
	$selected$$end$
	system("pause");
	return EXIT_SUCCESS;
}
		]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

printf特性

printf函数有\0才停的特性,%s,要手动加\0,不然会出现一堆乱码,这是用’a’来赋值会产生的情况
以“”来赋值的字符数组才不用谢\0

统计字符串中每个字符出现的次数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
	char str[11] = { 0 };		//每个元素都是\0 
								//因为每个\0的ASCII码都是0
	//scanf("%s",str)
	printf("请输入小于100个字符:\n");
	for (size_t i = 0; i < 10; i++)
	{
		scanf("%c", &str[i]);
	}

	//统计每个字符出现的次数
	int count[26] = { 0 };		//代表字母表出现的次数
	for (size_t i = 0; i < 11; i++)
	{
		int index = str[i] - 'a';	//用户输入的字符在count中的下标值
		count[index]++;
	}

	for (size_t i = 0; i < 26; i++)
	{
		if (count[i] != 0)
		{
			printf("%c字符在字符串中出现%d次\n", i + 'a', count[i]);
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值