Triangles and Cubic Polynomials? - c++

Given 2 cubic equations of the form (A∗X^3+B∗X^2+C∗X)
find out how many obtuse triangles could be formed from the roots of the 2 equations.
Note that only positive roots could be a triangle side, so any triangle should be formed of 333 positive roots (sides).
Input Constraints:
1≤T≤10^6
(−4∗10^8)≤A1,B1,C1,A2,B2,C2≤(4∗10^8)
Format of the input file:
First line : T i.e number of testcases
For each testcase :
First line : Three space separated integers A1, B1 and C1
Second line : Three space separated integers A2, B2 and C2.
images for test case2
My code is:
#include <iostream>
#include <math.h>
using namespace std;
int* roots()
{
long int a,b,c;
int *x=new int[2];
int determinant;
cin >> a >> b >> c;
determinant = b*b - 4*a*c;
if (determinant > 0) {
x[1] = (-b + sqrt(determinant)) / (2*a);
x[0] = (-b - sqrt(determinant)) / (2*a);
}
else {
x[1] = -b/(2*a);
x[0]=x[1];
}
return x;
}
int main()
{int t;
int *a,*b,x,y,z,c,i;
cin>>t;
do{c=0;
a=roots();
b=roots();
for(i=0;i<2;i++)
{x=a[i]*a[i];
y=b[i]*b[i];
if(x!=0&&y!=0)
{switch(i)
{
case 0:z=a[1]*a[1];
if(((x+y)<z||y+z<x||x+z<y)&&z!=0)
c++;
z=b[1]*b[1];
if(((x+y)<z||y+z<x||x+z<y)&&z!=0)
c++;
break;
case 1:z=a[0]*a[0];
if(((x+y)<z||y+z<x||x+z<y)&&z!=0)
c++;
z=b[0]*b[0];
if(((x+y)<z||y+z<x||x+z<y)&&z!=0)
c++;
break;
}
}
}
cout<<c<<"\n";
t--;
}while(t);
return 0;
}

Related

Check if the system of equations have a solution

Question:
There are N variables x1, x2, x3, ...... xN.
There are M triplets such that triple {i,j,c} indicate that xi = xj + c and so each triplet denote an equation. Check if the system of equations have atleast one solution.
My idea:
I considered the system of equations as a graph of N nodes labelled from 1 to N. If {i,j,c} is a triplet then, i->j is an edge with weight c and j->i is an edge with weight -c. With 0 as the source, I applied Bellman Ford algorithm to detect any negative cycles. If there is any positive sum cycle, then there must also be a negative sum cycle, which can be traced with Bellman Ford.
Following is the code
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define INF (1<<30)
int main(){
int T;
cin>>T;
while(T--){
int N;int M;
cin>>N;cin>>M;
vector<vector<int>> edges;
for (int i=0;i<M;++i){
int first;int second;int third;
cin>>first;cin>>second;cin>>third;
first--;second--;
edges.push_back({first,second,third});
edges.push_back({second,first,-third});
}
vector<int> distances(N,INF);
distances[0] = 0;
for (int i=1;i<=N;++i){
for (int j=0;j<edges.size();++j){
int from = edges[j][0];
int to = edges[j][1];
int weight = edges[j][2];
if (distances[from]+weight<distances[to]){
distances[to] = distances[from] + weight;
}
}
}
int flag = 0;
for (int j=0;j<edges.size();++j){
int from = edges[j][0];
int to = edges[j][1];
int weight = edges[j][2];
if (distances[from]+weight<distances[to]){
flag = 1;
break;
}
}
if (flag==1){
cout << "NO" << endl;
}
else{
cout << "YES" << endl;
}
}
}
Is the idea correct? I am unable to generate test cases against the above logic. But, the code failed to give correct output for many test cases.
Can somebody point out the mistake.
Thank you

Not receiving output even after writing printf("...")

I've written a code to find the reciprocal of a number without using division but rather approximating it using bisection method. Got two doubts. One, what should I keep the lower limit and the upper limit for x? Also is there a faster method to converge from the limits to the required value(reciprocal of the input) rather than just averaging it? And the main doubt, when I try to run it, It just stops after receiving the input number from the user. Any hints so solve that?
Here is the code:
#include<stdio.h>
#include<cstdlib>
#define epsilon 0.0001
float f(float x, float &m)
{
if(m==0)
{
printf("Reciprocal not defined");
return 0.0;
}
else
{
return x+1/m;
}
}
int main(void)
{
float m,g1,x,g2,c;
printf("Enter a number:\n");
scanf("%f",f(x,m));
g1=epsilon;
g2=m;
while(abs(f(g1,m)-f(g2,m))>epsilon)
{
c=(g1+g2)/2;
if(f(g1,m)*f(c,m)<0)
{
g2=c;
}
else if(f(c,m)*f(g2,m)<0)
{
g1=c;
}
}
printf("The reciprocal is approximately %f",c);
}
The code is expected to work as follows:
Enter a number: 5
The reciprocal is approximately 0.2
Enter a number: 0
Reciprocal is not defined
Instead of this, it shows:
Enter a number:
Reciprocal is not defined
(without accepting any input)
Your overall code is far too convoluted and your usage of scanf doesn't make sens.
You probbaly want something like this:
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#define epsilon 0.0001f
int main(void)
{
float m, g1, g2, c, diff;
printf("Enter a number:\n");
scanf("%f", &m);
g1 = epsilon;
g2 = m;
do
{
c = (g1 + g2) / 2;
diff = c * m - 1.0f;
if (diff > 0)
g2 = c;
else
g1 = c;
} while (fabs(diff) > epsilon);
printf("The reciprocal is approximately %f", c);
}
For your question "Also is there a faster method to converge from the limits to the required value (reciprocal of the input) rather than just averaging it?", I have no idea, but searching by bisection is generally rather fast.
Test program that tests for a range of values:
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#define epsilon 0.0001f
int main(void)
{
float g1, g2, c, diff;
for (float m = 1; m < 20; m += 1)
{
g1 = epsilon;
g2 = m;
do
{
c = (g1 + g2) / 2;
diff = c * m - 1.0f;
if (diff > 0)
g2 = c;
else
g1 = c;
} while (fabs(diff) > epsilon);
printf("The reciprocal of %f is approximately %f vs. %f, diff %f\n", m, c, 1/m, c-1/m);
}
}
Live demonstration

How to compute sum, average and product of three numbers using different functions

I need to create a program that accepts 3 numbers and find the sum, average and product. I only need to use main(), get_ABC(), compute() and display() functions. I did it right but im not getting the right output about my mathematical operations.
#include<conio.h>
#include<iostream.h>
float get_A(float A)
{
cout<<"Enter First Number: ";
cin>>A;
return(A);
}
float get_B(float B)
{
cout<<"Enter Second Number: ";
cin>>B;
return(B);
}
float get_C(float C)
{
cout<<"Enter Third Number: ";
cin>>C;
return(C);
}
float compute_sum(float A,float B,float C)
{
float sum;
sum = A + B + C;
return(sum);
}
float compute_ave(float A,float B,float C)
{
float ave;
ave = (A + B + C) / 3;
return (ave);
}
float compute_prod(float A,float B,float C)
{
float prod;
prod = A * B * C;
return(prod);
}
void display(float sum,float ave,float prod)
{
cout<<"The sum of three numbers is "<<sum<<".\n";
cout<<"The average of three numbers is "<<ave<<".\n";
cout<<"The product of three numbers is "<<prod<<".";
}
float main()
{
float A,B,C;
float sum;
float ave;
float pro;
clrscr();
get_A(A);
get_B(B);
get_C(C);
sum = compute_sum(A,B,C);
ave = compute_ave(A,B,C);
pro = compute_prod(A,B,C);
display(sum,ave,pro);
getch();
return(0);
}
This is the output.
Enter First Number: 1
Enter Second Number: 2
Enter Third Number: 3
The sum of three numbers is 0.
The average of three numbers is 0.
The product of three numbers is 0.
I really need help. My prof give me this problem without teaching how to code, so i only come up with basics, i really gave up and end up here. You can change, add or replace the codes(with basic codes) if you want and i'll appreciate it.
Change this:
get_A(A);
get_B(B);
get_C(C);
to this:
A = get_A(A);
B = get_B(B);
C = get_C(C);
so that you use the return values of your functions.
Moreover, main() should return an int, not a float.
Furthermore, initialize your variables when you declare them, so that you avoid "is used uninitialized in this function" warnings.

total Number of rectangles in a grid?

Here i want to count the total numbers of rectangles in M*N grid. This code is working properly but not for the large input like M=100000 N=100000. It shows something like -nan or any negative integer.
Thee result will be the modulo of 1000000007. How can i get an accurate answer with this large integer range?
Thank you
#include<iostream>
#include<cmath>
using namespace std;
double factorial(int);
int main(){
int m,n;
double mod= 1000000007;
double p,q,result;
cout << "\nPlease enter the dimensions of grid: ";
cin>>m>>n;
if (m <0&&n<0)
{
cout << "Invalid dimensions!\n";
return 1;
}
m++; n++; /*if n is the no of vertical/horizontal boxes,
there will be n+1 vertical/horizontal lines in the grid */
result=(factorial(m)/(2*factorial(m-2)))*(factorial(n)/(2*factorial(n-2)));
cout<<"\nThe rectangles in the grid is:"<<fmod(result,mod);
return 0;
}
double factorial(int x) {
double temp;
if(x <= 1) return 1;
temp = x * factorial(x - 1);
return temp;
}
You can't do 100000 factorial in a double-precision number, you'll get overflow (as you've observed).
In your case think about this expansion of what you're calculating
m * (m-1) * (m-2) * (m-3) * ... * 2 * 1
-----------------------------------------
2 * (m-2) * (m-3) * ... * 2 * 1
This all simplifies to m * (m-1) / 2. So, you don't need your factorial function at all.
Edit: The code in another post was not right. Try this:
result = static_cast<double>(m) * (m-1) * n * (n-1) / 4;
Not really programming related, but factorial(m)/(2*factorial(m-2) is the same as m * (m-1) / 2 which will probably not cause an overflow.
#include<iostream>
#include<cmath>
using namespace std;
double factorial(int);
int main(){
int m,n;
double mod= 1000000007;
double p,q,result;
cout << "\nPlease enter the dimensions of grid: ";
cin>>m>>n;
if (m <0&&n<0)
{
cout << "Invalid dimensions!\n";
return 1;
}
m++; n++; /*if n is the no of vertical/horizontal boxes,
there will be n+1 vertical/horizontal lines in the grid */
//This is where I had the typo. Forgot to divide by 2.
result=((m*(m-1))/2)*((n*(n-1))/2);
cout<<"\nThe rectangles in the grid is:"<<fmod(result,mod);
return 0;
}
/*double factorial(int x) {
double temp;
if(x <= 1) return 1;
temp = x * factorial(x - 1);
return temp;
} */
This should work fine. There was no need for you to implement a factorial function. You can have just simplified your statement down and you would have gotten your answer.

Keep getting 1.#INF as my output

#include <iostream>
using namespace std;
double calc(int a, int b);
int main()
{
int n1, n2;
cout << "Enter a number for a: ";
cin >> n1;
cout << "Enter a number for b: ";
cin >> n2;
cout << calc(n1, n2) << endl;
system("PAUSE");
return 0;
}
double calc(int a, int b)
{
double s;
s = (a) / ((sqrt(a / b)));
return s;
}
This program is meant to check whether the two integers are greater than zero. If it is it will calcualte the formula. Otherwise if one of the integers is zero or less than zero it will not return anything and exit the program.
My question here is that no matter what I input for a and b, i keep getting 1.#INF as the output and I have no idea why. I've checked the formula in a seperate program and it worked fine.
Any ideas?
Here, you are operating with int numbers:
s = (a) / ((sqrt(a / b)));
If a is less then b, then a/b (both are integers, remember, so the fractional part of the result will simply be lost) will be equal to 0, which leads to division by 0. You need to cast one of the numbers to double:
s = (a) / ((sqrt(static_cast<double>(a) / b)));
sqrt takes and returns a double. When you call it with integer arguments it will be converted in a double, and will thus get the value of infinity.
change your function signature to:
double calc(double a, double b);
and declare n1 and n2 as double.
You say that the function will exit the program when one of the integers are 0 or less, but where?
Try to check them like this:
Additionally, you should have a check whether a is greater than b
double calc(int a, int b)
{
double s;
if(a <= 0) exit(-1);
if(b <= 0) exit(-1);
if(a < b) exit(-1);
s = (a) / ((sqrt(a / b)));
return s;
}
You are having problems with infinity. For it use isinf. Here is some sample usage:
#include <stdio.h> /* printf */
#include <math.h> /* isinf, sqrt */
int main()
{
printf ("isinf(-1.0/0.0) : %d\n",isinf(-1.0/0.0));
printf ("isinf(sqrt(-1.0)): %d\n",isinf(sqrt(-1.0)));
return 0;
}
output:
isinf(-1.0/0.0) : 1 isinf(sqrt(-1.0): 0