Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358
// Define functions
void calculate_impedance(double* result_table[6], int components_type[], double component_value[], int component_number, int source_voltage, int frequency, int circuit_type);
void print_results(double* result_table[6], int component_number, int component_type[]); // Function to print result
int i;
double temp_impedRR, temp_impedCR, temp_impedlR, temp_impedReal, temp_impedImag, temp_impedRI, temp_impedCI, temp_impedlI; // impedance variables
double voltreal_R, voltimag_R, voltreal_C, voltimag_C, voltreal_L, voltimag_L, total_voltReal, total_voltImag; // voltage variables (real voltage, imaginary voltage and total voltage - for each item R = resistor, C - capacitor and i - inductor)
double curreal_R, curimag_R, curreal_C, curimag_C, curreal_L, curimag_L, total_curReal, total_curImag; // Current variables (real current, imaginary current and total current)
int main()
{
//Define variables
int parallel;
int series;
int frequency;
int source_voltage;
int component_number;
int circuit_type;
float Zr;
float Zc;
float Zl;
double Zrc_real;
double Zrc_imag;
double current_real;
double current_imag;
double Vr, Vc, Vl;
double Ir;
double Ic_real;
double Ic_imag;
double Il_real;
double Il_imag;
int i;
double* result_table[6];
// Printf statements to get the user input
printf("**17763564 Ananth Kadekodi Assignment 2**\n");
printf("Do you want to create a 1.Parallel or 2.Series circuit evaluation?\n");
scanf("%d", &circuit_type);
printf("Enter the frequency in Hertz of the source: \n");
scanf("%d", &frequency);
printf("Enter the source source_voltage: \n");
scanf("%d", &source_voltage);
printf("How many components are present \n"); // number of components
scanf("%d", &component_number);
//for loop to allcate malloc memory
for(i=0; i < 6; i++)
{
result_table[i] = malloc((component_number+1)*sizeof(double)); //allocate malloc memory for 3D array
}
result_table[4][component_number] = source_voltage;
result_table[5][component_number] = 0;
// allocating memory for component_type and component_values
int* component_type = malloc((component_number)*sizeof(int));
double* component_value = malloc((component_number)*sizeof(double));
int* type_ptrPosition = component_type; // pointer calculation done
double* value_ptrPosition = component_value;// pointer calculation done
switch(circuit_type) // Switch - Case statements for user input
{
case 1:
// For- loop for the component_number
for (i = 0; i < component_number; i++)
{
printf("Enter the component type for each component where 1 is resistor, 2 is capacitor and 3 is inductor\n");
scanf("%d", &type_ptrPosition);
if (component_type[i] == 1)
{
printf("Component %d: \n", (i+1));
printf("Enter the component_value of the component in Ohms: \n");
scanf("%lf", &value_ptrPosition);
} else if (component_type[i]== 2)
{
printf("Component %d: \n", (i+1));
printf("Enter the component_value of the component in Farads: \n");
scanf("%lf", &value_ptrPosition);
} else if (component_type[i] == 3)
{
printf("Component %d: \n", (i+1));
printf("Enter the component_value of the component_number in Henrys: \n");
scanf("%lf", &value_ptrPosition);
}
calculate_impedance(&result_table[6], component_type, component_value, component_number, frequency, circuit_type, source_voltage);
print_results(&result_table[6], component_number, component_type);
type_ptrPosition++;
value_ptrPosition++;
}
break;
case 2:
for (i = 0; i < component_number; i++)
{
printf("Enter the component type for each component where 1 is resistor, 2 is capacitor and 3 is inductor\n");
scanf("%d", &type_ptrPosition);
if (component_type[i] == 1)
{
printf("Component %d: \n", (i+1));
printf("Enter the component_value of the component in Ohms: \n");
scanf("%lf", &value_ptrPosition);
} else if (component_type[i] == 2)
{
printf("Component %d: \n", (i+1));
printf("Enter the component_value of the component in Farads: \n");
scanf("%lf", &value_ptrPosition);
} else if (component_type[i] == 3)
{
printf("Component %d: \n", (i+1));
printf("Enter the component_value of the component_number in Henrys: \n");
scanf("%lf", &value_ptrPosition);
}
calculate_impedance(&result_table[6], component_type, component_value, component_number, frequency, circuit_type, source_voltage);
print_results(&result_table[6], component_number, component_type);
type_ptrPosition++;
value_ptrPosition++;
}
break;
default:
printf("You have specified an invalid input\n");
printf("\n \n");
}
return 0;
}
// Calculation impedance function
void calculate_impedance(double *result_table[6], int component_type[], double component_value[], int component_number, int frequency, int circuit_type, int source_voltage)
{
//define variables
// CR = Capacitance real, voltimag_C (imaginary value of voltage capacitance), curreal_R - real value of current resistor, total_curReal - total real current values
if(circuit_type == 1)
{
for (i=0; i< component_number; i++)
{
if(component_type[i] == 1)
{
// calculations
temp_impedRR = component_value; // Impedance real part of resistor
temp_impedRI = 0;
voltreal_R = source_voltage;
voltimag_R = 0;
curreal_R = (source_voltage/component_value);
curimag_R = 0;
*result_table[0][i] = temp_impedRR; // Store values in array
*result_table[1][i] = temp_impedRI;
*result_table[4][i] = voltreal_R;
*result_table[5][i] = voltimag_R;
*result_table[2][i] = curreal_R;
*result_table[3][i] = curimag_R;
}else if (component_type[i] == 2)
{
temp_impedCI = (double)(0-(1/(2*PI*frequency*component_value)));
temp_impedCR = 0;
voltreal_C = source_voltage; // voltage real value of capacitor
voltimag_C = 0;
curreal_C = ((source_voltage*0)+(0*temp_impedCI))/((0*0)+(temp_impedCI*temp_impedCI)); // current real value of capacitor
curimag_C = ((0*0)-(source_voltage*temp_impedCI))/((0*0)+(temp_impedCI*temp_impedCI)); // current imaginary value of capacitor
*result_table[0][i] = temp_impedRR; // store values in array
*result_table[1][i] = temp_impedRI;
*result_table[4][i] = voltreal_C;
*result_table[5][i] = voltimag_C;
*result_table[3][i] = curreal_C;
*result_table[4][i] = curimag_C;
}else if (component_type[i] == 3)
{
temp_impedlR = 0;
temp_impedlI = (2*PI*frequency*component_value);
voltreal_L = source_voltage;
voltimag_L = 0;
curreal_L = ((source_voltage*0)+(0*temp_impedlI))/((0*0)+(temp_impedlI*temp_impedlI));
curimag_L = ((0*0)-(source_voltage*temp_impedlI))/((0*0)+(temp_impedlI*temp_impedlI));
*result_table[0][i] = temp_impedlR;
*result_table[1][i] = temp_impedlI;
*result_table[4][i] = voltreal_L;
*result_table[5][i] = voltimag_L;
*result_table[3][i] = curreal_L;
*result_table[3][i] = curimag_L;
}
temp_impedReal = (((temp_impedRR*0)+(0*temp_impedlI))/((0*0)+(temp_impedlI*temp_impedlI))); // Temporary impedance real value
temp_impedImag = ((temp_impedRR*0)-(temp_impedRR*temp_impedlI))/((0*0)+(temp_impedlI*temp_impedlI)); // Temporary impedance imaginary
imped_real = ((temp_impedReal*0)+(temp_impedImag*temp_impedlI))/((0*0)+(temp_impedlI*temp_impedlI)); // Total impedance real
*result_table[0][component_number] = imped_real; // store in Array
imped_imag = ((temp_impedImag*0)-(temp_impedReal*temp_impedlI))/((0*0)+(temp_impedlI*temp_impedlI)); // Total impedance imaginary
*result_table[1][component_number] = imped_imag; // store in array
total_curReal = (((source_voltage*imped_real)+(0*imped_imag))/((imped_real*imped_real)+(imped_imag*imped_imag))); // Total current real
*result_table[2][component_number] = total_curReal;
total_curImag = ((0*imped_real)-(source_voltage*imped_imag))/((imped_real*imped_real)+(imped_imag*imped_imag)); // Total current imaginary
*result_table[3][component_number] = total_curReal;
}
}
else if (circuit_type == 2)
{
for (i=0; i< component_number; i++)
{
if(component_type[i] == 1)
{
temp_impedRR = component_value; // Temp total impedance of Resistor Real
temp_impedRI = 0; // Temp total impedance of Resistor Imaginary
}else if (component_type[i] == 2)
{
temp_impedCR = 0-(1/(2*PI*frequency*component_value));
temp_impedCI = 0;
}else if(component_type[i] == 3)
{
temp_impedlR = (2*PI*frequency*component_value); // Temp total impedance of inductor Real
temp_impedlI = 0; // Temp total impedance of inductor imaginary
}
temp_impedReal = (temp_impedRR + 0);
temp_impedImag = (0 + temp_impedCR);
imped_real = (temp_impedRReal + 0);
*result_table[0][component_number] = imped_real;
imped_imag = (temp_impedImag + temp_impedlR);
*result_table[1][component_number] = imped_imag;
total_curReal = ((source_voltage*imped_real)+(0*imped_imag)/((imped_real*imped_real)+(imped_imag*imped_imag));
*result_table[2][component_number] = total_curReal;
*result_table[2][i] = total_curReal;
total_curImag = ((0*out3_real)-(source_voltage*imped_imag))/((imped_real*imped_real)+(imped_imag*imped_imag));
*result_table[3][component_number] = total_curImag;
*result_table[3][i] = total_curImag;
if(component_type[i] == 1)
{
voltreal_R = ((temp_impedRR*total_curReal)-(temp_impedRI*total_curImag));
voltimag_R = ((temp_impedRI*tatal_curReal)+(temp_impedRR*total_curImag));
*result_table[4][i] = voltreal_R;
*result_table[5][i] = voltimag_R;
}else if(component_type[i] == 2)
{
voltreal_C = ((temp_impedCR*total_curReal)-(temp_impedCI*total_curImag));
voltimag_C = ((temp_impedCI*tatal_curReal)+(temp_impedCR*total_curImag));
*result_table[4][i] = voltreal_C;
*result_table[5][i] = voltimag_C;
}else if(component_type[i] == 3)
{
voltreal_L = ((temp_impedlR*total_curReal)-(temp_impedlI*total_curImag)); // Total voltage real value of inductor
voltimag_L = ((temp_impedRlI*tatal_curReal)+(temp_impedlR*total_curImag)); // Total voltage imaginary value of inductor
*result_table[4][i] = voltreal_L;
*result_table[5][i] = voltimag_L;
}
}
}
void print_results(double *result_table[6], int component_number, int component_type) // Function to print result
{
//Declare variable
int i,j;
// Print results
printf("COMPONENTS ");
printf(" | R ");
printf(" | C ");
printf(" I |");
printf(" Total |\n");
printf("Z ( Ohms ) |");
for(i=0; i < 1; i++)
{
for (j=0; j <= 6; j++ ) // For loop to print out array row 0 and 1
{
printf("| %lf% |", &result_table[0][j]);
printf("%lf%", &result_table[1][j]);
}
}
printf("\n");
printf("I ( Amps ) |");
for(i=0; i < 1; i++) // For loop to print out array 2 and 3
{
for (j=0; j <= 6; j++ )
{
printf("| %lf% |", &result_table[2][j]);
printf("%lf%", &result_table[3][j]);
}
}
printf("\n");
printf("V ( Volts) |");
for(i=0; i < 1; i++) // For loop to print out array 4 and 5
{
for (j=0; j <= 6; j++ )
{
printf("| %lf% |", &result_table[4][j]);
printf("%lf%", &result_table[5][j]);
}
}
}
}
Errors I am getting from Borland: