當前位置:首頁 » 編程語言 » c語言求向量

c語言求向量

發布時間: 2022-12-22 19:11:13

① 怎麼用c語言實現向量操作

//使用動態分配
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
inti,L;
char*p;
voidmain(){
for(i=0;i<20000;i++){
L=rand();
p=malloc(L);
if(NULL==p){
printf("mallocerror! ");
continue;
}
memset(p,0,L);
free(p);
}
}
//不使用動態分配
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#defineMAXLEN30000
inti,L;
charbuf[MAXLEN];
char*p;
voidmain(){
p=&buf[0];
for(i=0;i<20000;i++){
L=rand();
if(L>MAXLEN){
printf("L>MAXLEN==%d,ignorespilth. ",MAXLEN);
L=MAXLEN;
}
memset(p,0,L);
}
}

② C語言求向量夾角的程序部分看不懂 高手幫看看分析 紅字部分不懂

vectorProct 這個函數寫的有問題, 看函數的實現, length應該是指數組vecB和vecB中的數據個數, 但是在函數里明明已經寫了是3, 這個參數就是多餘的, 它只能等於3.

另外, 這個程序為了表示是3維向量, 定義了一個常數#define VECSIZE 3

但是在這里又直接寫3, 不一致.
這樣雖然不會出錯, 但是給理解造成困惑.

vectorNorm 也一樣.

double x[VECSIZE] = {0.0,1.0,0.0};

這個是這里作為示例, 求兩個向量的夾角的第一個向量的變數,
變數名是x, 而不是X軸, 而這個向量是y軸的,
這樣同樣是容易讓人困惑, 下面的y同理.

這樣, 整個程序的含義是:
函數 vectorProct求兩個向量的點積

函數 vectorNorm 求向量的模(長度)

main函數是示例:
定義了x和y兩個向量,
用vectorNorm分別求出兩個向量的模, xNorm, yNorm

用vectorProct求出兩個向量的叉積, 然後除以兩個向量的模, 得到夾角的餘弦值, cos_phi

用acos反餘弦求出兩個向量的夾角(單位弧度), 然後換算成角度為單位, cos_phi

③ 如何用C語言編程:求兩個空間向量的夾角呀可以用坐標表示~謝謝!

你先定義一個結構體,表示空間的向量,然後利用公式:
cosβ=(x1*x2+y1*y2+z1*z2)/sqrt((x1*x1+y1*y1+z1*z1)*(x2*x2+y2*y2+z2*z2))
然後β可以用反三角函數求得。

④ c語言 向量的運算

根據題意可得如下代碼:

#include<stdio.h>
intmain()
{
intn;
inta[1010],b[1010];
inti,ans=0;
scanf("%d",&n);
for(i=0;i<n;++i){
scanf("%d",&a[i]);
}
for(i=0;i<n;++i){
scanf("%d",&b[i]);
}
for(i=0;i<n;++i){
ans+=a[i]*b[i];
}
printf("%d ",ans);
return0;
}

⑤ c語言求向量的內積

看沒人幫你做,我幫你寫吧

#include<stdio.h>

intmain()
{
intn,i;
doublea[105],b[105],sum=0;
printf("輸入向量的維數:");
scanf("%d",&n);
printf("輸入A向量:");
for(i=0;i<n;i++)scanf("%lf",&a[i]);
printf("輸入b向量:");
for(i=0;i<n;i++)scanf("%lf",&b[i]);
for(i=0;i<n;i++)sum+=a[i]*b[i];
printf("A·B=%lf ",sum);
return0;
}

⑥ c語言.求向量旋轉後的向量的問題

錯誤原因:
輸入角度的時候碰到麻煩了,重新定義了一個int類型的變數用來保存角度,結果代碼里忘了換變數名稱了。

修正後的代碼:

#include<stdio.h>
#include<math.h>
double p=3.1415926;

int main()
{
double x,y,x1,x2;
double r2,r1,d;
int angle;
char flag = 'Y';

while(flag == 'Y' || flag == 'y')
{
printf("Please input the new xCoord, yCoord, Angle:\n");
scanf_s("%lf %lf %d", &x, &y, &angle);

r2=(p/180)*angle;
r1=atan(y/x)+r2;
d=sqrt(x*x+y*y);
x1=d*cos(r1);
x2=d*sin(r1);
printf("%.4lf %.4lf\n",x1,x2);

printf("Do you want to continue ? (Y/N)\n");
fflush(stdin);
flag = getchar();
}

return 0;
}

#include<stdio.h>
#include<math.h>
double p=3.1415926;

int main()
{
double x,y,x1,x2;
double r2,r1,d;
int angle;
char flag = 'Y';

while(flag == 'Y' || flag == 'y')
{
printf("Please input the new xCoord, yCoord, Angle:\n");
scanf_s("%lf %lf %d", &x, &y, &angle);

r2=(p/180)*angle;
r1=atan(y/x)+r2;
d=sqrt(x*x+y*y);
x1=d*cos(r1);
x2=d*sin(r1);
printf("%.4lf %.4lf\n",x1,x2);

printf("Do you want to continue ? (Y/N)\n");
fflush(stdin);
flag = getchar();
}

return 0;
}

測試結果:

⑦ c語言要求結構體儲存空間向量求向量的模,怎麼實現輸入任意向量都完成運算

已知一個平面的兩個法向量a=(x1,y1,z1),b=(x2,y2,z2) 其中x1,x2,y1,y2,z1,z2均為已知
設平面法向量為n=(x,y,z)
n為平面的法向量則
n*a=0 x*x1+y*y1+z*z1=0
n*b=0 x*x2+y*y2+z*z2=0
兩個方程,三個未知數x,y,z
故設出其中一個,例如設x=1(不能為0),從而求出y,z的值,即可得到平面的一個法向量,因為平面的法向量有無數個,且模可以任意,故可以這樣假設

⑧ 求c語言向量的問題

這樣即可!

⑨ C語言求向量的矢量積、模、單位向量、還有判斷2個向量是否共線,在線等答案,醬油黨麻煩讓讓

//很簡單,你對照一下吧。。。#include <stdio.h>
#include <math.h>
void main()
{
int i,sum=0,p[3]={1,2,3},p1[3]={2,3,4},flag=1;
double model=0,model1=0,temp;
for(i=0;i<3;i++)
{sum+=p[i]*p1[i];
model+=p[i]*p[i];
model1+=p1[i]*p1[i];
}
model=sqrt(model);
model1=sqrt(model1);
printf("向量p,p1的積: %d\n",sum);
printf("p,p1的模為:%lf %lf\n",model,model1);
temp=p[0]/p1[0];
if((p[1]*1.0/p1[1]-temp>=1e-3) &&(p[2]*1.0/p1[2]-temp>=1e-3))
flag=0;
if(flag)
printf("兩向量共線!");
else
printf("不共線");

}

⑩ C語言用一維數組求兩個向量之和,結果不對呀,求指教

#include<stdio.h>
int main()
{ double a[3];
double b[3];
for(int i=0;i<3;i++)
{
scanf("%lf",&a[i]);
}
for( i=0;i<3;i++)
{
scanf("%lf",&b[i]);
}
double s[3];

for( i=0;i<3;i++)
{
s[i]=a[i]+b[i];
printf("%lf\n",s[i]);

}
return 0;

}
你的s定義為三個元素,最後一個元素為s[2]而不是s[3] ,我的回答中把你所有的輸出都換行給顯示了

熱點內容
美嘉演算法口訣 發布:2025-05-16 06:03:15 瀏覽:951
c程序編譯連接 發布:2025-05-16 06:02:36 瀏覽:963
腳本魔獸 發布:2025-05-16 06:01:52 瀏覽:329
文件夾python 發布:2025-05-16 06:01:43 瀏覽:626
電腦我的世界伺服器游戲幣 發布:2025-05-16 05:27:25 瀏覽:487
索尼手機為什麼不能用安卓10 發布:2025-05-16 05:18:46 瀏覽:784
蔚來es6選擇哪些配置實用 發布:2025-05-16 05:18:05 瀏覽:130
小米如何掃碼wifi密碼 發布:2025-05-16 05:13:38 瀏覽:807
樓層密碼是什麼意思 發布:2025-05-16 05:13:37 瀏覽:13
創建文件夾失敗 發布:2025-05-16 05:12:59 瀏覽:396