C编程辅导:ECE222 Vectors And Matrices
原文链接:tecdat.cn/?p=29614
Requirement
In this lab, each student is to write a program that allows the user to manipulate the entries in vector, or in a matrix. The program should keep track of one vector of variable length, and one matrix of exactly 4x4 size. The program should enter a loop, displaying a set of options (given below). Once the user selects an option, the program should display the vector (or matrix, as appropriate) before and after the operation chosen by the user. For example, if the user selects “reverse vector” and the current vector is [-3 0 2 5] then the program should display:
input
-3 0 2 5
reversed
5 2 0 -1
复制代码
The program should run until the user selects an option to quit. The program must use the following structure definitions.
struct vector {
float *data;
int size;
};
struct matrix {
struct vector rows[4];
};
复制代码
Analysis
Vectors和Matrices,矢量和矩阵,也称一维和二维数组。属于C语言很常见的数据结构。本题要实现的是矢量的反转,以及矩阵的转置。 反转和转置需要用到排序算法,这里我们采用Quicksort,也就是快速排序。
Tips
矢量反转所用的快速排序算法如下
int parition(struct vector *vec, int left, int right) {
float piovt, temp;
int i, j;
piovt = *(vec[left]);
i = left;
j = right + 1;
while (1) {
do {
++i;
} while (*(vec[i]) <= piovt && i <= right);
do {
--j;
} while (*(vec[j]) > piovt);
if (i >= j) {
break;
}
temp = *(vec[i]);
*(vec[i]) = *(vec[j]);
*(vec[j]) = temp;
}
temp = *(vec[left]);
*(vec[left]) = *(vec[right]);
*(vec[right]) = temp;
return j;
}
void quick_sort(struct vector *vec, int left, int right) {
int i;
if (left < right) {
i = partition(vec, left, right);
quick_sort(vec, left, right - 1);
quick_sort(vec, i + 1, right);
}
}
复制代码
相关文章
- JavaScript代做编程辅导:CSE6242 D3 Graphs and Visualization
- 计算机编程原理_如何编程
- 【网络编程开发系列】好端端的MQTT-broker重新部署后居然出现TLS握手失败了
- (十一)编程式导航
- mac科研软件-MATLAB R2022b for Mac(专业编程和数学计算软件)完美激活
- Java高效编程之二【对所有对象都通用的方法】详解编程语言
- 子Linux C:编程之旅的不懈努力(linuxc后缀)
- 利用Linux C编程实现网络广播(linuxc广播地址)
- MySQL中的OR与AND操作符比较(mysqlor和and)
- 多条件查询MySQL中使用And多条件查询的步骤(mysql中and)
- Linux UDP实例:网络编程的利器(linuxudp实例)
- 条件MySQL 子句之间`AND`操作符多条件查询(mysql多个and)
- SQL Server编程面试中的问答(sqlserver问答题)
- 体验Linux与Groovy带来的新型编程乐趣(linuxgroovy)
- C语言编程下MySQL查询效率低何解决(c mysql 查询慢)
- MySQL中AND的使用方法及含义解析(mysql中and的含义)
- MySQL中的AND逻辑操作符是什么(mysql中and是什么)
- MySQL中如何正确使用AND运算符(mysql中and怎么用)
- MySQL中的AND和OR使用逻辑运算符优化查询语句(mysql中and与or)