Skip to content

Instantly share code, notes, and snippets.

@osamaadam
Last active February 12, 2018 21:48
Show Gist options
  • Save osamaadam/a50e91d7ba53ad4cd74c767da257cc40 to your computer and use it in GitHub Desktop.
Save osamaadam/a50e91d7ba53ad4cd74c767da257cc40 to your computer and use it in GitHub Desktop.
Simple data plugging
#include <stdio.h>
#include <math.h>
#define Ra 4.7 //The constant resistance
#define Ein 10 //Input voltage
#define Rx 1 //True value
int size; //Number of readings you're going to input
void resistance(double *volt, double *resi);
void mean(double *Rb, double *mean);
void meanDeviation(double *mean, double *resi, double *deviation);
void avgDeviation(double *deviation, double *avgDev);
void stdDeviation(double *deviation, double *stdDeviation);
void rangeFu(double *resi, double *range);
void impresFu(double *resi, double *impres);
int main()
{
printf("Enter the number of readings:");
scanf("%d",&size);
double Eo[size],Rb[size],meanVr,devMean[size],avgDev,stdDevRes,range,impres[size]; //If you see squiggly lines here, ignore them
double *Eptr;
Eptr = Eo;
for(int i=0; i<size; i++)
{
printf("Enter reading number %d: ",(i+1));
scanf("%lf",(Eptr+i));
if(*(Eptr+i) >= Ein) //Eliminating negative resistance values
{
printf("The value of reading you entered was more than %d.\n",Ein);
return 1;
}
else if (*(Eptr+i) <= 0) //Eliminating runtime errors errors
{
printf("The value of the reading your entered was zero or negative.\n");
return 1;
}
}
/*Data plugging*/
resistance(&Eo[0],&Rb[0]);
mean(&Rb[0],&meanVr);
meanDeviation(&meanVr,&Rb[0],&devMean[0]);
avgDeviation(&devMean[0], &avgDev);
stdDeviation(&devMean[0], &stdDevRes);
rangeFu(&Rb[0],&range);
impresFu(&Rb[0],&impres[0]);
/* Printing output starts*/
for(int j=0; j<size; j++)
{
printf("The value of resistance number %d = %lf\n", (j+1), Rb[j]);
}
for(int k=0; k<size; k++)
{
printf("Deviation from mean for resistance number %d = %lf\n",(k+1),*(devMean+k));
}
for(int l=0; l<size; l++)
{
printf("Error for resistance number %d = %lf\n",(l+1),impres[l]);
}
printf("The average deviation = %lf\n",avgDev);
printf("Standard deviation = %lf\n",stdDevRes);
printf("Mean = %lf\n",meanVr);
printf("Range = %lf\n",range);
/*printing output ends*/
}
void resistance(double *volt, double *resi)
{//calculating the value of the resistance and plugging it into an array
for(int j=0; j<size; j++)
{
*(resi+j) = (Ra * (*(volt+j)))/(Ein-(*(volt+j)));
}
}
void mean(double *Rb, double *mean)
{
double numerator = 0;
for(int k=0; k<size; k++)
{
numerator += *(Rb+k);
}
*mean = numerator/size;
}
void meanDeviation(double *mean, double *resi, double *deviation)
{
for(int l=0; l<size; l++)
{
*(deviation+l) = (*(resi+l) - *(mean));
}
}
void avgDeviation(double *deviation, double *avgDev)
{
for(int m=0; m<size; m++)
{
*(deviation+m) = sqrt(*(deviation+m) * *(deviation+m)); //Modulus
}
double sigmaDev = 0;
for(int n=0; n<size; n++)
{
sigmaDev += *(deviation+n);
}
*avgDev = (sigmaDev)/size;
}
void stdDeviation(double *deviation, double *stdDeviation)
{
double devSqr[size];
for(int i=0; i<size;i++)
{
devSqr[i] = (*(deviation+i)) * (*(deviation+i)); //squaring
}
double sigmadevSqr;
for(int j=0; j<size; j++)
{
sigmadevSqr += devSqr[j];
}
*stdDeviation = sqrt(sigmadevSqr/(size-1));
}
void rangeFu(double *resi, double *range)
{
double Rmax=0,Rmin=500;
for(int i=0; i<(size); i++)
{ //Selecting the highest value
if(*(resi+i) > Rmax)
{
Rmax = *(resi+i);
}
//Selecting the lowest value
if(*(resi+i) < Rmin)
{
Rmin = *(resi+i);
}
}
*range = sqrt((Rmax - Rmin) * (Rmax - Rmin));
}
void impresFu(double *resi, double *impres)
{
for(int i=0; i<size; i++)
{
*(impres+i) = (sqrt((*(resi+i) - Rx) * (*(resi+i) - Rx))/Rx) * 100; //Modulus
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment