Chukxy's Posts
Nairaland Forum › Chukxy's Profile › Chukxy's Posts
Please C professionals in the house, assist me as it is urgent! |
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" ;scanf("%lf",&L1); while(L1<0) { printf("\n Please enter a valid value \n" ;} *Lval=L1; printf("\n Please enter the value of the capacitor \n" ;scanf("%lf",&C1); while(C1<0) { printf("\n Please enter a valid value \n" ;} *Cval=C1; printf("\n Please enter the value of the resistor \n" ;scanf("%lf",&R1); while(R1<0) { printf("\n Please enter a valid value \n" ;} *Rval=R1; printf("\n Please enter the value of the frequency \n" ;scanf("%lf",&F1); while(F1<0) { printf("\n Please enter a valid value \n" ;} *Fval=F1; printf("\n Please enter the value of the second frequency \n" ;scanf("%lf",&F2); while(F1<0) { printf("\n Please enter a valid value \n" ;} *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" ;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" ;} } 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. |
@Movingcoil! How are you doing? It has been a long time I heard from you. I would want us to be sharing our experiences in C language as i am also new in C. By so doing, we get to learn more and faster. Hope to hear from you soon. |
@ poster. If you have not got the solution before now, here is the corrected code: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> /*string variable declaration*/ char usrname[], pass[]; char *name = "dikachi"; char *passr = "xerone"; /*variable and constant declaration*/ int rad; const float pie = 3.142; /*function declaration*/ int area(void); /*main function statement*/ int main(int argc, char *argv[]) { printf("enter the username and password to calculate the area of a circle\n" ;scanf("%s", usrname); scanf("%s", pass); /*control statement*/ if((strcmp(name,usrname)==0)&&(strcmp(passr,pass)==0)){ //if((name == usrname) && (passr == pass)) printf("the area of the circle is:%d\n ", area ());/*calling the function area*/ } else { printf("ACCESS DENIED" ;} system("PAUSE" ;return 0; } /*function definition*/ int area(void) { printf("what is the value of the radius!" ;scanf("%d", &rad); return(pie * pow(rad, 2)); } Please go through it carefully and learn where you made mistakes. Cheers!! |
Thank you guys! My interest is rekindled again. You people are too much! |
Thanks Dueal! I appreciate. |
@dueal. Thanks for the clarification. I also want you to give me more insight on why one should make c the first choice among other languages in terms of hardware programming. |
Good people, please I want to know why hardware programmers make C programming language as their first choice. This is because, i am currently studying digital systems and computer engineering; hence we are studying c as part of our course. What i have learnt so far about C, there is no much different with java(which i have known before now) except that it is a structured language while java is object oriented language. Before now, my opinion about c is that it is a low level language associated with machine codes(0s and 1s) which makes it more efficient to program hardwares with it, but what we have done so far, I am yet to see that. C professionals in the house please educate me on the capabilities of C so that I will regain my interest in it. Thanks in anticipation! |
yes, send me your email add for me to send the one i used then, though i don't know if is out of date now. |
@poster, happy to note that you are about taken a very challenging exam. In short i am very pleased with your decision . However, my experience about the exam when I took it at NIIT kaduna in 2006, you must in addition to what you have studied so far concerning the course go through Scjp dumps for you to be fully prepared. One more caution, getting the certification is not a guarantee to great achievement in IT. What matters most is the skills you have gathered through rigoruos training and personal development. With your skill, you can on your own do one unique project that will open doors or catapult you to lime light. Once you have built your potofolio through this project and can deliver, they would not even ask you of your certification. Once again, I wish you best of luck. |
@Poster, to my humble opinion is not wort it as you did not get a fair bargain. One thing i know about IT from my experience with NIIT and other IT institute, you can only have the basis there, then develop yourself afterwards. You can always learn on your own if actually want to get the skill you want. Wishing good luck! |
Good people, I want to know if Nigeria wins their match and Tunisia draws their match, What will be the fate of Nigeria.? |
I have developed some applications in java. My challenge is to set this application on the desktop so that by just clicking on the icon on the desktop, the application would run, instead of the usual java classname on the command prompt to run the application. Your suggestions are welcomed. Thank you in advance. |
My good people, I'm so much pleased with your suggestions. The problem i have now is to package the application in a jar file. Please direct me on how to package the application. Thank you. |
It is meant to be a desktop application. |
I developed an inventory application using microsoft access as my data base(Java based). My problem now is to transfer this application to different users using different systems. Do I need to creat data base in any system i want to transfer the application for its subsequent use or is there any better way to do it? Suggestions are welcomed. Thank you. |
How about developing an automated Admission process for post graduate studies. If need help in doing this don't hesitate to contact me. My email is chukxy2004@yahoo.com or +234 08035484239 |
Thank you very much for the suggestions so far. The truth is that I can not run the programmer after implementing the suggestions. Please is there anything i can do to the browser in terms of configurationb to enable me run the applet. thanks. |
Please c an somebody in the house help me? I'm having problem running my applet in a browser. Can only run it with appletviewer. Suggestions will be welcomed. Thank you. |
If you had compiled this program before now, it must have displayed error during compilation due to mistake in line 7; but i have just corrected th error. So you can go on compile and run the program. Thanks. pally, just like i promised yesterday, here is the solution to the problem: class BinaryDec { public static void main(String arg[]) { String binaryno = arg[0]; int decno; decno= Integer.parseInt(binaryno,2); System.out.println(''The Decimal equivalent =''+decno); } } compile, javac BinaryDec.com run java BinaryDec 1101 the ans will be 13. Thanks and stay connected.[/color][color=#990000][/color][color=#990000] |
Pally, I will be at your disposal for all you need to be a guru.As a matter of fact, I have a working knowledge of javascript, html,and java. I'm sun certified java programmer. So you can contact me with this no:+234 08035484239 or my email chukxy2004@yahoo.com Looking forward hearing from you. |
pally, just like i promised yesterday, here is the solution to the problem: class BinaryDec { public static void main(String arg[]) { String binaryno = arg[0]; int decno; decno= Integer.parseInt(binary,2); System.out.println(''The Decimal equivalent =''+decno); } } compile, javac BinaryDec.com run java BinaryDec 1101 the ans will be 13. Thanks and stay connected. |
hi guy, I just read your post. I promise to give you the solution morrow. Stay connected. Tnx. |
;