Reputation: 986
This program is for adding two polynomials and printing the result.
It just stores the multipliers in array and use the index az the power of parameter
It does sum and subtract but when it comes to product it prints
0x0 0*x2 0*x3 ...
It's for basic C programming class and in three hours I should give it to master :-(
It gets the multiplier in an array and calculates the result using some functions:
#include <stdio.h>
#include <stdlib.h>
double a[50], b[50], c[101];
int dega, degb;
SumArray (a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i, max = (dega < degb ? degb : dega) + 1;
for(i = 0; i < max; i++)
c[i] = a[i] + b[i];
}
SubtractArray(a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i, max = (dega < degb ? degb : dega) + 1;
for (i = 0; i < max; i++)
c[i]=a[i]-b[i];
}
ProductArray(a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i,j;
double tempa, tempb;
for(i = 0; i < dega + 1; i++)
for(j = 0; j < degb + 1; j++)
{
tempa = a[i];
tempb = b[j];
c[i + j] = c[i + j] + (tempa * tempb);
}
}
int main()
{
extern int dega, degb;
extern double a[50], b[50], c[]; //stores the multipliers
int i, operation;
for(i = 0; i < 50; i++)
a[i] = b[i] = 0;
for(i = 0; i < 102; i++)
c[i] = 0;
printf("darjeye chand jomleee ha ra vared konid");
scanf("%d %d", °a, °b);
printf("zarayebe chand jomlee aval ra vaerd konid");
for(i = 0; i < dega + 1; i++)
scanf("%d", &a[i]);
printf("zarayebe chand jomlee dovoom ra vaerd konid");
for(i = 0; i < degb + 1; i++)
scanf("%d", &b[i]);
printf("amaliyate morede nazartan ra vare konid baraye jame 0, tafrigh 1, zarb 2 ra vared konid");
scanf("%d", &operation);
switch(operation)
{
case 0:
{
SumArray(a,b);
for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
{
printf(" %d*x", c[i]);
printf("%d ", i);
}
break;
}
case 1:
{
SubtractArray(a,b);
for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
{
printf(" %d*x", c[i]);
printf("%d ", i);
}
break;
}
case 2:
{
ProductArray(a,b);
for(i = 0; i < (dega + degb + 1); i++)
{
printf("\%d*x", c[i]);
printf("%d ", i);
}
break;
}
default:
printf("amaliyate vared shode sahih nabud");
}
system("PAUSE");
return 0;
}
Upvotes: 1
Views: 153
Reputation: 1922
scanf("%d",a[i]);
should be scanf("%d",&a[i]);
and scanf("%d",b[i]);
should be scanf("%d",&b[i]);
scanf
requires address of the variable. For an array, say, in your case double a[50]
, simply writing a
gives you the starting address of array a
which will be same as &a[0]
where a[0]
is simply the first element, but not the address to the first element. a[i]
is the element where &a[i]
is the address to that element. Hope you got it.
Upvotes: 1