Hi I'm finding a sum of polynomial in c, without massive, and i have this error that says " invalid operands to binary+(have 'float()(int, int, int)' and 'float()(int, int, int)'"
here is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float p6(int p6, int x, int a){ `function to find pow`
p6=pow(x, 6);
p6=a*p6;
return p6;
}
float p5(int p5, int x, int a){ `small function`
p5=pow(x, 5);
p5=p5*a;
return p5;
}
float p4(int p4, int x, int a){
p4=pow(x, 4);
p4=a*p4;
return p4;
}
float p3(int p3, int a, int x){
p3=pow(x, 3);
p3=a*p3;
return p3;
}
float p2(int p2, int a, int x){
p2=pow(x, 2);
p2=a*p2;
return p2;
}
main (){ `main function starts here`
int i, a;
double sum=0;
float x;
printf("x-iin utgiig oruul"); `value of x`
scanf("%lf", &x);
printf("a1-a6 toog oruul"); `value of coefficents`
for(i=1; i<=6; i++){ `for coeffincents`
scanf("%d", &a);
}
sum=p6+p5+p4+p3+p2+a*x; `error occurs here`
printf("%d", sum);
system("pause");
return 0;
}
baceause p6, p5, ... are functions so you you have to make function calls in the main. Somthing like that:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float p6(int x, int a){ //`function to find pow`
int p6=pow(x, 6);
p6=a*p6;
return p6;
}
float p5(int x, int a){ //`small function`
int p5=pow(x, 5);
p5=p5*a;
return p5;
}
float p4(int x, int a){
int p4=pow(x, 4);
p4=a*p4;
return p4;
}
float p3(int a, int x){
int p3=pow(x, 3);
p3=a*p3;
return p3;
}
float p2(int a, int x){
int p2=pow(x, 2);
p2=a*p2;
return p2;
}
int main (){ //`main function starts here`
int i, a;
double sum=0;
float x;
printf("x-iin utgiig oruul"); //`value of x`
scanf("%f", &x);
printf("a1-a6 toog oruul"); //`value of coefficents`
for(i=1; i<=6; i++){ //`for coeffincents`
scanf("%d", &a);
}
sum=p6(x, a)+p5(x, a)+p4(x, a)+p3(x, a)+p2(x, a)+a*x; //`error occurs here`
printf("%lf", sum);
system("pause");
return 0;
}
Related
I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;
I am doing an assignment for school and I am supposed to make the following code (which generates pgm image with a Buddhabrot fractal) parallel.
Thing is the compiler doesn't point any errors and still the code won't even get to main. I have already tried looking it up but didn't find a way to solve it.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define NT 3
int nColumns, nLines, size, ite, proximo;
double dt, x, y;
float **mat;
pthread_mutex_t mutex;
pthread_cond_t condM;
pthread_cond_t condE;
typedef struct {
int x;
int y;
} int2;
typedef struct{
double x;
double y;
} double2;
int2 coordinatesConversion(double x,double y, int nColumns,int nLines){
int2 ret;
int2 retError;
retError.x=-1;
retError.y=-1;
ret.x=round(((2.0+x)/3.5) *((double)(nColumns-1)));
ret.y=round(((1.5+y)/3.5) *((double)(nLines-1)));
if(ret.x<0 || ret.x>=nColumns) return retError;
if(ret.y<0 || ret.y>=nLines) return retError;
return ret;
}
int printMatrixToFilePGM(float **mat,int tamx, int nLines, char *srcFile){
FILE *arq=fopen(srcFile,"w");
int cont, cont2;
float min,max;
min=mat[0][0];
max=mat[0][0];
for(cont=0;cont<nLines;cont++)
for(cont2=0;cont2<tamx;cont2++){
if(min>mat[cont][cont2]) min=mat[cont][cont2];
if(max<mat[cont][cont2]) max=mat[cont][cont2];
}
max=max*0.35;
float delta = max-min;
fprintf(arq,"P2 \n");
fprintf(arq,"%d\n%d \n",tamx,nLines);
fprintf(arq,"255\n");
for(cont=0;cont<nLines;cont++)
for(cont2=0;cont2<tamx;cont2++){
int valpixel=((mat[cont][cont2]-min)/delta)*255.0f;
if(valpixel>255) valpixel=255;
fprintf(arq,"%d \n", valpixel);
}
fclose(arq);
}
float** mallocFloatMatrix(int tamx, int nLines, float defaultValueOfTheElementsAtMatrix){
float **errorCodeReturn=0x0;
float **mat;
int i,j;
int condErrorMalloc=0;
mat=malloc(sizeof(float *)*nLines);
if(mat==0x0) return errorCodeReturn;
for(i=0;i<tamx;i++) mat[i]=malloc(sizeof(float )*tamx);
for(i=0;i<tamx;i++)
if(mat[i]==0x0){
condErrorMalloc=1;
break;
}
if(condErrorMalloc==0) return mat;
for(i=0;i<nLines;i++) for(j=0;j<tamx;j++) mat[i][j]=defaultValueOfTheElementsAtMatrix;
for(i=0;i<tamx;i++) if(mat[i]!=0x0) free(mat[i]);
free(mat);
return errorCodeReturn;
}
void freeFloatMatrix(float **mat,int tamx, int nLines){
int i;
for(i=0;i<nLines;i++) if(mat[i]!=0x0) free(mat[i]);
free(mat);
}
int iteration(double x,double y, int nColumns,int nLines, int ite,int2 *iterationPath){
int cont;
int condInvalidPointer=1;
double2 z;
z.x=0.0;
z.y=0.0;
double2 c;
c.x=x;
c.y=y;
double2 zt;
for(cont=0;cont<ite;cont++){
zt.x=((z.x*z.x)-(z.y*z.y))+c.x;
zt.y=(2.0*(z.x*z.y))+c.y;
z=zt;
if(((z.x*z.x)+(z.y*z.y))>4.0){
if(cont>100) condInvalidPointer=0;
break;
}
iterationPath[cont]=coordinatesConversion(z.x,z.y,nColumns,nLines);
}
if(condInvalidPointer) return 0;
return cont;
}
void *mestre(void *param) {
int i;
for(i=0;i<size;i++){
x = -2.0+((double)i*dt);
for(y=-2.0;y<2.0;y=y+dt){
pthread_mutex_lock(&mutex);
proximo++;
pthread_cond_signal(&condE);
pthread_cond_wait(&condM, &mutex);
pthread_mutex_unlock(&mutex);
}
}
}
void *escravo(void *param) {
pthread_mutex_lock(&mutex);
if(proximo==0) pthread_cond_wait(&condE, &mutex);
double xe = x;
double ye = y;
proximo--;
pthread_cond_signal(&condM);
pthread_mutex_unlock(&mutex);
int k;
int2* iterationPath = (int2 *)malloc(sizeof(int2)*ite);
if(iterationPath==0x0) return 0x0;
int completedIterations = iteration(xe, ye, nColumns, nLines, ite, iterationPath);
for(k=0;k<completedIterations;k++){
if(iterationPath[k].x!=-1 && iterationPath[k].y!=-1)
mat[iterationPath[k].x][iterationPath[k].y]=mat[iterationPath[k].x][iterationPath[k].y]+1.0f;
}
free(iterationPath);
}
int main(void){
printf("Main method has started.");
pthread_mutex_init(&(mutex), NULL);
pthread_cond_init(&(condM), NULL);
pthread_cond_init(&(condE), NULL);
nColumns=2048;
nLines=2048;
dt=0.001;
size=round(4.0/dt);
ite=600;
proximo = 0;
mat=mallocFloatMatrix(nColumns,nLines,0.0f);
if(mat==0x0) return 0;
pthread_t m;
pthread_t escravos[NT-1];
pthread_create(&m, NULL, mestre, (void*)0);
int i;
for(i=0; i<NT-1; i++) pthread_create(&escravos[i], NULL, escravo, (void*)0);
pthread_exit(NULL);
printMatrixToFilePGM(mat,nColumns,nLines,"saida5.pgm");
freeFloatMatrix(mat,nColumns,nLines);
return 0;
}
This question already has answers here:
conflicting types error when compiling c program using gcc
(3 answers)
Closed 7 years ago.
So I'm making a function that makes a table, that puts the x and y values through an equation. this is what I have so far.
#include <stdio.h>
#include <math.h>
int main(){
int x, y;
float num;
printf("%3c", '+');
for (x=5; x <= 100;x=x+5){
printf("%8i",x);
}
printf("\n");
for (y = 5; y<= 100;y=y+5){
printf("%3d ",y);
for (x=5;x<=100;x=x+5){
num = theMath(x, y);
printf("%7f", num);
printf(" ");
}
printf("\n");
}
return 0;
}
float theMath(int x, int y){
float sum;
sum = ((x*x*x*x)/(y*y)) + sqrt(y);
return sum;
}
It's telling me "Error: Conflicting types for 'theMath'", and I can't figure out why. Compiling with gcc.
Add a prototype definition(function declaration) of theMath before its function call. That should be the reason for the error.
#include <stdio.h>
#include <math.h>
//add declaration of the function here
float theMath(int x, int y);
int main(){
int x, y;
float num;
printf("%3c", '+');
for (x=5; x <= 100;x=x+5){
printf("%8i",x);
}
printf("\n");
for (y = 5; y<= 100;y=y+5){
printf("%3d ",y);
for (x=5;x<=100;x=x+5){
num = theMath(x, y);
printf("%7f", num);
printf(" ");
}
printf("\n");
}
return 0;
}
float theMath(int x, int y){
float sum;
sum = ((x*x*x*x)/(y*y)) + sqrt(y);
return sum;
}
Your function theMath is used before being defined, and it is not declared before usage. You just have to declare the function at the beginning of your C file:
float theMath(int x, int y);
I am getting errors in the following code. The errors disappear if I take out "struct point p2...". p1 is assembled the same way and works fine, what is the catch here?
#include <stdio.h>
struct point {
int x;
int y;
};
struct point makepoint(int x, int y)
{
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
void main()
{
struct point p1 = makepoint(5, 7);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
struct point p2 = makepoint(2, 9);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
}
#include <stdio.h>
#include <string.h>
struct arithmet {
char eval;
int first_num,sec_num;
};
/* function declaration */
void eval_value( struct arithmet *valyu );
int main( ) {
struct arithmet valyu1;
struct arithmet valyu2;
char oper;
int num1,num2;
oper=getch(); //gets(oper);
scanf("%d",&num1);
scanf("%d",&num1);
valyu1.first_num=num1;
valyu2.sec_num=num2;
valyu1.eval=oper;
eval_value(&num1);
eval_value(&num2);
eval_value(&per);
printBook( &Book1 );
printBook( &Book2 );
return 0;
}
void eval_value(struct arithmet *valyu)
{
valyu->first_num;
valyu->sec_num;
valyu->oper;
if(oper=='+')
{
printf("%d",addit(int a,int b));
}
if(oper=='-')
{
printf("%d",subtractit(int a,int b));
}
if(oper=='*')
{
printf("%d",multiplyit(int a,int b));
}
if(oper=='/')
{
printf("%d",divideit(int a,int b));
}
if(oper=='#')
{
printf("%d",intdivideit(int a,int b));
}
if(oper=='%')
{
printf("%d",remdivideit(int a,int b));
}
if(oper=='~')
{
printf("%d",exponeit(int a,int b));
}
}
#include <stdio.h>
struct arithmetic {
int x;
int y;
char oper;
};
int calcul(int a,int b)
{
struct arithmetic temp;
int x,y;
char proc;
temp.x=x;
temp.y=y;
temp.proc=proc;
if(proc=='+')
{
int addit(int x,int y)
{
return(x+y);
}
}
if(proc=='-')
{
int devit(int x,int y)
{
return(x-y);
}
}
if(proc=='*')
{
int cumlatit(int x,int y)
{
return(x*y);
}
}
if(proc=='/')
{
int dividit(int x,int y)
{
return(x/y);
}
}
if(proc=='#')
{
int intdividit(int x,int y)
{
return(x/y);
}
}
if(proc=='%')
{
int remdivit(int x,int y)
{
return(x%y);
}
}
if(proc=='`')
{
int exponit(int x,int y)
{
int i=1,k=1;
for(i=1;i<=y;i++)
{
k*=x;
}
return(k);
}
}
void main()
{
struct arithmetic v1;
int num1,num2;
char proc;
scanf(&num1);
scanf(&num1);
proc=getch();
v1.x=num1;
v1.y=num2;
v1.oper=proc;
struct calcul();
}
To avoid the many hidden calls to memcpy()
To avoid the many compiler allocated memory areas
(that cannot be accessed/used by anything else.)
I suggest the following code,
which compiles cleanly
and exercises each function
#include <stdio.h>
struct point
{
int x;
int y;
};
void makepoint(int x, int y, struct point* newPoint)
{
newPoint->x = x;
newPoint->y = y;
} // end function: makepoint
void addpoint(struct point* p1, struct point* p2)
{
p1->x += p2->x;
p1->y += p2->y;
} // end function: addpoint
int main()
{
struct point p1;
struct point p2;
makepoint(5, 7, &p1);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
makepoint(2, 9, &p2);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
addpoint( &p1, &p2 );
printf("p1 = (%d, %d)\n", p1.x, p1.y);
return(0);
} // end function: main
i have code to array of func pointer
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
code for array of pointer to function:
#include <stdio.h>
int sum(int, int);
int product(int, int);
int subtract(int, int);
int main()
{
int i = 0;
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int);
pfun[0] = sum;
pfun[1] = product;
pfun[2] = subtract;
for( i = 0 ; i < 3 ; i++)
{
result = pfun[i](a, b);
printf("\nresult = %d", result);
}
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("\n\nThe product of the sum and the subtract = %d\n",result);
}
int sum(int x, int y)
{
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int subtract(int x, int y)
{
return x - y;
}
now how to combine this two program. such that array of pointers pointing to func pointers and the func pointers may have different number of args? any suggestion.
You not only need to store function pointers with a variable number of arguments (that is not very difficult, you could use a union for instance), but you also need to make sure you call the functions with the correct argument, and that is a bit trickier given your design.
I suggest to use a stack instead. All your functions would only take the stack as an argument:
void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
And your array could be declared this way:
typedef void callback_t(stack_t *);
callback_t *p[] =
{
sum,
subtract,
product,
/* ... */
};
Then for instance sum would be implemented as such:
void sum(stack_t *stack)
{
if (depth(stack) < 2)
perror("Not enough arguments in stack!");
int b = popstack(stack);
int a = popstack(stack);
int c = a + b;
pushstack(stack, c);
}
But unary minus would be implemented this way:
void neg(stack_t *stack)
{
if (depth(stack) < 1)
perror("Not enough arguments in stack!");
int a = popstack(stack);
pushstack(stack, -a);
}
Each function decides how many arguments they need. The caller does not need to know.