i've written these codes to evaluate a total heat transfer.but there are some problem arrived

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    int *temp,*thick,*area,*cond,total_temp,n,i;
    double q,total_other;
    temp=(int *)malloc(20 * sizeof(int));
    thick=(int *)malloc(20 * sizeof(int));
    area=(int *)malloc(20 * sizeof(int));
    cond=(int *)malloc(20 * sizeof(int));
    printf("how many slabs?");
    scanf("%d",&n);

    for(i=0;i<=n;i++){
                      scanf("%d",&temp[i]);
                      }

    for(i=0;i<n;i++){
                     scanf(" %d %d %d",&thick[i],&area[i],&cond[i]);
                     }
    for(i=0;i<=n;i++){
                      printf("temp[%d]=%d",i,temp[i]);
                      }

    for(i=0;i<n;i++){
                     printf(" thick[%d]=%d,area[%d]=%d,cond[%d]=%d",i,thick[i],i,area[i],i,cond[i]);
                     }

    store_temp=0;
    for(i=0;i<n;i++){
                     total_temp=store_temp+(temp[i]-temp[i+1]);
                     }

    store_other=0;
    for(i=0;i<n;i++){
                     total_other=store_other+(thick[i]/(cond[i] * area[i]));
                     }

    q = (store_temp/store_other);
    printf("transfered heat, q = %f",q);
    getch();
}

my first question is here-

for(i=0;i<n;i++){
                         printf(" thick[%d]=%d,area[%d]=%d,cond[%d]=%d",i,thick[i],i,area[i],i,cond[i]);
                         }

i've to write 'i' one after another to print my values.this is annoying.is there any shorter form to perform this task?

and the final 'q' is a double value.but its showing some abnormal values during compilation.why is that happening?

thanks!!!!

Recommended Answers

All 11 Replies

  1. There is not other option in your printf statement, the only way to output a value multiple times is to put in multiple times on the parameter list.

  2. Not surprised that q has odd values. You have not included a definition of store_temp or store_other but they both are only ever assigned the value 0. Sure you should have total_temp and total_other respectively insead?

As an aside: given that you are always allocating the same amount of memory for each of the arrays, and you never free() them, is it really necessary to use dynamic allocation for the arrays? What I mean is, with the code as it is now, you could just as easily declare them as

int temp[20],thick[20],area[20],cond[20],total_temp,n,i;

Is there some reason for dynamically allocating them that I'm simply missing?

@banfa actually what do you mean by include a definition. how can i make q work properly???
@Schol-R-LEA there was no special reason behind using dynamic memory allocation!!

Actually I think I should have used 'declaration' not 'definition'.

However what I mean is that you use the variable store_temp but no-where is there a line int store_temp or something similar. The variables store_temp and store_other are not declared so in theory your code does not compile.

Even if you did declare them they are only ever assigned the value 0 so q is assigned the value 0/0 which actually I might expect to cause some sort of crash at least some of the time.

Just noticed that you wrote

and the final 'q' is a double value.but its showing some abnormal values during compilatio.
So that through me off because you don't normally get values during complation, do you mean you got error messages? If you got compilation errors then post them.

actually it compiles and q shows the values 1.soef# something like that
// and previously it was store_temp and while posting in this forum i changed it to total_temp for better understanding .
// you are saying i initialized the values with zero,but additional values are added with it while it passed through the loop.what is the problem here??
but the result is the same .q prints gurbage values.
what changes i should make to make it right . that total_temp/store_temp is not an issue here as, if i changed it ultimately the result is the same.
// read store_temp instead of total_temp. thats my mistake as i mentioned above
//similarly read store_other instead of total_other

i decided to re post the code here

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    int *temp,*thick,*area,*cond,store_temp,n,i;
    double q,store_other;
    temp=(int *)malloc(20 * sizeof(int));
    thick=(int *)malloc(20 * sizeof(int));
    area=(int *)malloc(20 * sizeof(int));
    cond=(int *)malloc(20 * sizeof(int));
    printf("how many slabs?");
    scanf("%d",&n);

    for(i=0;i<=n;i++){
                      scanf("%d",&temp[i]);
                      }

    for(i=0;i<n;i++){
                     scanf(" %d %d %d",&thick[i],&area[i],&cond[i]);
                     }
    for(i=0;i<=n;i++){
                      printf("temp[%d]=%d",i,temp[i]);
                      }

    for(i=0;i<n;i++){
                     printf(" thick[%d]=%d,area[%d]=%d,cond[%d]=%d",i,thick[i],i,area[i],i,cond[i]);
                     }

    store_temp=0;
    for(i=0;i<n;i++){
                     store_temp=store_temp+(temp[i]-temp[i+1]);
                     }

    store_other=0;
    for(i=0;i<n;i++){
                     store_other=store_other+(thick[i]/(cond[i] * area[i]));
                     }

    q = (store_temp/store_other);
    printf("transfered heat, q = %f",q);
    getch();
}

OK that code makes sense.

Assuming that n != 0 then 1.soef# looks like some error has happened in the maths. I would suggest that you output the values of store_temp and store_other to see what they are.

One possible error that suggests itself is from line 37

    store_other=store_other+(thick[i]/(cond[i] * area[i]));

Thick, cond and area are all ints which means that the bracketed section at the end of the line will be done as integer division. If cond[i]*area[i] is always greater than thick[i] this means that the result of (thick[i]/(cond[i] * area[i])) will always be 0. In that case you will always be adding 0 to store_other and since your divide by store_other you will be getting a divide by zero error (causing the 1.soef#). Even if store_other is not ending up as 0 it is going to end up having an integer value rather than being the sum of decimal values suggested by the code line.

If this is the problem then you can solve it by changing line 37 to

    store_other=store_other+(double(thick[i])/(cond[i] * area[i]));

The cast of the denominator (thick[i]) forces the whole calculation to be done as floating point arithmetic.

It is important when mixing integers and floating point types to make sure you do your calculation with the correct type.

thanks a lot .code properly worked at last.

store_other=store_other+(double(thick[i])/(cond[i] * area[i]));

this type of type casting was not known to me earlier.

Opps made another mistake, this

store_other=store_other+(double(thick[i])/(cond[i] * area[i]));

should not work in a C compiler because double(thick[i]) is not a cast it is instantiating a double object through its constructor which takes an initialiser which is C++. A true C version of this line is

store_other=store_other+(((double)thick[i])/(cond[i] * area[i]));

which actualy does a cast to double.

thanks.actually i cant get rid of this type of problems.if you see this code in a similar way ive done the type cast things but printed values are just nonsence.how to get rid of this type of troubles???

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    int x[10],p,i,n,m,k,j;
     double y[6][5];
    printf("how many numbers?");
    scanf("%d",&n);
    for(i=0;i<n;i++){
                     scanf("%d",&x[i]);
                     }
    i=0;
    j=0;
    while(j<n){
               scanf("%d",&y[j][i]);
               j++;
               }


    for(i=0;i<n;i++){
                     printf("x[%d]=%d",i,x[i]);
                     }
                 i=0;j=0;
    printf("\n");
     while(j<n){
                        printf("y[%d][%d]=%d",j,i,y[j][i]);
                          j++;

                          }  
    printf("\n");
                     k=0; 
                     m=1;    
      for(i=1;i<n;i++){
                         for(j=0;j<n-i;j++){
                                            y[j][i]= (double)((y[j+1][i-1]-y[j][i-1])/(x[k+m]-x[k]));
                                            k++;

                                            }
                                            k=0;
                                            m=1+m;
                                            }
      printf("\n");

         for(i=0;i<n-1;i++){
                            for(j=0;j<n-i;j++){
                                               printf("y[%d][%d]=%f",j,i,y[j][i]);
                                               printf("\n");
                                               }}

                                               getch();
                                               }         
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.