The program asks the user for a numerator and denominator in the enter function, then it needs to simplify and then display it.
I tried running it and my program broke.
Any tips on how to do this?
I am still trying to learn how to do structures.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct Fraction
{
int numerator;
int denominator;
};
void enter(struct Fraction *choice)
{
printf("Numerator: \n");
scanf("%d", choice->numerator);
printf("Denominator: \n");
scanf("%d", choice->denominator);
}
void simplify(struct Fraction *reduce)
{
reduce->numerator = reduce->numerator / reduce->numerator;
reduce->denominator = reduce->denominator / reduce->denominator;
}
void display(const struct Fraction *show)
{
printf("%d / %d", show->numerator, show->denominator);
}
int main(void)
{
struct Fraction f;
printf("Fraction Simplifier\n");
printf("===================\n");
enter(&f);
simplify(&f);
display(&f);
}
Problem 1
The lines
scanf("%d", choice->numerator);
scanf("%d", choice->denominator);
need to be:
scanf("%d", &choice->numerator);
scanf("%d", &choice->denominator);
// ^^ Missing
Problem 2
The following lines:
reduce->numerator = reduce->numerator / reduce->numerator;
reduce->denominator = reduce->denominator / reduce->denominator;
are equivalent to:
reduce->numerator = 1.0;
reduce->denominator = 1.0;
You need code to compute the GCD of the numerator and denominator and then use:
double gcd = get_gcd(reduce->numerator, reduce->denominator);
reduce->numerator = reduce->numerator/gcd;
reduce->denominator = reduce->denominator/gcd;
Related
I need help solving this task, if anyone had a similar problem it would help me a lot.
The task is:
Write a program that calculates the degree and polynomial p(x) for a given x.
For example:
Enter n:2 //degree of polynomial and function degree
Enter x:2
x^n=4
Enter coefficients of polynomial:
a[0]=1
a[1]=2
a[2]=3
P(x)=3*x^2 + 2*x^1 +1*x^0 = 17
I did it like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
/*
*/
typedef struct polynomial {
double coef[MAX];
} POLYNOMIAL;
double degree(double ,int );
double px(POLYNOMIAL ,double );
int main()
{
POLYNOMIAL p;
double x,pom;
int n;
printf("Enter degree (n>=0):");
scanf("%d",&n);
while(n<1 || n>MAX)
{
printf("Enter degree (n>=0):");
scanf("%d",&n);
}
printf("Enter x:");
scanf("%lf",&x);
pom=degree(x,n);
printf("%.2lf^%d =%lf",x,n,pom);
printf("\nEnter coefficients of polynomial :\n");
for(int i=0;i<=n;i++)
{
printf("a[%d]:",i);
scanf("%lf",&p.coef[i]);
}
return 0;
}
double degree(double x,int n)
{
double degree=1;
if(n==0)
{
return 1;
}
for(int i=1;i<=n;i++)
{
degree*=x;
}
return degree;
}
double px(POLYNOMIAL p,double x)
{
double sum=0;
for(int j=0;j<"I don't know what to put here";j++)
{
sum+=(double)p.coef[j]*degree(x,j);
}
printf("%lf",sum);
}
The problem arises when calculating polynomials, because I don't know what to put as a condition in the for loop, there should be j < of the length of the array entered, that is, of degree n,
but n cannot be used as a parameter in the px function? The task must be done with the structure and functions listed.
Thanks in advance !
If you are not allowed to pass n to the function, you can instead just loop to MAX and make sure that all unused coefficients are zero.
In other words, just initialize all elements of p to zero
POLYNOMIAL p = {.coef = {0} };
and let the loop be:
j < MAX
BTW: Notice that you need return sum in the function.
Further the function degree is pretty unnecessary. Consider this:
double px(POLYNOMIAL p,double x)
{
double sum=p.coef[0];
double d = x;
for(int j=1;j<MAX;j++)
{
sum+=(double)p.coef[j]*d;
d = d * x;
}
printf("%lf",sum);
return sum;
}
This is what I need to do:
Define a structure called “fraction” with integer members “numerator” and
“denominator.” Prompt the user to enter a fraction in the form “#/#”. Call a function called
“simplFrac” that simplifies a fraction by dividing the numerator and denominator by the greatest
common factor and returns the simplified fraction to the calling function. Print the results from
main().
And here is my code:
#include <stdio.h>
struct fraction {
int numerator;
int denominator;
};
struct fraction simplFrac(struct fraction x);
int main(void) {
struct fraction a;
printf("Enter a fraction in the form #/#: ");
scanf("%i/%i",&a.numerator,&a.denominator);
printf("\n");
printf("The simplified fraction is: %i/%i\n",);
return 0;
}
struct fraction simplFrac(struct fraction x) {
int gcf,remainder;
while (x.numerator != 0)
{
remainder = x.denominator % x.numerator;
x.denominator = x.numerator;
x.numerator = remainder;
}
gcf = x.denominator;
x.numerator = x.numerator / gcf;
x.denominator = x.denominator / gcf;
return x;
}
Would you please try the following:
#include <stdio.h>
struct fraction
{
int numerator;
int denominator;
};
struct fraction simplFrac(struct fraction x);
int gcf(int, int);
int main(void)
{
struct fraction a;
char buf[BUFSIZ];
printf("Enter a fraction in the form #/#: ");
fgets(buf, BUFSIZ, stdin);
sscanf(buf, "%i/%i", &a.numerator, &a.denominator);
a = simplFrac(a);
printf("The simplified fraction is: %i/%i\n", a.numerator, a.denominator);
return 0;
}
struct fraction simplFrac(struct fraction x)
{
int factor = gcf(x.denominator, x.numerator);
x.numerator /= factor;
x.denominator /= factor;
return x;
}
int gcf(int a, int b)
{
if (b == 0) return a;
else return gcf(b, a % b);
}
The algorythm to calculate the gcf is separated as a function gcf().
I am very new to C programming. Here, I have written a very simple C program to evaluate the Taylor series expansion of exponential function e^x, but I am getting error in my output, though the program gets compiled successfully.
#include <stdio.h>
int main()
{
double sum;
int x;
printf("Enter the value of x: ");
scanf("%d",&x);
sum=1+x+(x^2)/2+(x^3)/6+(x^4)/24+(x^5)/120+(x^6)/720;
printf("The value of e^%d is %.3lf",x,sum);
return 0;
}
^ in C is not an exponentiation operator. It is a bitwise operator. For a short number of terms, it is easier to just multiply.
You also need to take care of integer division. If you divide x*x/2, then you will get integer division. You need to divide the number to get a double answer, as shown below.
You can replace the line calculating the sum with the following line.
sum=1+x+(x*x)/2.0+(x*x*x)/6.0+(x*x*x*x)/24.0+(x*x*x*x*x)/120.0+(x*x*x*x*x*x)/720.0;
A better option would be to use a loop to calculate each term and add it to the answer.
double answer, term = 1;
int divisor = 1;
amswer = term;
for (i=0; i<6; i++)
{
term = term * x / divisor;
answer += term;
divisor *= (i+2);
}
Use pow() instead of ^
Use double x instead of int x
So the result code will look like:
#include <stdio.h>
#include <math.h>
int main()
{
double sum;
double x;
printf("Enter the value of x: ");
scanf("%lf",&x);
sum=1+x+pow(x,2)/2+pow(x,3)/6+pow(x,4)/24+pow(x,5)/120+pow(x,6)/720;
printf("The value of e^%f is %.3lf",x,sum);
return 0;
}
It should be linked with math lib, i.e.:
gcc prog.c -lm
As other people failed to provide proper piece of C code, I have to try it:
#include <stdio.h>
int main() {
printf("Enter the value of x: ");
double x;
scanf("%lf", &x);
double sum = 1.0 + x * (1.0 + x * (1.0 / 2 + x * (1.0 / 3 + x * (1.0 / 4 + x * (1.0 / 5 + x / 6.0)))));
printf("The value of e^%.3lf is %.3lf", x, sum);
}
Better make it dynamic like this one.
#include <stdio.h>
int power(int x,int n){
int sum=1,i;
if (n == 0)
return 1;
for(i=1; i<= n; i++){
sum *= x;
}
return sum;
}
int fact(int n){
if(n == 0)
return 1;
for(i=1; i<= n; i++){
fact *=i;
}
return fact;
}
int main()
{
float sum=0.0;
int i,x,n;
printf("Enter the value of x and n terms: ");
scanf("%d %d",&x,&n);
for(i=0; i<=n; i++){
sum += (float)power(x,i)/fact(i);
}
printf("The value of %d^%d is %.3f",x,n,sum);
return 0;
}
I have a simple script and it works fine. My question is, how can I get my results to display in a reduced simplified fraction? Also, at this point I am defining the values myself, is there a way to have the script ask the user to input the numerator and denominator for the different fractions used?
The posted script worked wonders for me in regards towards asking and performing the operations, so I am very thankful for that. It did not reduce the fractions, am I still missing something?
#include<stdio.h>
#include<math.h>
#include<string.h>
int gcd(int a, int b) {
while(0!=b) { int r = a % b; a=b; b=r; }
return a;
}
int input(char* prompt) {
int res;
printf("%s: ", prompt);
scanf("%d", &res);
return res;
}
main()
{
int add,sub,mul,dd;
int add1,sub1,mul1,dd1;
int a,b,c,d;
int fac = gcd(add, add1);
a=input("Please enter the numerator for your first equation:");
b=input("Please enter the denominator for your first equation:");
c=input("Please enter the numerator for your second equation:");
d=input("Please enter the denominator for your second equation:");
add=(a*d+b*c);
add1=(b*d);
add /=fac;
add1/=fac;
printf("\The sum of your fractions is: %d/%d",add,add1);
sub=(a*d-b*c);
sub1=(b*d);
printf("\nThe difference of your fractions is: %d/%d",sub,sub1);
mul=(a*c);
mul1=(b*d);
printf("\nThe product of your fractions is: %d/%d",mul,mul1);
dd=(a*d);
dd1=(b*c);
printf("\nThe quotient of your fractions is: %d/%d",dd,dd1);
}
for each possible factor from 2 to min (numerator, denominator)
while this factor evenly divides both numerator and denominator
numerator /= factor;
denominator /= factor;
This will do it. Actually, you could go up to min (sqrt (numerator), sqrt (denominator)), and that would work too.
Since you need it several times, best put it into a function and call it on each of your results.
Look up the Euclidean algorithm. This is faster to compute the gcd of two numbers than trying out all possible prime factors.
int gcd(int a, int b) {
return (0==b) ? a : gcd(b, a % b);
}
or
int gcd(int a, int b) {
while(0!=b) { int r = a % b; a=b; b=r; }
return a;
}
Then apply, for instance for the sum, as
fac = gcd(add, add1);
add /=fac;
add1/=fac;
to get the reduced numbers.
To your other question, use the atoi function to transform argument string into numbers or use the scanf function to prompt for input. Be aware that no input verification is done by the C functions.
#include ...
int gcd(int a, int b) { ... }
int input(char* prompt) {
int res;
printf("%s: ", prompt);
scanf("%d", &res);
return res;
}
int main() {
...
int a,b,c,d;
a = input("Number for a");
b = input("Number for b");
...
the formula is pretty complicated. the numerator is num and the denominator is den, in the formula there is a root on the denominator so i have putted den in sqrrt() but sqrrt only accepts doubles
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
void main(void)
{ int i;
float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
float num,den[LEN],r[LEN],xy[LEN],x2[LEN],y2[LEN];
for(i=0;i<LEN;i++)
{
x2[i]=x[i]*x[i];
y2[i]=y[i]*y[i];
xy[i]=x[i]*y[i];
}
num=sum(xy)-sum(x)*sum(y);
for(i=0;i<LEN;i++)
{
den[i]=((LEN*sum(x2)-(sum(x))*(sum(x)))*(LEN*sum(y2))-(sum(y2))*(sum(y2)));
r[i]=num /sqrt(den); /*<----------the problem is here-----> */
}
printf("%f",r);
getch();
}
float sum(float arr[])
{
int i;
float total=0;
for(i=0;i<=LEN;i++)
{
total+=arr[i];
}
return total;
}
Out of sheer boredom I have fixed your code. It is still ugly and extremely inefficient but compiles and should work. I'll leave you or someone else to make it decent.
#include <stdio.h>
#include <math.h>
#define LEN 11
// for the following set of x and y find r by the formula ..
float sum(float arr[]);
int main(void)
{ int i;
float x[]={43.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,49.12,40.71,55.15};
float y[]={102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,91.59,94.85,94.6};
float num,den,r[LEN],xy[LEN],x2[LEN],y2[LEN];
for(i=0;i<LEN;i++)
{
x2[i]=x[i]*x[i];
y2[i]=y[i]*y[i];
xy[i]=x[i]*y[i];
}
num=LEN*sum(xy)-sum(x)*sum(y);
den = (LEN*sum(x2)) - sum(x)*sum(x);
float alpha = sum(y)/LEN - (num/den)*sum(x)/LEN;
printf("beta = %f, alpha = %f\n", num/den, alpha);
for(i=0;i<LEN;i++)
{
float term = y[i] - alpha - (num/den)*x[i];
r[i] = (term*term);
printf("%f",r[i]);
}
}
float sum(float arr[])
{
int i;
float total=0;
for(i=0;i<=LEN;i++)
{
total+=arr[i];
}
return total;
}
To be consistent with the rest of the code, you should presumably be writing:
r[i] = num / sqrt(den[i]);
However, the calculation is not one I recognize. The body of the second loop is going to produce the same result for each value in den and therefore also in r, which is probably not what the question asked for.
You need to give the index den[i] at the denominator....instead in your code you have just passed the base address!
r[i]=num /sqrt(den[i]);
If this is what you want to achieve, which is quite unclear.