-1

Can somebody tell me what is wrong with my code?

#include <stdio.h>
#include <math.h>


int main(void) {

   int nSet=0;
   int n1,n2;
   int sum;
float hm,gm,avg,prod;

printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);

for(;nSet<2;nSet++){

   int sum=n1+n2;
   float prod=n1*n2;
   float hm=nSet/(1/n1+1/n2);
   float gm=sqrt(n1+n2);
   float avg=(n1+n2)/2;


              }

    printf("Sum: %d\n",sum);
    printf("Product: %4.2f\n",prod);
    printf("Average: %4.2f\n",avg);
    printf("Geometric mean: %4.2f\n",gm);
    printf("Harmonic mean: %4.2f\n",hm);

     return 0;         
}

originally by initializing in for loop i got 0 as every answer but atleast that printed something. I have to use loops to find the answers and i dont see why for loop would'nt work.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Vpe Grill
  • 3
  • 2
  • 1
    Hint: In C `1/2` is `0`, whereas `1/2.` is `0.5`. – alk Oct 20 '18 at 09:16
  • You have to check if there is a division by zero :D https://stackoverflow.com/questions/2485547/how-to-check-if-there-is-a-division-by-zero-in-c – SegFault42 Oct 20 '18 at 09:35
  • OT: The formula the code uses to calculate the [Geometric Mean](https://en.wikipedia.org/wiki/Geometric_mean) is wrong. – alk Oct 20 '18 at 09:43
  • Just fixed both – Vpe Grill Oct 20 '18 at 09:59
  • Welcome to Stack Overflow! Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. But I think your question is still not answerable. **You** should [edit] your question now, to add missing details (see [mcve] ). Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Oct 20 '18 at 11:17
  • Hi GhostCat! not sure what you mean, people have already answered my question, thank you tho! – Vpe Grill Oct 20 '18 at 19:35

2 Answers2

1

In C if you declare a float and assign a value like 1/2 the result will be 0 because de expression 1/2 is evaluated. If you want to have a result as float put 1.0f/2 or 1/2.0f and will work. In case of having 2 variables and want a float you can do this: (float)n1/n2 or n1/((float)n2) if n1 and n2 are int. Another observation is in the for loop. If you declare your variables again in for they are local in the loop and outside the loop they don't exists.

These code will work:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    int nSet=0;
    int n1,n2;
    int sum;
    float hm,gm,avg,prod;

    printf("Enter two integers. \n");
    printf("n1 = ");scanf("%d",&n1);
    printf("n2 = ");scanf("%d",&n2);

    for(;nSet<2;nSet++)
    {
        sum=n1+n2;
        prod=n1*n2;
        hm=nSet/(1.0f/n1+1.0f/n2);
        gm=sqrt(n1+n2);
        avg=(n1+n2)/2.0f;
    }

    printf("Sum: %d\n",sum);
    printf("Product: %4.2f\n",prod);
    printf("Average: %4.2f\n",avg);
    printf("Geometric mean: %4.2f\n",gm);
    printf("Harmonic mean: %4.2f\n",hm);

    return 0;
}
mariusd96
  • 95
  • 5
0

You are declaring new var in for scope. When the program exit from the loop all of the var created in for scope will be destroyed.

Your code corrected :

#include <stdio.h>
#include <math.h>

int main(void) {

int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;

printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);

for(;nSet<2;nSet++){
    sum=n1+n2;
    prod=n1*n2;
    hm=nSet/(1/n1+1/n2);
    gm=sqrt(n1+n2);
    avg=(n1+n2)/2;
}

printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);

return 0;
}

It's work much better now :)

Enter two integers: 10
1
Sum: 11
Product: 10.00
Average: 5.00
Geometric mean: 3.32
Harmonic mean: 1.00
SegFault42
  • 56
  • 3