Last active
March 21, 2019 13:58
-
-
Save MD-AZMAL/63437a54046c884b45614fc0886bcd8c to your computer and use it in GitHub Desktop.
Programming Practice : Pointer Projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| /* | |
| dispense_change() parameters | |
| ----------------------------- | |
| int n : the amount | |
| int *s50 : how many 50's to return | |
| int *s20 : how many 20's to return | |
| int *s10 : // how many 10's to return | |
| ------------------------------- | |
| Taken all the returning values as output parameter as the function should return more | |
| than one value. | |
| */ | |
| void dispense_change(int n, int *s50,int *s20,int *s10) { | |
| *s50 = n/50; // basically means how many 50s ex in Rs 130, two 50s are required | |
| n%=50; // take out no of 50s and then calculate for 20, do same for 10 ... notice reduction is in decreasing order | |
| // with greatest number devided first and then remainder is calculated for the left over | |
| *s20 = n/20; | |
| n%=20; | |
| *s10 = n/10; | |
| } | |
| int main() { | |
| int amt; // to store user input amount | |
| int s50,s20,s10; // to store number of changes | |
| printf("Enter the amount : "); | |
| scanf("%d",&amt); | |
| dispense_change(amt,&s50,&s20,&s10); // passing only the address of output parameter | |
| // notice nothing is retured but in the method s50,s20,s10 values are set | |
| // print the change | |
| printf("No of 50s :%d\nNo of 20s : %d\nNo of 10s : %d\n",s50,s20,s10); | |
| // print statement is self explanatory, dont get confused by same variable names in the two methods | |
| // run the program yourself and analyse the logic.... should be tough | |
| return 0; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<ctype.h> | |
| #define start 1 | |
| #define stop 0 | |
| #define idt 2 | |
| #define num 3 | |
| #define bnum 4 | |
| #define bid 5 | |
| void transition(int *cstate,char transChar) { | |
| if(transChar == ' ' || transChar == '.'){ | |
| if(*cstate == bid) | |
| *cstate = idt; | |
| else if(*cstate == bnum) | |
| *cstate = num; | |
| if(transChar == '.') | |
| *cstate = stop; | |
| } else if(isalpha(transChar) || transChar == '_' || isdigit(transChar)) { | |
| if(*cstate != bid && isdigit(transChar)) | |
| *cstate = bnum; | |
| else | |
| *cstate = bid; | |
| } | |
| } | |
| int main(){ | |
| int inp,cstate; | |
| char ch; | |
| cstate = start; | |
| do{ | |
| do{ | |
| if(cstate == idt) { | |
| printf(" - identifier\n"); | |
| cstate = start; | |
| } else if (cstate == num) { | |
| printf(" - number\n"); | |
| cstate = start; | |
| } | |
| inp = scanf("%c",&ch); | |
| if(inp == 1) { | |
| if(ch != ' ' && ch != '.') | |
| printf("%c",ch); | |
| transition(&cstate,ch); | |
| } else { | |
| cstate = stop; | |
| } | |
| }while(cstate != stop); | |
| }while(inp == 1); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<math.h> | |
| void app_exp(double *calcExp,int x) { | |
| *calcExp = pow((2.0*x + 1)/(2.0*x - 1),x); | |
| } | |
| int main() { | |
| int x; | |
| double calcExp = 0.0; | |
| for(x=0; fabs(calcExp - exp(1)) > 1e-6;app_exp(&calcExp,x)){ | |
| x++; | |
| } | |
| printf("X : %d\nApproximate e : %.7f\ne : %.7f\n",x,calcExp,exp(1)); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<math.h> | |
| void checkAns(int,int *,int *,int *); // function prototype for check answer function | |
| int main() { | |
| int N, a1,a2,a3; | |
| a1 = a2 = a3 = 0; // a1,a2,a3 store answers of 3 questions respectively | |
| // zero means answer is false | |
| // the method will set it to one if answer is true | |
| printf("Enter N : "); | |
| scanf("%d",&N); | |
| checkAns(N,&a1,&a2,&a3); // method called so the result of answers are in a1, a2, a3 as they are passed by reference | |
| // each there are 3 if else for answer checking | |
| // notice no if else-if is there as it wasnt required | |
| if(a1) { | |
| printf("It is multiple of 7, 11 or 13\n"); | |
| } else { | |
| printf("It is not multiple of 7, 11 or 13\n"); | |
| } | |
| if(a2) { | |
| printf("The sum of digits is even\n"); | |
| } else { | |
| printf("The sum of digits is odd\n"); | |
| } | |
| if(a3) { | |
| printf("It is a prime number\n"); | |
| } else { | |
| printf("It is not a prime number\n"); | |
| } | |
| return 0; | |
| } | |
| /* | |
| checkAns() parameters | |
| ----------------------- | |
| int N : the number entered | |
| int *q1 : to store answer to first question | |
| int *q2 : to store answer of second question | |
| int *q3 : to store answer of third question | |
| they are in pointer as they are used as output parameters | |
| */ | |
| void checkAns(int N, int *q1,int *q2,int *q3) { | |
| if(N % 7 == 0 || N % 11 == 0 || N % 13 == 0) { | |
| *q1 = 1; | |
| } // if ends here .. it checks if N is divisible by 7,21 or 13 and sets value of q1 to 1 | |
| // notice there is no else to this if ... so even if condition is true other blocks are executed | |
| int i,s=0; | |
| for(i = 2 ; i < floor(sqrt(N)) ; i++) { | |
| if( N % i == 0) { | |
| break; | |
| } | |
| } // loop checks for prime number .. if not prime loops breaks before floor(sqrt(N)) | |
| if(i == floor(sqrt(N))) { | |
| *q3 = 1; | |
| } // if i = floor(Sqrt(N)) means the number is prime so set q3 to 1 | |
| while(N > 0) { | |
| i = N % 10; | |
| s+=i; | |
| N /= 10; | |
| } // calculate sum of each digit | |
| if(s % 2 == 0) { | |
| *q2 = 1; | |
| } // check if sum is even then set q2 as 1 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| /* | |
| All calc function recieve same parameters as pointer and set the value of missing variable | |
| notice if any one value is missing others must be present to calculate the missing one | |
| The formula will change depending on the value missing ... its like finding x in an equation | |
| and deducing the formula for x | |
| */ | |
| // calculate h depending on formula | |
| void calc_h(double *h,double *k,double *A,double *t1,double *t2,double *x) { | |
| *h = *k * *A * (*t2 - *t1) / *x; | |
| } | |
| // calculate k .. and so on | |
| void calc_k(double *h,double *k,double *A,double *t1,double *t2,double *x) { | |
| *k = *h * *x / (*A * (*t2 - *t1)); | |
| } | |
| void calc_a(double *h,double *k,double *A,double *t1,double *t2,double *x) { | |
| *A = *h * *x / (*k * (*t2 - *t1)); | |
| } | |
| void calc_t1(double *h,double *k,double *A,double *t1,double *t2,double *x) { | |
| *t1 = *t2 - (*h * *x / (*k * *A)); | |
| } | |
| void calc_t2(double *h,double *k,double *A,double *t1,double *t2,double *x) { | |
| *t2 = *t1 + (*h * *x / (*k * *A)); | |
| } | |
| void calc_x(double *h,double *k,double *A,double *t1,double *t2,double *x) { | |
| *x = *k * *A * (*t2 - *t1) / *h; | |
| } | |
| /* | |
| driver() prarameters | |
| -------------------- | |
| accepts all parameter of the formula as pointer parameters | |
| checks for the missing one and calculates it using required calc method | |
| */ | |
| void driver(double *h,double *k, double *a,double *t1,double *t2,double *x) { | |
| char hc,kc,ac,t1c,t2c,xc; // for scanning question mark of any missing parameter | |
| int inp; // to check input status | |
| printf("Respond with data known for unknown quantity (?)\n"); | |
| printf("Enter H : "); | |
| inp = scanf("%lf",h); // inp will be zero if the value entered is not double type as %lf is in scanf | |
| // if ? is entered the inp is zero | |
| // hence check for inp validation after every input value to check for ? | |
| if(!inp){ | |
| scanf("%c",&hc); | |
| } | |
| printf("Enter K : "); | |
| inp = scanf("%lf",k); | |
| if(!inp) { | |
| scanf("%c",&kc); | |
| } | |
| printf("Enter A : "); | |
| inp = scanf("%lf",a); | |
| if(!inp) { | |
| scanf("%c",&ac); | |
| } | |
| printf("Enter T1 : "); | |
| inp = scanf("%lf",t1); | |
| if(!inp) { | |
| scanf("%c",&t1c); | |
| } | |
| printf("Enter T2 : "); | |
| inp = scanf("%lf",t2); | |
| if(!inp) { | |
| scanf("%c",&t2c); | |
| } | |
| printf("Enter X : "); | |
| inp = scanf("%lf",x); | |
| if(!inp) { | |
| scanf("%c",&xc); | |
| } | |
| // print the formula | |
| printf(" KA (T2 - T1)\n"); | |
| printf("H = --------------\n"); | |
| printf(" X\n"); | |
| // call required calc function and print the value of it based on which variable is ? | |
| if(hc == '?') { | |
| calc_h(h,k,a,t1,t2,x); | |
| printf("Rate of heat transfer %.2f W\n",*h); | |
| } else if(kc == '?') { | |
| calc_k(h,k,a,t1,t2,x); | |
| printf("Coefficiet of thermal conductivity %.2f W/m-k\n",*k); | |
| } else if(ac == '?') { | |
| calc_a(h,k,a,t1,t2,x); | |
| printf("Cross-sectional area of conductor %.2f m^2\n",*a); | |
| } else if(t1c == '?') { | |
| calc_t1(h,k,a,t1,t2,x); | |
| printf("Temperature on one side is %.2f K\n",*t1); | |
| } else if(t2c == '?') { | |
| calc_t2(h,k,a,t1,t2,x); | |
| printf("Temperature on other side is %.2f K\n",*t2); | |
| } else { | |
| calc_x(h,k,a,t1,t2,x); | |
| printf("Thickness of conductor %.4f m\n",*x); | |
| } | |
| // printing all the values | |
| printf("H = %.2f W \t T2 = %.2f K\nK = %.2f W/m-k \t T1 = %.2f K\nA = %.2f m^2 \t X = %.4f m\n",*h,*t2,*k,*t1,*a,*x); | |
| } | |
| int main() { | |
| double h,k,a,t1,t2,x; | |
| driver(&h,&k,&a,&t1,&t2,&x); | |
| return 0; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<math.h> | |
| /* | |
| approx_root parameters | |
| ---------------------- | |
| double *NG : to make the next guess... which will eventually become the approximation | |
| double *LG : to keep track of last guess | |
| double N : value of X | |
| */ | |
| void approx_root(double *NG,double *LG,double N) { | |
| *NG = 0.5 * (*LG + N/ *LG); // calculate the first next guess by the given formula | |
| // loop until the next guess and last guess have an absolute difference of 0.005 | |
| // if true then last guess becomes the new guess | |
| // in the increment section next guess is recalculated | |
| for(; fabs(*NG - *LG) > 0.005 ; *NG = 0.5 * (*LG + N/ *LG)) { | |
| *LG = *NG; | |
| } | |
| // at the end of the loop NG holds the approximate value | |
| } | |
| int main() { | |
| double N,LG=1.0,NG; | |
| /* | |
| N : value of N | |
| LG : Last Guess initally set to 1.0 atq | |
| NG : next guess...to store the final approximate result | |
| */ | |
| printf("Enter N : "); | |
| scanf("%lf",&N); | |
| approx_root(&NG,&LG,N); // call approx root and pass the parameters | |
| printf("Approximate root is %.4f\n",NG); // display approximation | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<math.h> | |
| double round_money(double N) { | |
| N = N * 100 + 0.5; | |
| int tmp = (int)N; | |
| return tmp/100.0; | |
| } | |
| void calc_charge(double H,double *tot,double *avg) { | |
| *tot = 7.99; | |
| if(H - 10.0 > 0) { | |
| *tot += ceil(H - 10.0)*1.99; | |
| } | |
| *tot = round_money(*tot); | |
| *avg = *tot / H; | |
| *avg = round_money(*avg); | |
| } | |
| int main() { | |
| int inpStatus,id,MM,YYYY; | |
| double H,tot,avg; | |
| FILE *inp; | |
| FILE *op; | |
| inp = fopen("usage.txt","r"); | |
| op = fopen("charges.txt","w"); | |
| inpStatus = fscanf(inp,"%d",&MM); | |
| inpStatus = fscanf(inp,"%d",&YYYY); | |
| if(inpStatus) { | |
| fprintf(op,"Charges for %d/%d\n\n",MM,YYYY); | |
| fprintf(op,"Customer Hours Charges Average\n"); | |
| } | |
| inpStatus = fscanf(inp,"%d",&id); | |
| inpStatus = fscanf(inp,"%lf",&H); | |
| while(inpStatus == 1) { | |
| calc_charge(H,&tot,&avg); | |
| fprintf(op,"%d\t%.2lf\t%.2lf\t%.2lf\n",id,H,tot,avg); | |
| inpStatus = fscanf(inp,"%d",&id); | |
| inpStatus = fscanf(inp,"%lf",&H); | |
| } | |
| fclose(inp); | |
| fclose(op); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment