導航:首頁 > 創造發明 > 用c創造矩陣類

用c創造矩陣類

發布時間:2021-10-08 13:40:12

❶ C語言中如何定義矩陣

兩種方式可以參考:
1、最簡單的就是二維數組,比如存儲全是整形的一個m*n的矩陣。然後可以定義int a[m][n]。
輸入或者輸出可以用兩層循環來完成,外層控制行m比如for(i=0;i<m;++i),內層控制列n比如for(j=0;j<n;++j);
2、第二種方式就是壓縮矩陣進行存儲,如果學了數據結構應該比較好理解。
結構體進行封裝,比如:
第一步:先定義一個有效數據的位置
typedef struct node
{
int hang;int lie;int data;//用來存儲一個有效數據位的行列和值
}node;
typedef struct matrix
{
node *m;//一個數組,用來存儲所有的node數據
int sum;//記錄一共有多少個有效數據位
}matrix;

❷ 怎樣用C語言寫矩陣

用二維數組,如下:
#include <stdio.h>
main()
{
int i, j, a[4][4];

for ( i = 1; i < 4; i++ )
{
for ( j = 1; j < 4; j++ )
{
scanf ("%d", &a[i][j]);
}
}
//這樣就可以將一個3*3 的矩陣存在2維數組中了

for ( i = 1; i < 4; i++ )
{
for ( j = 1; j < 4; j++ )
{
printf (" %-4d ", a[i][j]);
}
printf ("\n");
}
//這樣就可以顯示矩陣
return 0;
}

❸ 如何用C語言定義矩陣

矩陣其實就是二維數組,在進行編碼的時候,矩陣就會被定義成為二維數組

❹ 用C語言輸出矩陣

//螺旋賦值數組並輸出的問題
#include<stdio.h>
#defineM100
#defineN100
intmain()
{
inta[M][N];
intm=3,n=4;//這里設置數組的維度
inti,j;
intt;
intup=0,down=m-1;
intleft=0,right=n-1;
i=up,j=left;
t=1;
while(1)
{
while(j!=right+1)
{
a[i][j]=t;
t++;
j++;
}
j--;
if((t-1)==(m*n))
{
break;
}
up++;
i=up;
while(i!=down+1)
{
a[i][j]=t;
t++;
i++;
}
if((t-1)==(m*n))
{
break;
}
i--;
right--;
j=right;
while(j!=left-1)
{
a[i][j]=t;
t++;
j--;
}
if((t-1)==(m*n))
{
break;
}
j++;
down--;
i=down;
while(i!=up-1)
{
a[i][j]=t;
t++;
i--;
}
if((t-1)==(m*n))
{
break;
}
i++;
left++;
j=left;
}
printf("---------螺旋形輸出-------- ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d",a[i][j]);
}
printf(" ");
}
printf("--------------------------- ");
return0;
}

哥們你很摳門兒呀,才5分!!

❺ 怎樣用C語言定義一個矩陣

用二維數組

❻ 矩陣類的設計,用C++,要求:定義矩陣類,包含行、列和矩陣數據元素;

我沒有重載函數,只重載了運算符,你看看行不行。

這是頭文件:

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using std::cout;
using std::endl;

class Matrix{
public:
Matrix():row(0),column(0),m(0){}
Matrix(const Matrix& ma);
~Matrix();
void setMatrix(int r,int c,double **ma);
int getRow(){return row;}
int getColumn(){return column;}
void display();
Matrix& operator=(const Matrix& ma);
Matrix operator+(const Matrix& ma)const;
Matrix operator-(const Matrix& ma)const;
Matrix operator*(const Matrix& ma)const;
private:
int row;
int column;
double** m;
};

Matrix::Matrix(const Matrix &ma){
this->row=ma.row;
this->column=ma.column;
m=new double *[row];
for(int i=0;i<row;i++)
m[i]=new double[column];
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
m[i][j]=ma.m[i][j];
}

Matrix::~Matrix(){
for(int i=0;i<this->row;i++)
delete[] m[i];
delete[] m;
}

void Matrix::setMatrix(int r,int c,double** ma){
for(int i=0;i<this->row;i++)
delete[] m[i];
delete[] m;
this->row=r;
this->column=c;
m=new double *[row];
for(int i=0;i<row;i++)
m[i]=new double[column];
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
m[i][j]=ma[i][j];
}

void Matrix::display(){
if(m==0) cout<<"Matrix has not been initialised.\n";
else{
for(int i=0;i<row;i++){
for(int j=0;j<column;j++)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
}

Matrix& Matrix::operator =(const Matrix &ma){
if(m!=0){
for(int i=0;i<this->row;i++)
delete[] m[i];
delete[] m;
}
this->row=ma.row;
this->column=ma.column;
m=new double *[row];
for(int i=0;i<row;i++)
m[i]=new double[column];
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
m[i][j]=ma.m[i][j];
return *this;
}

Matrix Matrix::operator +(const Matrix &ma) const{
double** result=new double *[this->row];
for(int i=0;i<this->row;i++)
result[i]=new double[this->column];
for(int i=0;i<this->row;i++)
for(int j=0;j<this->column;j++)
result[i][j]=this->m[i][j]+ma.m[i][j];
Matrix temp;
temp.setMatrix(this->row,this->column,result);
for(int i=0;i<this->row;i++)
delete[] result[i];
delete[] result;
return temp;
}

Matrix Matrix::operator -(const Matrix &ma) const{
double** result=new double *[this->row];
for(int i=0;i<this->row;i++)
result[i]=new double[this->column];
for(int i=0;i<this->row;i++)
for(int j=0;j<this->column;j++)
result[i][j]=this->m[i][j]-ma.m[i][j];
Matrix temp;
temp.setMatrix(this->row,this->column,result);
for(int i=0;i<this->row;i++)
delete[] result[i];
delete[] result;
return temp;
}

Matrix Matrix::operator *(const Matrix &ma) const{
double** result=new double *[this->row];
for(int i=0;i<this->row;i++)
result[i]=new double[this->column];
double entry;
for(int i=0;i<this->row;i++)
for(int j=0;j<ma.column;j++){
entry=0;
for(int k=0;k<this->column;k++)
entry=entry+(this->m[i][k])*(ma.m[k][j]);
result[i][j]=entry;
}
Matrix temp;
temp.setMatrix(this->row,ma.column,result);
for(int i=0;i<this->row;i++)
delete[] result[i];
delete[] result;
return temp;
}

#endif

這是主程序:

#include <iostream>
#include <cstring>
using std::cout;
using std::cin;
using std::endl;
#include "Matrix.h"

int main(){
int i;
Matrix op1;
Matrix op2;
Matrix result;
do{
cout<<"1.Enter operand 1 2.Enter operand 2 3.Add 4.Subtract 5.Multiply 6.Display 7.Quit"<<endl;
cout<<"Enter your choice: ";
cin>>i;
if(i==1){
int r,c;
cout<<"Enter the number of rows for operand 1: ";
cin>>r;
cout<<"Enter the number of columns for operand 1: ";
cin>>c;
double** m=new double *[r];
for(int j=0;j<r;j++)
m[j]=new double[c];
cout<<"Enter the elements of operand 1, from left to right and from top to bottom:"<<endl;
for(int j=0;j<r;j++)
for(int k=0;k<c;k++)
cin>>m[j][k];
op1.setMatrix(r,c,m);
for(int j=0;j<r;j++)
delete[] m[j];
delete m;
}
else if(i==2){
int r,c;
cout<<"Enter the number of rows for operand 2: ";
cin>>r;
cout<<"Enter the number of columns for operand 2: ";
cin>>c;
double** m=new double *[r];
for(int j=0;j<r;j++)
m[j]=new double[c];
cout<<"Enter the elements of operand 2, from left to right and from top to bottom:"<<endl;
for(int j=0;j<r;j++)
for(int k=0;k<c;k++)
cin>>m[j][k];
op2.setMatrix(r,c,m);
for(int j=0;j<r;j++)
delete[] m[j];
delete m;
}
else if(i==3){
if(op1.getRow()>0&&op1.getColumn()>0&&op2.getRow()>0&&op2.getColumn()>0&&op1.getRow()==op2.getRow()&&op1.getColumn()==op2.getColumn()){
result=op1+op2;
cout<<endl<<"The result of op1 plus op2 is:"<<endl;
result.display();
}
else cout<<"The dimensions of the two operands do not match. Unable to do the addition."<<endl;
}
else if(i==4){
if(op1.getRow()>0&&op1.getColumn()>0&&op2.getRow()>0&&op2.getColumn()>0&&op1.getRow()==op2.getRow()&&op1.getColumn()==op2.getColumn()){
result=op1-op2;
cout<<endl<<"The result of op1 minus op2 is:"<<endl;
result.display();
}
else cout<<"The dimensions of the two operands do not match. Unable to do the subtraction."<<endl;
}
else if(i==5){
if(op1.getRow()>0&&op1.getColumn()>0&&op2.getRow()>0&&op2.getColumn()>0&&op1.getColumn()==op2.getRow()){
result=op1*op2;
cout<<endl<<"The result of op1 times op2 is:"<<endl;
result.display();
}
else cout<<"The dimensions of the two operands do not match. Unable to do the multiplication."<<endl;
}
else if(i==6){
int j;
cout<<"Which matrices do you want to display?"<<endl;
cout<<"1.Operand 1 2.Operand 2 3.Result"<<endl;
cin>>j;
if(j==1) op1.display();
if(j==2) op2.display();
if(j==3) result.display();
}
else if(i==7) break;
}while(1);
return 0;
}

❼ 用c 寫矩陣

寫的不是很好。。時間不多,你自己可以改一下有結果在後面.

#include<stdio.h>

#defineN30

#defineM30

voidmain()

{inta[N][M];

inti,j,n,m,sum[6]={0,0,0,0,0,0};

printf(" 輸入行數n:");

scanf("%d",&n);

printf(" 輸入列數m:");

scanf("%d",&m);

for(i=0;i<n;i++)

{

printf(" 第%d行 ",i+1);

for(j=0;j<m;j++)

scanf("%d",&a[i][j]);

printf(" ");

}

for(i=0;i<6;i++)

{

for(j=0;j<6;j++)

{printf("%3d",a[i][j]);

sum[i]+=a[i][j];

}

printf(" ");

}

for(i=0;i<6;i++)

printf("第%d行數只和=%4d ",i,sum[i]);

}

❽ C語言如何生成一個隨機矩陣

#include <stdio.h>                      
#include <stdlib.h>

#define M 10
#define N 10

int main(void)
{
int i = 0, j = 0;
int Arr[M][N] = {{0}};

srand(time(NULL));

for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
Arr[i][j] = rand() % 1000;
}
}

printf("Array[%d][%d] is: ", M, N);
for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
printf("%d ", Arr[i][j]);
}
printf(" ");
}
return 0;
}
Array[10][10] is:
265 610 286 703 424 389 529 686 495 6
497 111 347 957 680 980 471 596 736 728
250 457 661 348 776 795 619 701 39 149
79 304 759 366 359 184 755 240 222 602
247 719 714 594 28 746 926 499 343 14
580 593 472 241 293 600 36 913 654 428
414 733 732 174 451 444 710 558 684 932
161 931 651 227 877 680 973 804 531 316
818 463 262 642 705 907 595 93 172 249
873 587 334 958 761 786 402 471 696 86

❾ C語言 簡單的矩陣生成

i=i+1;//這里你i加了1造成下面的for循環不成立
a[i][j]=count;
count++;
for (;i==0;)上面你給i加了1,使得i不等於0,for語句循環條件也就不成立。所以不進循環。
下面另一個循環不進也是相同原因。

❿ 用C語言編寫如下矩陣

我改了可以運行了
加了兩個變數 s,c來控制兩個數組下標
那麼for語句只需要輸出的次數就好了

你的 主要是for語句沒起做用
{for(n=1;n<=5;n++)
printf("%5d",a[i]);
就會輸出5次相同a[i])

#include<stdio.h>
void main()
{int a[15],b[10];
int l,n,i,j,k;
for(i=0;i<=14;i++)
a[i]=41+i;
for(j=0;j<=9;j++)
b[j]=35-j;
int s=0,c=0;
for(l=1;l<=5;l++)
{
for(n=0;n<l;n++)
printf("%5d ",a[s++]);
for(int x=0; x<5-l;x++)
printf("%5d ",b[c++]);

printf("\n");

}
}

閱讀全文

與用c創造矩陣類相關的資料

熱點內容
湖北省醫療糾紛預防與處理辦法 瀏覽:230
星光創造營後勤在哪 瀏覽:581
北京辦理知識產權 瀏覽:177
交通銀行信用卡有效期是幾年 瀏覽:913
公司協議股權轉讓 瀏覽:531
啥叫擔保物權 瀏覽:60
馬鞍山到徐州的火車 瀏覽:703
羊年限定金克絲多少錢 瀏覽:573
公共基本衛生服務結核項目試題 瀏覽:896
寶雞市工商局電話號碼 瀏覽:81
基本公共衛生服務督導工作方案 瀏覽:454
信息化成果總結 瀏覽:948
債務糾紛律師費必須提供發票嗎 瀏覽:876
手機我的世界創造模式怎麼去天堂 瀏覽:716
專利代理人個人總結 瀏覽:312
工商局黨建工作述職報告 瀏覽:685
創造力閱讀理解答案 瀏覽:866
金華質監局和工商局合並 瀏覽:334
衛生院公共衛生服務考核結果 瀏覽:693
專利權的內容有哪幾項 瀏覽:750