Ma trận tam giác trên c++

#include 

// Large math problems running on massively parallel systems sometimes use a lot
// of upper triangular matrices.  Implemented naively, these waste 50% of the memory
// in the machine, which is not recoverable by virtual memory techniques because it
// is interspersed with data on each row.  By mapping the array elements into an
// array that is half the size and not actually storing the zeroes, we can do twice
// the computation in the same machine or use half as many machines in total.

// To implement a safety feature of making the zero-part of an upper triangular matrix
// read-only, we place all the zeroes in write-protected memory and cause a memory violation
// if the programmer attempts to write to them.  System dependent but relatively portable.
// Requires that you compile with the -Wno-discarded-qualifiers option.

// for the awkward case (an even-sized array bound):

//                         +--------/
//     row  0, 40 items -> |0      /
//     row  1, 39 items -> |      /
//     row 19, 21 items -> |     /
//     row 20, 20 items -> |----/ <------  cut and rotate here to form a rectangle.
//     row 21, 19 items -> |   /
//                         |  /
//     row 39,  1 item  -> | /
//     row 40,  0 items -> |/
//                         /


//    x y   x  y
//    0,0   39,0
//     +----/           |                     +--------/
//     |   /            | row  0, 40 items -> |0      /| <-- row 40, 0 items
//     |  / - 20,18     | row  1, 39 items -> |      /0| <-- row 39, 1 item
//     | /\             | row 19, 21 items -> |     /  | <-- row 21, 19 items
//     |/  19,19        | row 20, 20 items -> |    /???| <-- row 20, 20 items  half of row 20 is wasted...
//0,39 v                |                     ~~~~~~~~~~
//     |                |

// for odd-sized array bounds, there is no need for the wasted half-row marked '???' above...

// And once the mapping above is done, mirror the indexes in x to get a proper Upper Triangular Matrix which
// looks like this...
//     ____
//     \  |
//      \ |
//       \|
//

// Rather than store the underlying data in a 2D array, if we use a 1-D array,
// and map the indexes ourselves, it is possible to recover that final half-row...

// The implementation allows for the matrix elements to be any scalar type.

#define DECLARE_TRIANGULAR_MATRIX(type, name, bound, zero)                      \
  type _##name[bound * (bound+1) / 2 + 1]; /* +1 is for a debugging tombstone */ \
  type *__##name(int x, int y) { \
    static const type Zero = zero; /* writing to the lower half of the matrix will segfault */ \
    x = (bound-1)-x; /* mirror */ \
    if (x+y >= bound) return &Zero; /* requires cc -Wno-discarded-qualifiers */ \
    if (y > bound/2) {x = (bound-1)-x; y = bound-y;} \
    return &_##name[y*bound+x]; /* standard mapping of x,y -> X */ \
  }
#define TRIANGULAR_MATRIX(name, x, y)  *__##name(x,y)


// ----------------------------------------------------------------------------------------


// Simulate 'int fred[11][11];' as an upper triangular matrix:
#define ARRAYSIZE 11
DECLARE_TRIANGULAR_MATRIX(int, fred, ARRAYSIZE, 0)
#define fred(x, y) TRIANGULAR_MATRIX(fred, x, y)
/* unfortunately we can't #define fred[x][y] here .. In the Imp language which used () both
   for array indexes and procedure parameters, we could write a mapping function fred(x,y)
   which made the indirected function call indistinguishable from a normal array access.
   We attempt to do something similar here using macros, but C is not as cooperative. */



int main(int argc, char **argv) {
  int x,y, element;

  // treat as fully populated 2D array...
  for (y = 0; y < ARRAYSIZE; y++) {
    for (x = 0; x < ARRAYSIZE; x++) {
      if (y <= x) fred(x,y) = (x+1) * 100 + (y+1); // upper triangle test
    }
  }

  fprintf(stdout, "Upper Triangular matrix:\n\n");
  fprintf(stdout, "    ");
  for (x = 0; x < ARRAYSIZE; x++) fprintf(stdout, "%5d", x);
  fprintf(stdout, "\n    ");
  for (x = 0; x < ARRAYSIZE; x++) fprintf(stdout, "_____");
  fprintf(stdout, "\n");
  for (y = 0; y < ARRAYSIZE; y++) {
    fprintf(stdout, "%2d |", y);
    for (x = 0; x < ARRAYSIZE; x++) {
      element = fred(x,y);
      fprintf(stdout, "%5d", element);
      if (y <= x) { // upper triangle test
        if (element != (x+1) * 100 + (y+1)) {
          fflush(stdout); fprintf(stderr, "Mismatch! at %d,%d (%d != %d)\n", x, y, element, x * 100 + y);
        }
      } else if (element != 0) {
        fflush(stdout); fprintf(stderr, "Mismatch! at %d,%d (%d != 0)\n", x, y, element);
      }
    }
    fprintf(stdout, "\n");
  }

  return 0;
}

bài tập C. Hiển thị tam giác trên của một ma trận nhất địnhCập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21. 51. 27 (UTC/GMT +8 giờ)

Mảng C. Bài tập-27 có lời giải

Viết chương trình in hoặc hiển thị ma trận tam giác trên bằng C

Trình bày bằng hình ảnh

Ma trận tam giác trên c++

Giải pháp mẫu

Mã C

#include 

void main()
 {
  int arr1[10][10],i,j,n;
  float determinant=0;
  
  
 printf("\n\nDisplay the upper triangular of a given matrix :\n");
       printf("----------------------------------------------\n");

     printf("Input the size of the square matrix : ");
     scanf("%d", &n); 
	 printf("Input elements in the first matrix :\n");
       for(i=0;i=j)
             printf("% 4d",arr1[i][j]);
           else
             printf("% 4d",0);
  }
       printf("\n\n");
}

Đầu ra mẫu

Display the upper triangular of a given matrix :                                                              
----------------------------------------------                                                                
Input the size of the square matrix : 3                                                                       
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [0],[2] : 3                                                                                         
element - [1],[0] : 4                                                                                         
element - [1],[1] : 5                                                                                         
element - [1],[2] : 6                                                                                         
element - [2],[0] : 7                                                                                         
element - [2],[1] : 8                                                                                         
element - [2],[2] : 9                                                                                         
The matrix is :                                                                                               
   1   2   3                                                                                                  
   4   5   6                                                                                                  
   7   8   9  
Setting zero in upper triangular matrix                                                                       
                                                                                                              
   1   0   0                                                                                                  
   4   5   0                                                                                                  
   7   8   9   

Sơ đồ

Ma trận tam giác trên c++

Trình chỉnh sửa mã lập trình C

Cải thiện giải pháp mẫu này và đăng mã của bạn qua Disqus

Trước. Viết chương trình bằng C để in hoặc hiển thị tam giác dưới của một ma trận đã cho.
Tiếp theo. Viết chương trình tính định thức của ma trận 3 x 3 bằng C.

Mức độ khó của bài tập này là gì?

Dễ dàng trung bình khó

Kiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource



Lập trình C. Lời khuyên trong ngày

Tại sao a+++++b không hoạt động?

printf("%d",a+++++b);

++ (postfix) không đánh giá một giá trị nhưng nó yêu cầu toán hạng của nó phải là một giá trị

Giới thiệu. https. //chút. ly/3CLKe2h


Ma trận tam giác trên c++

  • bài tập. Top 16 chủ đề phổ biến nhất hàng tuần
  • Bài tập SQL, Thực hành, Lời giải - THAM GIA
  • Bài tập, Thực hành, Giải pháp SQL - SUBQUERIES
  • JavaScript cơ bản - Bài tập, Thực hành, Lời giải
  • Mảng Java. Bài tập, Luyện tập, Lời giải
  • Bài Tập Lập Trình C, Thực Hành, Lời Giải. Tuyên bố có điều kiện
  • Cơ sở dữ liệu nhân sự - SORT FILTER. Bài tập, Luyện tập, Lời giải
  • Bài Tập Lập Trình C, Thực Hành, Lời Giải. Chuỗi
  • Kiểu dữ liệu Python. Từ điển - Bài tập, Thực hành, Lời giải
  • Câu đố lập trình Python - Bài tập, Thực hành, Giải pháp
  • Mảng C++. Bài tập, Luyện tập, Lời giải
  • Câu lệnh điều kiện và vòng lặp JavaScript - Bài tập, Thực hành, Lời giải
  • Thuật toán cơ bản C# Sharp. Bài tập, Luyện tập, Lời giải
  • Python Lambda - Bài tập, Thực hành, Giải pháp
  • Khung dữ liệu Python Pandas. Bài tập, Luyện tập, Lời giải
  • Công cụ chuyển đổi
  • JavaScript. Xác thực biểu mẫu HTML