1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

How to have to variable looping at the same time with the same increment?

Discussion in 'C' started by askmewhy25, Mar 5, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Here is my problem I can't figure out how to loop two variable at the same
    while they increment at the same time with the same increment
    I can't explain myself I will just give an example:

    Code:
    The increment is 50
    T1          T2
    300         350
    350         400
    400         450
    450         500
    500         550
    550         600
    600         650
    650         700
    700         750
    750         800
    800         850
    850         900
    900         950
    950         1000
    
    Here is my program
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
      int i,j,T;
      
      printf("Input T: ");
      scanf("%d",&T);
      for(j=300+T;j<=1000;j+=T)
      for(i=300;i<=(1000-T);i+=T){
        printf("T1 = %d and T2 = %d\n",i,j);
        }          
      getche();
    }
    
     
    Last edited: Mar 5, 2010
  2. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    I checked your code in linux machine.so I have changed few lines.

    In your code there is no need to two loops.You can do that using a single loop.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
      int i,j,T;
    
      printf("Input T: ");
      scanf("%d",&T);
        for(i=300;i<=950;i+=T)
        {
                j=i+50;
          printf("T1 = %d and T2 = %d\n",i,j);
        }
    }
    
     
  3. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    do this:-
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    int main(){
      int i,j,T;
      
      printf("Input T: ");
      scanf("%d",&T);
      for(i=300,j=350;i<=1000  && j<=1000 ;i+=T,j+=T)
       {
        printf("T1 = %d and T2 = %d\n",i,j);
        }          
      getch();
    return 0;
    }
    
    
     
  4. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Thanks techgeek.in but i have another question..:happy: if the user wanted to input numbers with decimal numbers how will i do that??
     
  5. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    
    int main(){
        float T1,T2,end1,increment;
        printf("first variable=");
        scanf("%f",&T1);
        printf("second variable=");
        scanf("%f",&T2);
        printf("increment for first and second variable=");
        scanf("%f",&increment);
        printf("end when first variable is>=");
        scanf("%f",&end1);
        printf("The increment is %f",increment);
            printf("\n     T1              T2\n");
        while(T1<end1){
            T1+=increment;
            T2+=increment;
            printf("%10.3f      %10.3f\n",T1,T2);
        }
        getchar(); getchar();
    }
    
    
    with this code you can have T1,T2 and increment as demical numbers
     
    Last edited: Mar 5, 2010
  6. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    u can try this:-
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    int main(){
      float i,j,T;
      
      printf("Input T: ");
      scanf("%f",&T);
      for(i=300.0,j=350.0;i<=1000.0  && j<=1000.0 ;i+=T,j+=T)
       {
        printf("T1 = %f and T2 = %f\n",i,j);
        }          
      getch();
      return 0;
    }
    
     
  7. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    Now I upgraded the program..:D thanks to all your help!! but i have a last question
    how can I set the range or the stopper to T2 to 1000K because the table of the ideal
    gas is for only 300K to 1000K so beyond that range in not applicable..

    Code:
    //This program generates a table for the specific internal energy
    //and the specific enthalpy of Methane from 300K to 1000K using the
    //specific heat variations listed on Table A.21
    
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
      int QA;
      float T1=300,T2,end1,inc;
      double a=3.826,b=-3.979,c=24.558,d=-22.733,e=6.963;
      double A,B,C,D,E,H,U,R=8.3143,M=16.04,i=10;
      char error[128];
      
      do{
      do{
        system("cls");
        printf("Enter the Increment in Temperature: ");
        scanf("%s",&error);
        inc=atof(error);
        if(inc<=0){
          system("cls");
          printf("Invalid Input. Please try again!!!\n");
          printf("Please press the spacebar to continue!!");
          getche();
          }
        }while(inc<=0);
      T2=T1+inc;
      end1=1000-inc;
      printf("The increment is %.3f\n\n",inc);
      printf("\nInitial Temperature\tFinal Temperature\tEnthalpy\tInternal Energy\n");
      while(T1<=end1){
        T1+=inc;
        T2+=inc;
        A=a*(T2-T1);
        B=(1/pow(i,3))*(b/2)*(pow(T2,2)-pow(T1,2));
        C=(1/pow(i,6))*(c/3)*(pow(T2,3)-pow(T1,3));
        D=(1/pow(i,9))*(d/4)*(pow(T2,4)-pow(T1,4));
        E=(1/pow(i,12))*(e/5)*(pow(T2,5)-pow(T1,5));
        H=(R/M)*(A+B+C+D+E);
        U=(R/M)*((A+B+C+D+E)-(T2-T1));
        printf("%10.3f K\t\t%10.3f K\t\t%.3f kJ/kg\t%.3f kJ/kg\n",T1,T2,H,U);
        }
      do{//This used to ask the user if s/he wants to repeat the evaluation
        printf("\n\nDo you want to repeat the computation or Quit?\n");
        printf("[1] Yes\n");
        printf("[2] Quit\n");
        scanf("%s",&error);
        QA=atoi(error);
        switch(QA){
          case 1:
            break;
          case 2://Return to the selection screen
            exit(1);
            break;
          default://The program redirects invalid inputs to this part
            system("cls");
            printf("Invalid input. Please try again!!!\n");
            printf("Please press the spacebar to continue!!");
            getche();
            break;   
            }
          }while(QA>2||QA<1);
        }while(QA==1);         
      getche();
    }
    
     
  8. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    see this:-
    Code:
    //This program generates a table for the specific internal energy
    //and the specific enthalpy of Methane from 300K to 1000K using the
    //specific heat variations listed on Table A.21
    
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
      int QA;
      float T1=300,T2,end1,inc;
      double a=3.826,b=-3.979,c=24.558,d=-22.733,e=6.963;
      double A,B,C,D,E,H,U,R=8.3143,M=16.04,i=10;
      char error[128];
      
      do{
      do{
        system("cls");
        printf("Enter the Increment in Temperature: ");
        scanf("%s",&error);
        inc=atof(error);
        if(inc<=0){
          system("cls");
          printf("Invalid Input. Please try again!!!\n");
          printf("Please press the spacebar to continue!!");
          getche();
          }
        }while(inc<=0);
      T2=T1+inc;
      end1=1000-inc;
      printf("The increment is %.3f\n\n",inc);
      printf("\nInitial Temperature\tFinal Temperature\tEnthalpy\tInternal Energy\n");
      while(T1<=end1){
        T1+=inc;
        T2+=inc;
        A=a*(T2-T1);
        B=(1/pow(i,3))*(b/2)*(pow(T2,2)-pow(T1,2));
        C=(1/pow(i,6))*(c/3)*(pow(T2,3)-pow(T1,3));
        D=(1/pow(i,9))*(d/4)*(pow(T2,4)-pow(T1,4));
        E=(1/pow(i,12))*(e/5)*(pow(T2,5)-pow(T1,5));
        H=(R/M)*(A+B+C+D+E);
        U=(R/M)*((A+B+C+D+E)-(T2-T1));
        printf("%10.3f K\t\t%10.3f K\t\t%.3f kJ/kg\t%.3f kJ/kg\n",T1,T2,H,U);
        }
      do{//This used to ask the user if s/he wants to repeat the evaluation
        printf("\n\nDo you want to repeat the computation or Quit?\n");
        printf("[1] Yes\n");
        printf("[2] Quit\n");
        scanf("%s",&error);
        QA=atoi(error);
        switch(QA){
          case 1:
            break;
          case 2://Return to the selection screen
            exit(1);
            break;
          default://The program redirects invalid inputs to this part
            system("cls");
            printf("Invalid input. Please try again!!!\n");
            printf("Please press the spacebar to continue!!");
            getche();
            break;   
            }
          }while(QA>2||QA<1);
        }while(QA==1 && T2<=1000);         
      getche();
    }
    
    
     
  9. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    correct the above code like this

    Code:
    .......................
        switch(QA){
          case 1:
                [COLOR=Red]T1=300;//add this
                T2=0;//add this[/COLOR]
            break;
          case 2://Return to the selection screen
            exit(1);
    ......................
    
    Code:
    }while(QA>2||QA<1);
        }while(QA==1 && T2<[COLOR=Red]=[/COLOR]1000); [COLOR=Red]//i am not sure but i think you must remove =   [/COLOR]     
      getche();
    }
    
    
     
  10. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    I figured it out already..hehe the only thing that is needed to be change is this

    Code:
      T1=300-inc;
      T2=300;
      end1=1000-inc;
    
    Program
    Code:
    //This program generates a table for the specific internal energy
    //and the specific enthalpy of Methane from 300K to 1000K using the
    //specific heat variations listed on Table A.21
    
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
      int QA;
      float T1=300,T2,end1,inc;
      double a=3.826,b=-3.979,c=24.558,d=-22.733,e=6.963;
      double A,B,C,D,E,H,U,R=8.3143,M=16.04,i=10;
      char error[128];
      
      do{
      do{
        system("cls");
        printf("Enter the Increment in Temperature: ");
        scanf("%s",&error);
        inc=atof(error);
        if(inc<=0){
          system("cls");
          printf("Invalid Input. Please try again!!!\n");
          printf("Please press the spacebar to continue!!");
          getche();
          }
        }while(inc<=0);
      T2=T1+inc;
      end1=1000-inc;
      printf("The increment is %.3f\n\n",inc);
      printf("\nInitial Temperature\tFinal Temperature\tEnthalpy\tInternal Energy\n");
      while(T1<=end1){
        T1+=inc;
        T2+=inc;
        A=a*(T2-T1);
        B=(1/pow(i,3))*(b/2)*(pow(T2,2)-pow(T1,2));
        C=(1/pow(i,6))*(c/3)*(pow(T2,3)-pow(T1,3));
        D=(1/pow(i,9))*(d/4)*(pow(T2,4)-pow(T1,4));
        E=(1/pow(i,12))*(e/5)*(pow(T2,5)-pow(T1,5));
        H=(R/M)*(A+B+C+D+E);
        U=(R/M)*((A+B+C+D+E)-(T2-T1));
        printf("%10.3f K\t\t%10.3f K\t\t%.3f kJ/kg\t%.3f kJ/kg\n",T1,T2,H,U);
        }
      do{//This used to ask the user if s/he wants to repeat the evaluation
        printf("\n\nDo you want to repeat the computation or Quit?\n");
        printf("[1] Yes\n");
        printf("[2] Quit\n");
        scanf("%s",&error);
        QA=atoi(error);
        switch(QA){
          case 1:
            break;
          case 2://Return to the selection screen
            exit(1);
            break;
          default://The program redirects invalid inputs to this part
            system("cls");
            printf("Invalid input. Please try again!!!\n");
            printf("Please press the spacebar to continue!!");
            getche();
            break;   
            }
          }while(QA>2||QA<1);
        }while(QA==1);         
      getche();
    }
    
     

Share This Page