Incompatible type errors for the 2 arguments I'm inputting in main

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rusty550
    New Member
    • Mar 2010
    • 1

    Incompatible type errors for the 2 arguments I'm inputting in main

    I can't seem to fix this. I'm really new at this, and this is driving me f**king crazy. Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double calculusx[1000];
    double calculusy[1000];
    double integral0i[1000];
    double integrali999[1000];
    double derivative[1000];
    int i=0;
    
    
    
    double Area0i(double[], double[]);
    double Areai999(double[], double[]); 
    double Slope(double[],double[]);
    
    double Slope(double x[], double y[])
    {
    double result;
    result = (((x[i] - x[i-1])/(x[i+1] - x[i-1]))*((y[i] - y[i-1])/(x[i] - x[i-1]))) + (((x[i+1] - x[i])/(x[i+1] - x[i-1]))*((y[i+1] - y[i-1])/(x[i] - x[i-1])));
    return result;
    }
    
    double Area0i(double x[], double y[])
    {
    double result;
    int j;
    	for (j=0; j<=i; j++)
    		{
    		result += ((x[i]-x[i-1])*((y[i]+y[i+1])/2));
    		}
    return result;
    }
    
    double Areai999(double x[], double y[])
    {
    double result;
    int j;
    	for (j=i; j<1000; j++)
    		{
    		result += ((x[i]-x[i-1])*((y[i]+y[i+1])/2));
    		}
    return result;
    }
    
    int main(int argc, char *argv[])
    {
    int z;
    FILE* fp;
    FILE* fp2;
    
    	if (argc<2)
    		{
    		return EXIT_FAILURE;
    		}
    
    	fp = fopen(argv[1],"r");
    
    	if (argv[i] == NULL || fp == NULL)
    		{
    		printf("Incorrect filename.\n");
    		return EXIT_FAILURE;
    		}
    		
    	fp2 = fopen("program2output.txt", "w");
    		
    	for (z=0; z<1000; z++)
    		{
    		fscanf(fp, "%lf %lf", &calculusx[z], &calculusy[z]);
    		}
    		
    	for (i=0; i<1000; i++)
    		{
    		integral0i[i] = Area0i(calculusx[i], calculusy[i]);
    		integrali999[i] = Areai999(calculusx[i], calculusy[i]);
    		derivative[i] = Slope(calculusx[i], calculusy[i]);
    			
    		fprintf(fp2, "%5.3lf\t%5.3lf\t%5.3lf\t%5.3lf\t%5.3lf\n", calculusx[i],calculusy[i], integral0i[i], integrali999[i], derivative[i]);
    		}
    		
    	fclose(fp);
    	fclose(fp2);
    	
    return EXIT_SUCCESS;
    	
    }

    The program is meant to read a list of inputs from a file, store then in two arrays I have named calculusx and calculusy, perform some math on them, and then spit the outputs into another file.

    The problem is, whenever I try to compile, the three functions I call in main (integral0i, integrali999, and derivative) give me errors that the arguments in the functions are of an incompatible type. I swear that I've defined the arrays as doubles, the functions return a double, and that I am putting in doubles as my argument. Can somebody please help me with this, it is driving me insane.
    Last edited by Banfa; Mar 12 '10, 01:29 AM. Reason: Added [code] ... [/code] tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Vut you define the parameters of your functions as pointers to doubles not doubles but you are passing them doubles.

    Also all you data is global and that is really bad practise.

    Look at you function Slope, defined like this

    Code:
    double Slope(double x[], double y[])
    {
        double result;
        result = (((x[i] - x[i-1])/(x[i+1] - x[i-1]))*((y[i] - y[i-1])/(x[i] - x[i-1]))) + (((x[i+1] - x[i])/(x[i+1] - x[i-1]))*((y[i+1] - y[i-1])/(x[i] - x[i-1])));
        return result;
    }
    Called like this
    Code:
    derivative[i] = Slope(calculusx[i], calculusy[i]);
    The parameters passed to the function do not match the parameters it is expecting. Additionally the function uses i with is declared globally and uses the expression x[i-1] even though i might be 0.

    i should be passed to all your functions the functions should not use the global variable. This is encapsulation, by encapsulating the data the function uses within the function itself you protect it from outside interference and make it more robust and easier to maintain. The function should check the value of i passed to it and make sure that its valid.

    These declarations
    Code:
    double calculusx[1000];
    double calculusy[1000];
    double integral0i[1000];
    double integrali999[1000];
    double derivative[1000];
    int i=0;
    Should be inside main. However you should not put large arrays on the stack so calculusx and calculusy should either be declared static or be allocated from the heap. integral0i, integrali999 and derivative do not need to be arrays since they are only used locally to the loop in the main function.

    Comment

    Working...