Click here to Skip to main content
15,891,846 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
#include<stdio.h>
#include<math.h>
int main(void)
{
   FILE*fmyfile;
   float a;
   float sq;
   float sqr;

   fmyfile = fopen("myfile.dat","w");
   if(fmyfile == NULL)
   {
    printf("failed to open datafile");
    exit (0);
   }
   else
   {
    printf("%7s%8s%15s\n","number","squares","square roots");
    for(a = 1; a <= 10; a++)
    {
    printf("%7.0f%8.2f%15.2f\n",a,pow(a,2),sqrt(a));
    fprintf(fmyfile,"%f",a);
    fprintf(fmyfile,"%f",pow(a,2));
    fprintf(fmyfile,"%f",sqrt(a));
    }

   }
fclose(fmyfile);
}


this is my program of writes a table of squares and square roots to the file myfiledat. Starts the table from number 1 to 10. i encounter a error. May i know where i my error?
Posted
Comments
Manfred Rudolf Bihy 3-May-11 13:31pm    
What is the error you are hitting?
Jayfam 3-May-11 13:33pm    
problem solve..no error already...thanx
Manfred Rudolf Bihy 3-May-11 13:48pm    
Great that you could work it out!
Jayfam 3-May-11 13:49pm    
thank you

1 solution

Would this work:

C#
#include<stdio.h>
#include<math.h>
int main(void)
{
    FILE*fmyfile;
    float a;
    float sq;
    float sqr;

    fmyfile = fopen("myfile.dat","w");
    if(fmyfile == NULL)
    {
        printf("failed to open datafile");
        exit (0);
    }
    else
    {
        printf("%7s%8s%15s\n","number","squares","square roots");
        for(int idx = 1; idx <= 10; idx++)
        {
            a  = 1.0f * idx;
            sq = pow(a,2);
            sqr= sqrt(a);  
            printf("%7.0f%8.2f%15.2f\n",a,sq,sqr);
            fprintf(fmyfile,"%f",a);
            fprintf(fmyfile,"%f",sq);
            fprintf(fmyfile,"%f",sqr);
        }
    }
    fclose(fmyfile);
}


Does this do the trick?

Regards,

-MRB
 
Share this answer
 
v2
Comments
Jayfam 3-May-11 13:40pm    
mine coding no problem already. Thanks for your coding too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900