Advertisement
snaptrap013

tijanie

Mar 20th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. //CSC099 Individual Assignment
  2. //Name : MUHAMMAD IRFAN TIJANIE BIN MOHAMMAD TASNIM
  3. //STUDENT ID : 2019273078
  4. //GROUP : PI009E34
  5.  
  6. #include <stdio.h>
  7. void realgas(float,int);
  8. void display(int,float);
  9. #define a 3.592         //co2 van der walls constant
  10. #define b 0.0427
  11. #define r 0.08206       //gas constant
  12.  
  13. int main()              //main fn
  14. {
  15.     float mole;
  16.     int temp;
  17.     char choice;
  18.     do  {
  19.         do{
  20.             printf("Mole: ");
  21.             scanf("%f", &mole);
  22.             if(mole<=0)                         //reenter if mole<=0
  23.             printf("Invalid Input . Please enter Positive value\n\n"); 
  24.             }
  25.             while(mole<=0);
  26.         do{
  27.             printf("Temperature (Kelvin): ");
  28.             scanf("%d", &temp);
  29.             if(temp<=0)                         //reenter if temperature<=0
  30.             printf("Invalid Input . Please enter Positive value\n\n");
  31.             }
  32.             while(temp<=0);
  33.         realgas(mole, temp);
  34.         do{
  35.             printf("\n\n\nDo you want to continue (Y,y-Yes, N,n-NO): ");
  36.             scanf(" %c", &choice);
  37.             if(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')  //reenter if invalid input
  38.             printf("Invalid Input. Please enter Y,y or N,n\n");
  39.             }
  40.         while(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n');
  41.        
  42.         printf("-------------------------------------------------------------\n");
  43.             }
  44.     while(choice=='Y'||choice=='y');
  45.     printf("From this calculation, you know that when volume (ml) in increasing,\npressure of gas reduce\n\nThank you");
  46. }
  47.  
  48. void realgas(float mole, int temp)      //calculation fn
  49. {
  50.     int i_vol,f_vol,inc,dec;
  51.     float ps;
  52.    
  53.     do{                                 //repeat if negative
  54.         printf("Initial volume (milliliter): ");
  55.         scanf("%d", &i_vol);
  56.         if(i_vol<=0)        //display error
  57.         printf("Invalid Input . Please enter Positive value\n\n");
  58.         }
  59.     while(i_vol<=0);
  60.    
  61.     do{                                 //repeat if negative
  62.         printf("Final volume(): ");
  63.         scanf("%d", &f_vol);
  64.         if(f_vol<=0)                        //display error
  65.         printf("Invalid Input . Please enter Positive value\n\n");
  66.         }
  67.     while(f_vol<=0);
  68.    
  69.     if(i_vol<f_vol){                    //user enter increment/decrement
  70.         do{
  71.             printf("Increment : ");
  72.             scanf("%d", &inc); 
  73.             if(inc<=0)                  //reenter if increment<=0
  74.             printf("Invalid Input . Please enter Positive value\n\n");
  75.             }
  76.         while(inc<=0);
  77.         }
  78.     else { 
  79.         do{
  80.             printf("Decrement : ");
  81.             scanf("%d", &dec);     
  82.             if(dec<=0)                  //reenter if decrement<=0
  83.             printf("Invalid Input . Please enter Positive value\n\n");
  84.             }
  85.         while(dec<=0);
  86.         }
  87.    
  88.     printf("\n\n%.4f moles of carbon dioxide at %d kelvin\n\n\n", mole, temp);
  89.     printf("Volume (milliliter)\t Pressure(atm)\n");
  90.    
  91.     if(i_vol<f_vol)
  92.     {   do{
  93.             ps=(mole*r*temp)/(i_vol*0.001-(mole*b))-((a*mole*mole)/(i_vol*0.001*i_vol*0.001));
  94.             display(i_vol,ps);      //fn call
  95.             i_vol+=inc;
  96.         }  
  97.         while(i_vol<=f_vol);
  98.     }
  99.    
  100.     else
  101.     {
  102.         do{
  103.             ps=(mole*r*temp)/(i_vol*0.001-(mole*b))-((a*mole*mole)/(i_vol*0.001*i_vol*0.001));
  104.             display(i_vol,ps);      //fn call
  105.             i_vol-=dec;
  106.         }
  107.         while(f_vol<=i_vol);
  108.     }
  109.    
  110. }
  111.  
  112. void display(int vol,float ps)          //display fn
  113. {
  114.     printf("%d\t\t\t %.4f\n", vol, ps);     //show in tab
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement