Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,502 members, 7,819,823 topics. Date: Tuesday, 07 May 2024 at 01:11 AM

Please Assist Me In This C Code Challenge! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Assist Me In This C Code Challenge! (1218 Views)

Help! Im Stuck With This C# Program / Please Programmars Help Me Out With This C# Program! I Have Test Tommorrow!!!!! / Programmer Should Please Help Me To Solve This C++ Questions (2) (3) (4)

(1) (Reply) (Go Down)

Please Assist Me In This C Code Challenge! by chukxy: 11:20am On Mar 21, 2010
The code below is meant to calculate the magnitude and phase of the impedance of a series C-R circuit. It calculates the result over a range of frequencies, stores them in two arrays of results and then uses a two dimensional array of characters to plot a simple graph using characters on the terminal screen. This is primarily an exercise in using arrays and the program is portable, so it could be re-compiled with almost any c compiler for almost operating system. Although it would be possible to produce a much more sophisticated graph using Windows graphics, this would make the program non-portable to any other operating system.

The program should prompt the user for two frequencies instead of one. These are the start frequency and stop frequency. It should then prompt the user for the resistance(ohms), the capacitance(farads) and the inductance(henrys) and do the calculation and plot the graph.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

#define PI 3.1415926538979323846
#define WIDE 40
#define HIGH 20
/* This program is written by chuxy, It gets values from the user and stores them in variables*/
/* The program also calculates the magnitude of impedance and phase angle of impedance */

/* declaration of prototype functions */

void input_vals(double *Lval,double *Cval,double *Rval,double *Fval,double *Fval2);
double calc_Xc(double *Cval,double *Fval);
double calc_Xl(double *Lval,double *Fval);
double calc_ZMag(double *Rval,double *Xc,double *Xl);
double calc_ZAngle(double *Rval,double *Xc,double *Xl);
void print_results(double *Zmag,double *Zphase);
void swap(double *p,double *p1);
void find_max_val(double Zmag1[],double Zphase1[]);
void initialize_graph_array(double Zmag[],double Zphase1[],char symbol_4_Zmag[HIGH][WIDE],char symbol_4_Zphase[HIGH][WIDE]);
void print_graph(char symbol_4_Zmag[HIGH][WIDE],char symbol_4_Zphase[HIGH][WIDE]);

int main(void)
{

double L1,C1,F1,F2,R1,Xc1[40],Xl1[40],Zmag1[40],Zphase1[40],Frq[40]; /* declaration main function variables */
char symbol_4_Zmag[HIGH][WIDE],symbol_4_Zphase[HIGH][WIDE]; /* declaration of variables for graph array */
int c1,c2,c3; //declaration of counters for for loops
int cnt=0;
int freq_scale_fact=0;

input_vals(&L1,&C1,&R1,&F1,&F2);

freq_scale_fact=(F2-F1)/40; //initializes the scalefactor


for(cnt=0;cnt<40;cnt++) //initializes the array of frequencies using scale factor
{
Frq[cnt]=F1+freq_scale_fact*cnt;
}


for(c1=0;c1<40;c1++) //calculates the the inductive reactance and capacitive reactance
{

Xc1[c1]=calc_Xc(&C1,&Frq[c1]);

Xl1[c1]=calc_Xl(&L1,&Frq[c1]);
}

for(c2=0;c2<40;c2++) //calculates the magnatitude and angle of impeadance and storethe value in array
{
Zmag1[c2]=calc_ZMag(&R1,&Xc1[c2],&Xl1[c2]);

Zphase1[c2]=calc_ZAngle(&R1,&Xc1[c2],&Xl1[c2]);
}


for(c3=0;c3<40;c3++) //prints the results of the arrays
{
print_results(&Zmag1[c3],&Zphase1[c3]);
}
/* the sorts the array of result */

find_max_val(Zmag1,Zphase1);

initialize_graph_array(Zmag1,Zphase1,symbol_4_Zmag,symbol_4_Zphase);
print_graph(symbol_4_Zmag,symbol_4_Zphase);
_getche();

return 0;

}

void input_vals(double *Lval,double *Cval,double *Rval,double *Fval,double *Fval2)
{
double L1,C1,R1,F1,F2;
printf("\n Please enter the value of the inductor \n"wink;
scanf("%lf",&L1);
while(L1<0)
{
printf("\n Please enter a valid value \n"wink;

}
*Lval=L1;

printf("\n Please enter the value of the capacitor \n"wink;
scanf("%lf",&C1);
while(C1<0)
{
printf("\n Please enter a valid value \n"wink;

}
*Cval=C1;

printf("\n Please enter the value of the resistor \n"wink;
scanf("%lf",&R1);
while(R1<0)
{
printf("\n Please enter a valid value \n"wink;

}

*Rval=R1;


printf("\n Please enter the value of the frequency \n"wink;
scanf("%lf",&F1);
while(F1<0)
{
printf("\n Please enter a valid value \n"wink;

}
*Fval=F1;

printf("\n Please enter the value of the second frequency \n"wink;
scanf("%lf",&F2);
while(F1<0)
{
printf("\n Please enter a valid value \n"wink;

}
*Fval2=F2;
}

double calc_Xc(double *Cval,double *Fval)
{
double F1a,C1a,Xc;
C1a=*Cval;
F1a=*Fval;
Xc=1/(2*PI*F1a*C1a);
return Xc;
}

double calc_Xl(double *Lval,double *Fval)
{
double L1a,F1a,Xl;
L1a = *Lval;
F1a= *Fval;

Xl=2*PI*L1a*F1a;
return Xl;

}

double calc_ZMag(double *Rval,double *Xc,double *Xl)
{
double Zmag,R1,Xc1,Xl1;
R1=*Rval;
Xc1=*Xc;
Xl1=*Xl;

Zmag=sqrt(((Xl1-Xc1)*(Xl1-Xc1))+(R1*R1));
return Zmag;

}

double calc_ZAngle(double *Rval,double *Xc,double *Xl)
{
double Zphase,R1b,Xc2,Xl2,ZphaseA;
R1b=*Rval;
Xc2=*Xc;
Xl2=*Xl;

Zphase=atan((Xl2-Xc2)/R1b);
ZphaseA=(Zphase*180)/PI;
return ZphaseA;

}

void print_results(double *Zmag,double *Zphase)
{
printf("\n Zmagnitude = %lf ohms, Zphase = %lf degrees \n",*Zmag,*Zphase);

}

void swap(double *p,double *p1)
{
double tmt;
tmt=*p;
*p=*p1;
*p1=tmt;
}
void find_max_val(double Zmag1[],double Zphase1[])
{
int c4,c5;
for(c4=0;c4<39;++c4)
for(c5=39;c5>c4;--c5)
{
if(Zmag1[c5-1]>Zmag1[c5])
swap(&Zmag1[c5-1],&Zmag1[c5]);

if(Zphase1[c5-1]>Zphase1[c5])
swap(&Zphase1[c5-1],&Zphase1[c5]);
}
printf("\n\n\n The maximum impeadance and its angle after sorting is:\n\n\n"wink;
printf("\n Zmagnitude = %lf ohms, Zphase = %lf degrees \n",Zmag1[39],Zphase1[0]);


}
void initialize_graph_array(double Zmag[],double Zphase1[],char symbol_4_Zmag[HIGH][WIDE],char symbol_4_Zphase[HIGH][WIDE])
{
int cnt1, cnt2;

int scale_star[40],scale_cross[40],cnt3;
for(cnt3=0;cnt3<40;cnt3++)
{
scale_star[cnt3]=((Zmag[cnt3]/Zmag[39])*20);
scale_cross[cnt3]=(((Zphase1[cnt3]+90)/180)*20);
}
for(cnt1=0;cnt1<HIGH;cnt1++)
for(cnt2=0;cnt2<WIDE;cnt2++)
{
symbol_4_Zmag[(scale_star[cnt1])][cnt2]='*';
symbol_4_Zphase[(scale_cross[cnt1])][cnt2]='+';
}

}
void print_graph(char symbol_4_Zmag[HIGH][WIDE],char symbol_4_Zphase[HIGH][WIDE])
{
int cnt1,cnt2;
for(cnt1=0;cnt1<HIGH;cnt1++)
for(cnt2=0;cnt2<WIDE;cnt2++)
{
printf("%c",symbol_4_Zmag[cnt1][cnt2]);
printf("%c",symbol_4_Zphase[cnt1][cnt2]);
printf("\n"wink;
}
}

My current challenge in this code is that when the function to print graph is invoked, it prints disarray of characters that i did not even specify in the program. Please assist me to retify the challenge.

Thanks in anticipation.
Re: Please Assist Me In This C Code Challenge! by chukxy: 12:40pm On Mar 21, 2010
Please C professionals in the house, assist me as it is urgent!
Re: Please Assist Me In This C Code Challenge! by chukxy: 4:59pm On Mar 22, 2010
I thank God that I have overcome my challenge. I am sort of disappointed that nobody even suggested how to go about it, if it were to debate on the best programming language , i would have received more than a thousand response. Thanks but no thanks!
Re: Please Assist Me In This C Code Challenge! by dellnet: 5:32pm On Mar 22, 2010
Who have you employed here to sit down and wait for your post and give you immediate response? if it is that urgent.

(1) (Reply)

Nigerian Developers Can Now Get Paid On Google Play / Help Me Cross Check My Visual Basic Code / Host A Wordpress

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 29
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.