How to write a quadratic equation solver using vectors

function [quadRoots,disc] = Q1_19041694(coeff)
coeff = (a b c);
disc = sqrt((b^2) - (4*a*c));
x1 = (disc - b)./(2*a);
x2 = (-b-disc)./(2*a);
if disc > 0
quadRoots = [x1,x2];
elseif disc == 0
quadRoots = x1;
else
quadRoots = (NaN)^4;
end

2 件のコメント

Abdulbasit Yekeen
Abdulbasit Yekeen 2020 年 1 月 20 日
Having errors in the code
Rocky Sharaf
Rocky Sharaf 2021 年 5 月 23 日
class Solution {
public:
vector
<int> quadraticRoots(int a, int b, int c) {
vector
<int> roots;
int root1 = 0, root2 = 0;
// value of b^2-4ac
int temp = (pow(b, 2) - 4 * a * c);
// if b^2-4ac is less then zero then roots are imaginary
if (temp < 0)
roots
.push_back(-1);
else {
// calculate root1 and root2 using fomula
// floor function returns greatest integer below ( -b + sqrt(temp) )
// sqrt function returns square root of temp
root1
= floor((-b + sqrt(temp)) / (2 * a));
root2
= floor((-b - sqrt(temp)) / (2 * a));
// store both roots calculated in vector
// max function returns greater value between root1 and root2
// min function returns smaller value between root1 and root2
roots
.push_back(max(root1, root2));
roots
.push_back(min(root1, root2));
}
return roots;
}
};

サインインしてコメントする。

回答 (1 件)

Sindar
Sindar 2020 年 1 月 20 日

0 投票

Search the help for one of the other students asking how to solve this exact same problem

カテゴリ

ヘルプ センター および File ExchangeSimulink Coder についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2020 年 1 月 20 日

コメント済み:

2021 年 5 月 23 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by