前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C编程辅导:ECE222 Vectors And Matrices

C编程辅导:ECE222 Vectors And Matrices

原创
作者头像
拓端
发布2022-10-25 15:14:24
2160
发布2022-10-25 15:14:24
举报
文章被收录于专栏:拓端tecdat

原文链接: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:

代码语言:javascript
复制
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.

代码语言:javascript
复制
struct vector {
  float *data;
  int    size;
};

struct matrix {
  struct vector rows[4];
};
复制代码

Analysis

Vectors和Matrices,矢量矩阵,也称一维和二维数组。属于C语言很常见的数据结构。本题要实现的是矢量的反转,以及矩阵的转置。 反转和转置需要用到排序算法,这里我们采用Quicksort,也就是快速排序

Tips

矢量反转所用的快速排序算法如下

代码语言:javascript
复制
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);
  }
}
复制代码

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原文链接:tecdat.cn/?p=29614
    • Requirement
      • Analysis
        • Tips
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档