Thread: Need some help...

  1. #31
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    Help Me..

    When I compile my code using the koolplot project, it does not show any error.. Any reason for that?

  2. #32
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by darkconvoy View Post
    When I compile my code using the koolplot project, it does not show any error.. Any reason for that?
    Your compiler does not warn about usage of not initilized variables - you need to look for that yourself - use my posted warnigns as a prompt where to start...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #33
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    Help me..(cont)

    My values of sumxy, sumx2, sumnx2, sumnxy, sumy2, sumny2 and sumnxy2 does not return the supposed value... Can anyone check my code?

    Code:
    /*
    *This program will read a set of data from a file, calulate the correlation with the ok-line and
    *draw the graph of the ok-line and plot the set of data on the graph
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <koolplot.h>
    
    int main()
    {
    	/* Define variables */
    	int j, xy, sl, yi, xl, xu, xp, p, n, sn;
    	double arrayx[50], arrayy[50], Mx, My, sumxy, sumx2, sumnx2, sumnxy, sumy2, sumny2, sumnxy2;
    	double sMx, sMy, ssumx2, ssumxy, ssumy2, ssumnx2, ssumny2, cor, scor;
    	
    	/*Function Prototype*/
    	int readData(double arrayx[], double arrayy[]);
    	double getMx(double arrayx[], int n);
    	double getMy(double arrayy[], int n);
    	double getsumxy(double arrayx[], double arrayy[], double Mx, double My, int n);
    	double getsumx2(double arrayx[], double Mx, int n);
    	double getsumnx2(double sumx2, int n);
    	double getsumnxy(double sumxy, int n);
    	double getsumy2(double arrayy[], double My, int n);
    	double getsumny2(double sumy2, int n);
    	double getsumnxy2(double sumnx2, double sumny2);
    	void printResults(double scor);
    	void graph(double arrayx[], double arrayy[], int sl, int yi, int xl, int xu, int xy);
    	
    	/* Read data from file */
    	xy = readData(arrayx, arrayy);
    	
    	/*Enter details of Ok line*/
    	printf("Generating the ok-line\n");
    	printf("Enter the slope of ok line: ");
    	scanf("%d", &sl);
    	printf("Enter the y-intercept of the ok line: ");
    	scanf("%d", &yi);
    	printf("Enter lower bound of x value: ");
    	scanf("%d", &xl);
    	printf("Enter upper bound of x value: ");
    	scanf("%d", &xu);
    	printf("Enter the number of points to be generated: ");
    	scanf("%d", &p);
    	
    	/*Store data from ok-line*/
    	n = xy+p;
    	
    	for (j=xy; j<n; j++)
    	{
    			xp = j-xy;
    			arrayy[j] = sl*xp + yi;
    	}
    	
    	for (j=xy; j<n; j++)
    	{
    			xp = j-xy;
    			arrayx[j] = xp;
    	}
    
    	/*Obtain values to calculate for correlation*/
    	Mx = getMx(arrayx, n);
    	My = getMy(arrayy, n);
    	sumxy = getsumxy(arrayx, arrayy, sMx, sMy, n);
    	sumx2 = getsumx2(arrayx, sMx, n);
    	sumnx2 = getsumnx2(ssumx2, n);
    	sumnxy = getsumnxy(ssumxy, n);
    	sumy2 = getsumy2(arrayy, sMy, n);
    	sumny2 = getsumny2(ssumy2, n);
    	sumnxy2 = getsumnxy2(ssumnx2, ssumny2);
    
    	/*End of values to calculate for correlation*/
    	
    	/*Obtain value of cor*/
    	cor = sumnxy/sumnxy2;
    	
    	/*Print results*/
    	printResults(scor);
    
    	/*Draw graph*/
    	graph(arrayx, arrayy, sl, yi, xl, xu, xy);
    	
    	return 0;
    }
    
    /* Function readData 
     * Opens file and read data into marks array.
     *
     * Parameter: arrayx and arrayy - array of doubles to store data from file
     *                  
     * Return:    number of entries read from file
     */
    int readData(double arrayx[], double arrayy[])
    {
       	char rdata[25];
    	int j, xy;
    	FILE *data;
    	printf("File name(must be typed with file type): ");
    	scanf("%s", &rdata);
    	
    	/*Total pair of data from file*/
    	printf("Total data pair in file: ");
    	scanf("%d", &xy);
       
        /* Open input file.  */
        data = fopen(rdata,"r");
    
        /*  Read data .  */
    	for(j=0; j < (xy); j++)
    	{
    		fscanf(data, "%lf %lf", &arrayx[j], &arrayy[j]);
        }	 
    	
    	return xy;
    }/*End of readData*/
    
    /*Determine mean of x*/
    double getMx(double arrayx[], int n)
    {
    	double sumx1=0.00, Mx;
    	int j;
    	for(j = 0; j < n; j++)
    	{
    		sumx1 = sumx1 + arrayx[j];
    	}
    	Mx = sumx1/n;
    	return Mx;
    }/*End mean of x*/
    
    /*Determine mean of y*/
    double getMy(double arrayy[], int n)
    {
    	double sumy1=0.00, My;
    	int j;
    	for(j = 0; j < n; j++)
    	{
    		sumy1 = sumy1 + arrayy[j];
    	}
    	My = sumy1/n;
    	return My;
    }/*End mean of y*/
    
    /*Determine sumxy*/
    double getsumxy(double arrayx[], double arrayy[], double Mx, double My, int n)
    {
    	double sumxy;
    	int j;
    	
    	sumxy = 0.00;
    	for(j=0; j < n; j++)
    	{
    		sumxy = sumxy + ((arrayx[j]) - Mx)*((arrayy[j]) - My);
    	}
    	
    	return sumxy;
    }/*End of sumxy*/
    
    double getsumx2(double arrayx[], double Mx, int n)
    {
    	/*Determine sumx2*/
    	double sumx2 = 0.00;
    	int j;
    	for(j = 0; j < n; j++)
    	{
    		sumx2 = sumx2 + ((arrayx[j] - Mx)*(arrayx[j] - Mx));
    	}
    	/*End of sumx2*/
    	return sumx2;
    }
    
    double getsumnx2(double sumx2, int n)
    {
    	double sumnx2; 
    	double c;
    	int j; 
    	
    	/*Determine sumnx2*/
    	sumnx2 = 0.00;
    	c = 0.00;
    	c = c + (sumx2/n);
    	sumnx2 = sumnx2 + sqrt(c);
    	return sumnx2;
    }/*End of sumnx2*/
    
    double getsumnxy(double sumxy, int n)
    {
    	double sumnxy;
    	/*Determine sumnxy*/
    	sumnxy = 0.00;
    	sumnxy = sumnxy + (sumxy/n);
    	return sumnxy;
    }/*End of sumnxy*/
    
    double getsumy2(double arrayy[], double My, int n)
    {
    	double sumy2;
    	int j;
    	/*Determine sumy2*/
    	sumy2 = 0.00;
    	for(j = 0; j < n; j++)
    	{
    		sumy2 = sumy2 + ((arrayy[j] - My)*(arrayy[j] - My));
    	}
    	return sumy2;
    }/*End of sumy2*/
    
    double getsumny2(double sumy2, int n)
    {
    	/*Determine sumny2*/
    	double sumny2 = 0.00;
    	double d = 0.00;
    	d = d + (sumy2/n);
    	sumny2 = sumny2 + sqrt(d);
    	return sumny2;
    }/*End of sumny2*/
    
    double getsumnxy2(double sumnx2, double sumny2)
    {
    	/*Determine sumnxy2*/
    	double sumnxy2 = 0.00;
    	sumnxy2 = sumnxy2 + (sumnx2 * sumny2);
    	return sumnxy2;
    }/*End of sumnxy2*/
    
    /* Function printResults 
     * Display the value of a, b, and cor
     * Determine the status of chip
     *
     * Parameter: geta           - the value of a
     * Parameter: getb           - the value of b
     * Parameter: cor  			 - the value of correlation
     * return   - void
     */
    void printResults(double cor)
    {
    	printf("Value of correlation is %.2lf\n", cor);
    	if (cor < 0.98)
    	{
    		printf("Possible chip failure\n");
    	}
    	else
    	{
    		printf("Chip is OK\n");
    	}
    
    }
    
    /*Function graph
     *Draws graph of ok line and 
     *plots the data from the file on the graph*/
    void graph(double arrayx[], double arrayy[], int sl, int yi, int xl, int xu, int xy)
    {
    	int j;
    	
    	plotdata x(xl, xu);
    	plotdata y = sl*x + yi;
    	for (j=0; j<xy; j++)
    	{
    		addMark(x, y, arrayx[j], arrayy[j]);
    	}
    	plot(x, y, BLACK, "SELF MONITORING: ok line: y = 2x + 3");
    
    }

Popular pages Recent additions subscribe to a feed