#include <stdio.h>
#define N 2
#define M 4
int main() {
int a[N][M] = { {1, 9, 8, 4}, {2, 0, 2, 2} };
char b[N][M] = { {'1', '9', '8', '4'}, {'2', '0', '2', '2'} };
int i, j;
// 输出二维数组a中每个元素的地址和值
for (i = 0; i < N; ++i)
for (j = 0; j < M; ++j)
printf("%p: %d\n", &a[i][j], a[i][j]);
printf("\n");
// 输出二维数组a中每个元素的地址和值
for (i = 0; i < N; ++i)
for (j = 0; j < M; ++j)
printf("%p: %c\n", &b[i][j], b[i][j]);
return 0;
}
是按行连续存放 每个元素占用4个字节
是按行连续存放 每个元素占用1个字节
#include <stdio.h>
#define N 13
int days_of_year(int year, int month, int day);
int main() {
int year, month, day;
int days;
while (scanf_s("%d%d%d", &year, &month, &day) != EOF) {
days = days_of_year(year, month, day);
printf("%4d-%02d-%02d是这一年的第%d天.\n\n", year, month, day, days);
}
return 0;
}
// 函数定义
// 补足函数days_of_year的定义
// ×××
int days_of_year(int year, int month, int day){
int days;
if (month <= 2) {
switch (month) {
case 1:return day;
case 2: days = day + 31; return days;
}
}
switch (month)
{
case 3: days = 31 + 28 + day; break;
case 4: days = 31 + 28 + 31 + day; break;
case 5: days = 31 + 28 + 31 + 30 + day; break;
case 6: days = 31 + 28 + 31 + 30 + 31 + day; break;
case 7: days = 31 + 28 + 31 + 30 + 31 + 30 + day; break;
case 8: days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day; break;
case 9: days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day; break;
case 10:days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day; break;
case 11:days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day; break;
case 12:days = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day; break;
}
if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0)
{
days += 1; return days;
}
else return days;
}
#include <stdio.h>
#define N 5
// 函数声明
void input(int x[], int n);
void output(int x[], int n);
double average(int x[], int n);
void bubble_sort(int x[], int n);
int main() {
int scores[N];
double ave;
printf("录入%d个分数:\n", N);
input(scores, N);
printf("\n输出课程分数: \n");
output(scores, N);
printf("\n课程分数处理: 计算均分、排序...\n");
ave = average(scores, N);
bubble_sort(scores, N);
printf("\n输出课程均分: %.2f\n", ave);
printf("\n输出课程分数(高->低):\n");
output(scores, N);
return 0;
}
// 函数定义
// 输入n个整数保存到整型数组x中
void input(int x[], int n) {
int i;
for (i = 0; i < n; ++i)
scanf_s("%d", &x[i]);
}
// 输出整型数组x中n个元素
void output(int x[], int n) {
int i;
for (i = 0; i < n; ++i)
printf("%d ", x[i]);
printf("\n");
}
// 计算整型数组x中n个元素均值,并返回
// 补足函数average()实现
// ×××
double average(int x[], int n) {
int i;
int average=0;
for (i = 0; i < n; i++) {
average += x[i];
}
average /= n; return average;
}
// 对整型数组x中的n个元素降序排序
// 补足函数bubble_sort()实现
// ×××
void bubble_sort(int x[], int n) {
int i, j, t;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (x[j] < x[j + 1]) {
t = x[j];
x[j] = x[j + 1];
x[j + 1] = t;
}
}
}
}
#include <stdio.h>
#define N 100
// 函数声明
void func(int x[][N], int n); // 注意:两维数组作为形参的语法
void output(int x[][N], int n); // 注意:两维数组作为形参的语法
int main() {
int x[N][N];
int i, j, n;
printf("Enter n: ");
while (scanf_s("%d", &n) != EOF) {
func(x, n); // 注意:两维数组作为实参的语法
output(x, n); // 注意:两维数组作为实参的语法
printf("\nEnter n: ");
}
return 0;
}
// 函数定义
// 功能:输出两维数组x中的数据,数据项n行、n列
void output(int x[][N], int n) {
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j)
printf("%5d", x[i][j]);
printf("\n");
}
}
// 函数定义
// 功能:生成n行n列的左上拐矩阵,保存在两维数组x中
// 补足函数实现
// ×××
void func(int x[][N], int n) {
int i, j;
for (i = 0; i < n; i++)
x[i][i] = i+1;
for ( j= 0; j< n; j++)
for ( i= 0; i < n; i++) {
x[i][j] = x[i][i];
x[j][i] = x[i][j];
}
}
#include <stdio.h>
#include <string.h>
#define N 80
int main() {
char views1[N] = "hey, C, I have not love u yet.";
char views2[N] = "hey, C, how can I love u?";
char t[N];
printf("交换前: \n");
printf("views1: %s\n", views1);
printf("views2: %s\n", views2);
// 交换
strcpy_s(t, views1);
strcpy_s(views1, views2);
strcpy_s(views2, t);
printf("交换后: \n");
printf("views1: %s\n", views1);
printf("views2: %s\n", views2);
return 0;
}
#include <stdio.h>
#include <string.h>
#define N 80
int main() {
char views[2][N] = { "hey, C, I have not love u yet.",
"hey, C, how can I love u?" };
char t[N];
printf("交换前: \n");
printf("views1: %s\n", views[0]);
printf("views2: %s\n", views[1]);
// 交换
strcpy_s(t, views[0]);
strcpy_s(views[0], views[1]);
strcpy_s(views[1], t);
printf("交换后: \n");
printf("views1: %s\n", views[0]);
printf("views2: %s\n", views[1]);
return 0;
}
#include <stdio.h>
#include <string.h>
#define N 5
#define M 20
void bubble_sort(char str[][M], int n); // 函数声明
int main() {
char name[][M] = { "Bob", "Bill", "Joseph", "Taylor", "George" };
int i;
printf("输出初始名单:\n");
for (i = 0; i < N; i++)
printf("%s\n", name[i]);
printf("\n排序中...\n");
bubble_sort(name, N); // 函数调用
printf("\n按字典序输出名单:\n");
for (i = 0; i < N; i++)
printf("%s\n", name[i]);
return 0;
}
// 函数定义
// 功能:使用冒泡排序算法对二维数组str中的n个字符串按字典序排序
// 补足函数bubble_sort()实现
// ×××
void bubble_sort(char str[][M], int n) // 函数声明
{
int i, j, t;
int c;
char tem[M];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4 - i; j++) {
for (t = 0; t < M; t++) {
if (str[j][t] > str[j + 1][t]) {
for (c = 0; c < M; c++) {
tem[c] = str[j][c];
str[j][c] = str[j + 1][c];
str[j + 1][c] = tem[c];
}
}
}
}
}
}
#include <stdio.h>
#define N 100
void dec2n(int x, int n); // 函数声明
int main() {
int x;
printf("输入一个十进制整数: ");
while (scanf_s("%d", &x) != EOF) {
dec2n(x, 2); // 函数调用: 把x转换成二进制输出
dec2n(x, 8); // 函数调用: 把x转换成八进制输出
dec2n(x, 16); // 函数调用: 把x转换成十六进制输出
printf("\n输入一个十进制整数: ");
}
return 0;
}
void dec2n(int x, int n)
{
int i;
int f[N];
int t;
int m = 0;
int a[N];
int v;
v = x;
while (v != 0) { a[m] = v / n; v = v / n; m++; }
for (t = 0; t < m; t++) {
f[t] = a[m - 1 - t];
}f[m] = x;
for (i = 1; i < m+1 ; i++)
{
f[i] = f[i] % n;
if (f[i] > 9 && f[i] < 16) {
switch (f[i]) {
case 10:printf("A"); break;
case 11:printf("B"); break;
case 12:printf("C"); break;
case 13:printf("D"); break;
case 14:printf("E"); break;
case 15:printf("F"); break;
}
}
else printf("%d", f[i]);
}
printf("\n");
}
标签:case,int,31,编程,days,实验,数组,printf,day From: https://www.cnblogs.com/yuure/p/16921689.html