上個禮拜的好像忘了寫出來...
補一個貼.
─────────────────────────────────────
E1) # include <stdio.h>
#define N(x) (sizeof (x) / sizeof *(x))
int main()
{
int a[] = {1, 2, 3, 4, 5};
char b[] = "This is an array.";
char c[][80] = {"This is a cat.", "That is a dog."};
int i, j;
printf("Content of [a]:\n");
for (i = 0; i < N(a); i++)
printf("%d ", a[i]);
printf("\n\n");
printf("Content of [b]:\n");
for (i = 0; i < N(b) && b[i]; i++)
printf("%c ", b[i]);
printf("\n\n");
printf("Content of [c]:\n");
for (i = 0; i < N(c); i++, putchar('\n'))
for (j = 0; j < N(*c) && c[i][j]; j++)
printf("%c ", c[i][j]);
return 0;
}
--------------------------- E1 output ---------------------------
Content of [a]:
1 2 3 4 5
Content of [b]:
T h i s i s a n a r r a y .
Content of [c]:
T h i s i s a c a t .
T h a t i s a d o g .
─────────────────────────────────────
E2) # include <stdio.h>
# define MAX_MEMBERS 5
int ima(int, double [MAX_MEMBERS]);
int main()
{
double a[MAX_MEMBERS] = {1, 2, 3, 4, 5};
int i, n;
puts("-- Original --");
for (i = 0; i < MAX_MEMBERS; i++)
printf("a[%d] == %f\n", i, a[i]);
n = 3;
printf("Multiplier: %d\n", n);
ima(n, a);
puts("-- Modified --");
for (i = 0; i < MAX_MEMBERS; i++)
printf("a[%d] == %f\n", i, a[i]);
return 0;
}
int ima(int n, double a[MAX_MEMBERS])
{
int i;
for (i = 0; i < MAX_MEMBERS; i++)
a[i] *= n;
return 0;
}
--------------------------- E2 output ---------------------------
-- Original --
a[0] == 1.000000
a[1] == 2.000000
a[2] == 3.000000
a[3] == 4.000000
a[4] == 5.000000
Multiplier: 3
-- Modified --
a[0] == 3.000000
a[1] == 6.000000
a[2] == 9.000000
a[3] == 12.000000
a[4] == 15.000000
─────────────────────────────────────
E3) # include <stdio.h>
# include <time.h>
# include <stdlib.h>
int rolldice(void);
int main()
{
int cnt[6] = {0, }, i;
srand(time(NULL));
for (i = 0; i < 6000; i++)
cnt[rolldice()]++;
for (i = 0; i < 6; i++)
printf("Number: %d, Count: %d, Probability: %f\n",
i + 1, cnt[i], cnt[i] / 6000.0);
return 0;
}
int rolldice()
{
return rand() % 6;
}
--------------------------- E3 output ---------------------------
Number: 1, Count: 1044, Probability: 0.174000
Number: 2, Count: 950, Probability: 0.158333
Number: 3, Count: 999, Probability: 0.166500
Number: 4, Count: 990, Probability: 0.165000
Number: 5, Count: 995, Probability: 0.165833
Number: 6, Count: 1022, Probability: 0.170333
─────────────────────────────────────
--
新詩練習:新鮮。踩破初春裡的狗大便;不經意的滄桑,滿溢著嫩黃的喜悅。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.161.102
※ 編輯: Muscovy 來自: 61.224.161.102 (05/29 14:53)