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

Go4Expert Member
5Mar2010,11:32   #1
askmewhy25's Avatar
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 by askmewhy25; 5Mar2010 at 12:00..
Go4Expert Member
5Mar2010,12:43   #2
karthigayan's Avatar
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);
    }
}
askmewhy25 likes this
Skilled contributor
5Mar2010,12:45   #3
techgeek.in's Avatar
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;
}
askmewhy25 likes this
Go4Expert Member
5Mar2010,12:53   #4
askmewhy25's Avatar
Thanks techgeek.in but i have another question.. if the user wanted to input numbers with decimal numbers how will i do that??
Pro contributor
5Mar2010,13:26   #5
virxen's Avatar
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 by virxen; 5Mar2010 at 13:30..
askmewhy25 likes this
Skilled contributor
5Mar2010,17:16   #6
techgeek.in's Avatar
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;
}
askmewhy25 likes this
Go4Expert Member
5Mar2010,17:28   #7
askmewhy25's Avatar
Now I upgraded the program.. 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();
}
Skilled contributor
5Mar2010,21:49   #8
techgeek.in's Avatar
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();
}
Pro contributor
5Mar2010,22:59   #9
virxen's Avatar
Quote:
Originally Posted by techgeek.in View Post
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();
}
correct the above code like this

Code:
.......................
    switch(QA){
      case 1:
            T1=300;//add this
            T2=0;//add this
        break;
      case 2://Return to the selection screen
        exit(1);
......................
Code:
}while(QA>2||QA<1);
    }while(QA==1 && T2<=1000); //i am not sure but i think you must remove =        
  getche();
}
askmewhy25 likes this
Go4Expert Member
6Mar2010,08:22   #10
askmewhy25's Avatar
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();
}