Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to have to variable looping at the same time with the same increment? (http://www.go4expert.com/forums/variable-looping-time-increment-t21205/)

askmewhy25 5Mar2010 11:32

How to have to variable looping at the same time with the same increment?
 
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();
}


karthigayan 5Mar2010 12:43

Re: How to have to variable looping at the same time with the same increment?
 
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);
    }
}


techgeek.in 5Mar2010 12:45

Re: How to have to variable looping at the same time with the same increment?
 
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 5Mar2010 12:53

Re: How to have to variable looping at the same time with the same increment?
 
Thanks techgeek.in but i have another question..:happy: if the user wanted to input numbers with decimal numbers how will i do that??

virxen 5Mar2010 13:26

Re: How to have to variable looping at the same time with the same increment?
 
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

techgeek.in 5Mar2010 17:16

Re: How to have to variable looping at the same time with the same increment?
 
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 5Mar2010 17:28

Re: How to have to variable looping at the same time with the same increment?
 
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();
}


techgeek.in 5Mar2010 21:49

Re: How to have to variable looping at the same time with the same increment?
 
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();
}


virxen 5Mar2010 22:59

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

Originally Posted by techgeek.in (Post 65185)
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 6Mar2010 08:22

Re: How to have to variable looping at the same time with the same increment?
 
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();
}



All times are GMT +5.5. The time now is 03:56.