Week 1 Exercises
fiveDigit.c
There is a 5-digit number that satisfies 4 * abcde = edcba, that is,when multiplied by 4 yields the same number read backwards.Write a C-program to find this number.
int swap_num(int a)
{
int result = 0;
while (1)
{
int i = a % 10;
result = result * 10 + i;
a = a / 10;
if (a == 0)
break;
}
return result;
}
innerProdFun.c
Write a C-function that returns the inner product of two n-dimensional vectorsa and b, encoded as 1-dimensional arrays of n floating point numbers.
Use the function prototype float innerProduct(float a[], float b[], int n).
By the way, the inner product of two vectors is calculated by the sum for i=1..n of ai * bi
float innerProduct(float a[], float b[], int n)
{
int i;
float sep, sum = 0.0;
for (i = 0; i < n; i++)
{
sep = a[i] * b[i];
sum = sum + sep;
}
return sum;
}
matrixProdFun.c
Write a C-function to compute C as the matrix product of matrices A and B.
Use the function prototype void matrixProduct(float a[M][N], float b[N][P], float c[M][P])
You can assume that M, N, P are given as symbolic constants, e.g.
#define M 3
#define N 4
#define P 4
By the way, the product of an m x n matrix A and an n x p matrix B is the m x p matrix C such that Cij is the sum for k=1..n of Aik * Bkj for all i∈{1..m} and j∈{1..p}
void matrixProduct(float a[M][N], float b[N][P], float c[M][P])
{
float innerProduct(float a[], float b[], int n);
int i, j, k;
float sum;
float rr[N], cc[N];
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
rr[j] = a[i][j];
for (k = 0; k < P; k++)
{
for (j = 0; j < N; j++)
cc[j] = b[j][k];
sum = innerProduct(rr, cc, N);
c[i][k] = sum;
}
}
}
数据调用
#include <stdio.h>
#define M 2
#define N 3
#define P 2
int main()
{
void matrixProduct(float a[M][N], float b[N][P], float c[M][P]);
float innerProduct(float a[], float b[], int n);
float a[2][3] = { 1, 2, 3, 4, 5, 6 };
float b[3][2] = { 1, 2, 3, 4, 5, 6 };
float c[2][2];
matrixProduct(a, b, c);
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%0.0f ", c[i][j]);
}
printf("
");
}
return 0;
}
able.c
Write a C-program that outputs, in alphabetical order, all strings that use each of the characters 'a', 'b', 'l', 'e' exactly once.
How many strings are there actually?
void able()
{
char a = 'a';
char b = 'b';
char l = 'l';
char e = 'e';
char z = 'z';
char rr[26];
char ss[26];
int i, j, tmp, flag;
for (i = (int)a; i <= (int)z; i++)
{
flag = 0;
if (i == (int)a || i == (int)b || i == (int)l || i == (int)e)
flag++;
*(rr) = (char)i;
if (flag == 1)
printf("%c
", i);
for (j = i + 1; j <= (int)z; j++)
{
if (j == (int)a || j == (int)b || j == (int)l || j == (int)e)
flag++;
if (flag > 1)
{
break;
*(rr + j - i + 1) = ' ';
}
*(rr + j - i) = (char)j;
if (flag == 1)
{
*(rr + j - i + 1) = ' ';
printf("%s
", rr);
}
}
}
}
※ 在字符串赋值的过程中,最后需要添加 ' ',否则会乱码。
collatzeFib.c
-
Write a C-function that takes a positive integer n as argument and prints a series of numbers generated by the following algorithm, until 1 is reached:
-
if n is even, then n ← n/2
-
if n is odd, then n ← 3*n+1
(Before you start programming, calculate yourself the series corresponding to n=3.)
-
- The Fibonacci numbers are defined as follows:
- Fib(1) = 1
- Fib(2) = 1
-
Fib(n) = Fib(n-1)+Fib(n-2) for n≥3
Fib[1] = 1: Fib[2] = 1: Fib[3] = 2: 1 Fib[4] = 3: 10 5 16 8 4 2 1
a - code
void even_odd(int n)
{
if (n < 0)
printf("Please input a positive integer!!!
");
else
printf("%d
", n);
while (n != 1)
{
if (n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
printf("%d
", n);
}
}
b - code
int* Fib()
{
static int ff[10];
ff[0] = 1;
ff[1] = 1;
int i;
for (i = 2; i < 10; i++)
ff[i] = ff[i - 2] + ff[i - 1];
return ff;
}
※ 注意返回数组的方法,另外需要通过 static 关键字来定义数组。
调用数据:
#include <stdio.h>
int main()
{
void even_odd(int n);
int* Fib();
int i;
int* fib_arr;
fib_arr = Fib();
for (i = 0; i < 10; i++)
{
printf("Fib[%d] = %d:", i + 1, fib_arr[i]);
even_odd(fib_arr[i]);
}
return 0;
}
参考:C 从函数返回数组
fastMax.c
Write a C-function that takes 3 integers as arguments and returns the largest of them. The following restrictions apply:
- You are permitted to only use assignment statements, a return statement and Boolean expressions
-
You are not permitted to use if-statements, loops (e.g. a while-statement), function calls or any data or control structures
int largest(int a, int b, int c)
{
int max;
max = (a > b) ? a: b;
max = (max > c) ? max : c;
return max;
}