Search Results

Search found 38 results on 2 pages for 'ycalleecharan'.

Page 2/2 | < Previous Page | 1 2 

  • Elapsed time of running a C program

    - by yCalleecharan
    Hi, I would like to know what lines of C code to add to a program so that it tells me the total time that the program takes to run. I guess there should be counter initialization near the beginning of main and one after the main function ends. Is the right header clock.h? Thanks a lot... Update I have a Win Xp machine. Is it just adding clock() at the beginning and another clock() at the end of the program? Then I can estimate the time difference. Yes, you're right it's time.h. Here's my code: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <share.h> #include <time.h> void f(long double fb[], long double fA, long double fB); int main() { clock_t start, end; start = clock(); const int ARRAY_SIZE = 11; long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE); int i; long double A, B; if (z == NULL) { printf("Out of memory\n"); exit(-1); } A = 0.5; B = 2; for (i = 0; i < ARRAY_SIZE; i++) { z[i] = 0; } z[1] = 5; f(z, A, B); for (i = 0; i < ARRAY_SIZE; i++) printf("z is %.16Le\n", z[i]); free(z); z = NULL; end = clock(); printf("Took %ld ticks\n", end-start); printf("Took %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC); return 0; } void f(long double fb[], long double fA, long double fB) { fb[0] = fb[1]* fA; fb[1] = fb[1] - 1; return; } Some errors with MVS2008: testim.c(16) : error C2143: syntax error : missing ';' before 'const' testim.c(18) :error C2143: syntax error : missing ';' before 'type' testim.c(20) :error C2143: syntax error : missing ';' before 'type' testim.c(21) :error C2143: syntax error : missing ';' before 'type' testim.c(23) :error C2065: 'z' : undeclared identifier testim.c(23) :warning C4047: '==' : 'int' differs in levels of indirection from 'void *' testim.c(28) : error C2065: 'A' : undeclared identifier testim.c(28) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data and it goes to 28 errors. Note that I don't have any errors/warnings without your clock codes. LATEST NEWS: I unfortunately didn't get a good reply here. But after a search on Google, the code is working. Here it is: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <share.h> #include <time.h> void f(long double fb[], long double fA, long double fB); int main() { clock_t start = clock(); const int ARRAY_SIZE = 11; long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE); int i; long double A, B; if (z == NULL) { printf("Out of memory\n"); exit(-1); } A = 0.5; B = 2; for (i = 0; i < ARRAY_SIZE; i++) { z[i] = 0; } z[1] = 5; f(z, A, B); for (i = 0; i < ARRAY_SIZE; i++) printf("z is %.16Le\n", z[i]); free(z); z = NULL; printf("Took %f seconds\n", ((double)clock()-start)/CLOCKS_PER_SEC); return 0; } void f(long double fb[], long double fA, long double fB) { fb[0] = fb[1]* fA; fb[1] = fb[1] - 1; return; } Cheers

    Read the article

  • Need opinions on LaTeX and ever upgrading

    - by yCalleecharan
    Hi, I've been using LaTeX since 2005 with the TeXLive distribution and I've been upgrading as each new TeXLive distribution comes out. In the recent years I noticed an increase in new packages, updated packages and in one instance a new package bearing a different name replacing an old one by the same package author. A LaTeX document which relies heavily on packages and which has been produced a few years back may start to get some warnings and error messages on present-day LaTeX compilation. The primary reason I switched to LaTeX is because of its reliability and robustness to create big documents easily, not to mention the adorable typographic quality. With LaTeX one doesn't have to worry about how to open a docx in an old program supporting only doc for instance. Now, when there are so much continual changes in the packages in a LaTeX distribution, I tend to wonder when will this madness end. Not that having enhanced and new features are bad in packages, but not all updated packages are backward compatible. Eventually one would like to be able to compile a LaTeX file in 10 years time that he/she is working on at present and not get any compilation warnings/error messages due to some unpredictable behavior of updated packages or due to a package that has been cast-off from a LaTeX distribution. If I understand correctly CTAN do keep a database with all packages from different versions. I would like to know how you LaTeX users handle this issue. Thanks a lot...

    Read the article

  • fopen / fopen_s and writing to files

    - by yCalleecharan
    Hi, I'm using fopen in C to write the output to a text file. The function declaration is (where ARRAY_SIZE has been defined earlier): void create_out_file(char file_name[],long double *z1){ FILE *out; int i; if((out = fopen(file_name, "w+")) == NULL){ fprintf(stderr, "* Open error on output file %s", file_name); exit(-1); } for(i = 0; i < ARRAY_SIZE; i++) fprintf(out, "%.16Le\n", z1[i]); fclose(out); } My questions: On compilation with MVS2008 I get the warning: warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. I haven't see much information on fopen_s so that I can change my code. Any suggestions? Can one instruct fprintf to write at desired precision? If I'm using long double then I assume that my answers are good till 15 digits after the decimal point. Am I right? Thanks a lot...

    Read the article

  • static, define, and const in C

    - by yCalleecharan
    Hi, I've read that static variables are used inside function when one doesn't want the variable value to change/initialize each time the function is called. But what about defining a variable static in the main program before "main" e.g. #include <stdio.h> static double m = 30000; int main(void) { value = m * 2 + 3; } Here the variable m has a constant value that won't get modified later in the main program. In the same line of thought what difference does it make to have these instead of using the static definition: const double m = 30000; or #define m 30000 //m or M and then making sure here to use double operations in the main code so as to convert m to the right data type. Thanks a lot...

    Read the article

  • printing out posts

    - by yCalleecharan
    Hi, I'm using the Firefox browser and trying to print out my posts (and replies). The pdf output doesn't include the comments in the posts. How to make sure that everything which is on the page gets printed? Same goes for codes for which are in a window. Only the visible part of the code gets printed. These are not programming questions :) but I hope that someone can suggest how to get proper printouts of the posts. Thanks a lot...

    Read the article

  • C static variables and intialization

    - by yCalleecharan
    Hi, If I have a global static variable x like in this code #include <stdio.h> #include <stdio.h> static int x; int main(void) { DO SOMETHING WITH x HERE x++; } What will be difference if I opted to initialize x to a value first say as in static int x = 0; before entering "main"? In my first case where I didn't assign a value to x, does the compiler implicitly know that x is to be set to zero as it's a static variable? I heard that we can do this with static variables. Thanks a lot...

    Read the article

  • C++ Expression Templates

    - by yCalleecharan
    Hi, I currently use C for numerical computations. I've heard that using C++ Expression Templates is better for scientific computing. What are C++ Expression Templates in simple terms? Are there books around that discuss numerical methods/computations using C++ Expression Templates? In what way, C++ Expression Templates are better than using pure C? Thanks a lot

    Read the article

  • C function prototype: void f(). Is it recommended?

    - by ycalleecharan
    Hi, I'm learning C and I saw in a book that a function prototype has the form void f() and in the function declaration or in the calling function, the f function takes arguments. Thus In the function declaration we have something like void f(long double y[], long double A) and in the calling function is f(y, A). The function is doing operations on the array y i.e. when the function is called, some elements in the array y are changing. A is just a constant numerical value that doesn't change. I have two questions: If defining the function prototype at the top in the program as void f() a good practice? Or is it better to put it as void f(long double y[], long double A) as in the function declaration? The called function f is changing elements in the array y. Is void the right return type? The program is working fine as such with the void as described. Or should I change all my "voids" to "long double". I'm working with long double as I need as much precision as possible though on my machine both double and long double gives me 15 precision digits. Thanks a lot

    Read the article

  • sizeof, size_t and dtddef.h

    - by yCalleecharan
    Hi, if I'm using the sizeof operator and making use of size_t in my code, do I have necessarily have to include the preprocessor directive stddef.h ? I haven't included the stddef.h and my code compiles without warning with both MVS2008 and with Borland C++ BuilderX. Thanks a lot...

    Read the article

  • When to pass pointers in functions?

    - by yCalleecharan
    scenario 1 Say my function declaration looks like this: void f(long double k[], long double y[], long double A, long double B) { k[0] = A * B; k[1] = A * y[1]; return; } where k and y are arrays, and A and B are numerical values that don't change. My calling function is f(k1, ya, A, B); Now, the function f is only modifying the array "k" or actually elements in the array k1 in the calling function. We see that A and B are numerical values that don't change values when f is called. scenario 2 If I use pointers on A and B, I have, the function declaration as void f(long double k[], long double y[], long double *A, long double *B) { k[0] = *A * *B; k[1] = *A * y[1]; return; } and the calling function is modified as f(k1, ya, &A, &B); I have two questions: Both scenarios 1 and 2 will work. In my opinion, scenario 1 is good when values A and B are not being modified by the function f while scenario 2 (passing A and B as pointers) is applicable when the function f is actually changing values of A and B due to some other operation like *A = *B + 2 in the function declaration. Am I thinking right? Both scenarios are can used equally only when A and B are not being changed in f. Am I right? Thanks a lot...

    Read the article

  • Casting in mixed type calculations in C?

    - by yCalleecharan
    Hi, If I define these variables: double x0, xn, h; int n; and I have this mathematical expression: h = (xn - x0)/n; Is it necessary that I cast n into double prior doing the division for maximum accuracy like in h = (xn - x0)/ (double) n; I wrote a program to check the above but both expressions give the same answers. I understand that C will promote the integer to double type as variables xn and x0 are of type double but strangely enough in a book, the second expression with casting was emphasized. Thanks a lot...

    Read the article

  • Without width printing format in C

    - by yCalleecharan
    Hi, in C if I have a printf statement containing say "%.2f", it says that the precision is 2 digits after the decimal place. I haven't explicitly specify the width. I have two questions: Is this good programming practice?; Is without specifying the width means that the width of the field will get adjusted automatically when printing the number, irrespective of the number of digits it contains? Thanks a lot...

    Read the article

< Previous Page | 1 2