【403】COMP9024 Exercise
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("\n");
} 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\n", 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) = '\0';
} *(rr + j - i) = (char)j; if (flag == 1)
{
*(rr + j - i + 1) = '\0';
printf("%s\n", rr);
}
}
}
}
※ 在字符串赋值的过程中,最后需要添加 '\0',否则会乱码。
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
Write a C program that calls the function in Part a. with the first 10 Fibonacci numbers. The program should print the Fibonacci number followed by its corresponding series. The first 4 lines of the output is as follows:
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!!!\n");
else
printf("%d\n", n); while (n != 1)
{
if (n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
printf("%d\n", 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;
}
【403】COMP9024 Exercise的更多相关文章
- 【434】COMP9024 Exercises Revision
目录: Week01 Week02 Week03 Week04 Week05 Week06 Week07 Week08 Week09 Week10 01. Week01 数字通过 #define 来定 ...
- 【411】COMP9024 Assignment1 问题汇总
1. 构建 Makefile 文件后运行错误,undefined reference to 'sqrt' 实际上是没有链接math数学库,所以要 $gcc test.c –lm //-lm就是链接到m ...
- 【433】COMP9024 复习
目录: 01. Week01 - Lec02 - Revision and setting the scene 02. Week02 - Lec01 - Data structures - memor ...
- 【423】COMP9024 Revision
目录: array '\0' 与 EOF 二维字符数组(字符串数组) 1. array: 参考:C 数组 参考:C 字符串 参考:C笔记之NULL和字符串结束符'\0'和EOF 总结:[个人理解,可能 ...
- 【AtCoder】【思维】【置换】Rabbit Exercise
题意: 有n只兔子,i号兔子开始的时候在a[i]号位置.每一轮操作都将若干只兔子依次进行操作: 加入操作的是b[i]号兔子,就将b[i]号兔子移动到关于b[i]-1号兔子现在所在的位置对称的地方,或者 ...
- 【AGC006C】Rabbit Exercise 置换
题目描述 有\(n\)只兔子站在数轴上.为了方便,将这些兔子标号为\(1\ldots n\).第\(i\)只兔子的初始位置为\(a_i\). 现在这些兔子会按照下面的规则做若干套体操.每一套体操由\( ...
- 【agc006C】Rabbit Exercise
Portal --> agc006C Solution 啊感觉是好有意思的一道题qwq官方题解里面的说辞也是够皮的哈哈哈..(大概就是说如果你没有意识到那个trick的话这题这辈子都做不出来qw ...
- 【432】COMP9024,Exercise9
eulerianCycle.c What determines whether a graph is Eulerian or not? Write a C program that reads a g ...
- 【UFLDL】Exercise: Convolutional Neural Network
这个exercise需要完成cnn中的forward pass,cost,error和gradient的计算.需要弄清楚每一层的以上四个步骤的原理,并且要充分利用matlab的矩阵运算.大概把过程总结 ...
随机推荐
- ORACLE SQL性能优化汇总
ORACLE SQL语句共享 Oracle SQL语句具备共享特性,为了不让ORACLE数据库重复解析相同的简单单表SQL语句,ORACLE在SGA系统共享区域内SBP共享池内存放的SQL语句将被所有 ...
- 推荐排序---Learning to Rank:从 pointwise 和 pairwise 到 listwise,经典模型与优缺点
转载:https://blog.csdn.net/lipengcn/article/details/80373744 Ranking 是信息检索领域的基本问题,也是搜索引擎背后的重要组成模块. 本文将 ...
- 自定义View-----汽泡效果
先来看一下这次要实现的最终效果: 首先来实现效果一,为实现效果二做充足的准备,下面开始: 新建工程,并定义一个自定义View,然后将其定义在布局文件中,里面是空实现,之后会一步步来填充代码: MyRi ...
- 【转】Golang汇编命令解读
原文: https://www.cnblogs.com/yjf512/p/6132868.html ------------------------------------------------- ...
- ES使用org.elasticsearch.client.transport.NoNodeAvailableException: No node available
1) 端口错 client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(ipAddress, ...
- Java锁--公平锁
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3496147.html 基本概念 本章,我们会讲解“线程获取公平锁”的原理:在讲解之前,需要了解几个基本概 ...
- 使用math中的hypot实现向量
from math import hypot class Vector: def __init__(self,x=0,y=0): self.x = x self.y = y def __repr__( ...
- 51nod 1843 排列合并机(DP+组合)
题解链接 不过求ggg不用O(n2)DPO(n^2)DPO(n2)DP,g[n]g[n]g[n]直接就是卡特兰数的第n−1n-1n−1项.即: g[n]=(2(n−1)n−1)−(2(n−1)n−2) ...
- 使用docker 起容器配置负载均衡(加权)
首先要准备三个nginx的容器: 第二个容器: 第三个容器: 进入第一个容器(主容器) 要配置的容器(docker exec -it 容器id /bin/bash) vi/etc/nginx/ng ...
- 1、python--第一天练习题
#1.使用while循环输入 1 2 3 4 5 6 8 9 10 k = 0 while k < 10: k += 1 if k == 7: continue print(k) #2.求1-1 ...