Thread: Using the outputted values from code

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Using the outputted values from code

    Hello there. I'm writing a program and I need to export values created from my code into Microsoft Excel. Here is my code:
    Code:
    int main()
    { 
          int i,j, zed;
          float x[1000],y[1000], u=0.25, m=0.5;
          double z[1000], w[1000], a[1000], b[1000];
    
          for(i=0;i<1000;i++)
             {
    
                  x[i] = 2*((float)rand()/(float)RAND_MAX)-1;
                  y[i] = 2*((float)rand()/(float)RAND_MAX)-1; 
                  a[i]= pow(x[i],2);
                  b[i]= pow(y[i],2);
                 w[i] = (a[i]+b[i]);
    
             }
         j=0;
         for(i=0;i<1000;i++)
    
         {
    
             if(w[i]<1){(z[j]=((u*x[i])*(sqrt((-1*2)*(log(w[i])/(w[i])))))+m);j++;}
    
         }
         zed = j;
         for(j=0;j<zed;j++)
           {
               printf("%f\t",z[j]);
           }
    
         return 0;
    }
    So I need all of the 'z[j]' array values to be exported into Excel. I've read about CSV files and fprintf but I couldn't find any good examples of their uses. Any help would be much appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your code is hard to read:
    • Include your header files so I don't have to screw around getting your code to compile.
    • Your indentation is crap. Read these, pick one of the top 3 styles from the Wikipedia article: Indentation - cpWiki and Indent style - Wikipedia, the free encyclopedia.
    • You need white space around your operators.
    • Don't put giant, ugly if statements on one line with their subordinate clauses. The if () part goes on one line, the statements inside the if go on their own lines.
    • Don't use magic numbers. #define NUM_ELEMENTS 1000, and replace all the uses of 1000 with NUM_ELEMENTS.

    On this very site, we have a great FAQ article on working with files: Cprogramming.com FAQ > Work with files (C), and we have a tutorial as well: C File I/O Tutorial - Cprogramming.com. Wikipedia has some good info on CSV files: Comma-separated values - Wikipedia, the free encyclopedia. You need to tell us what exactly you don't get about fprintf and CSV files.
    Last edited by anduril462; 09-13-2011 at 11:09 AM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    CSV files are just comma separated values. Newline indicates the next row, comma indicates the next column. So to create 1D column, write each element to a file, with one element per line.

    As for fprintf, there are tutorials and any book will explain how to use it. You should get a good book.

    Also, use descriptive variable names.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    Thankyou so much for your help, I went out today and got programming in C for dummies, was very helpful. I was able to write the array Z[j] to a txt file. However when I open it up in excel it comes up with a Text Import Wizard where I have to sort out columns to import the data. Is there a way to write the code so I don't have to go through this text import wizard?
    Here is my new code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define NUM_ELEMENTS 1000
    int main()
    {
        int i,j, zed;
        float x[ NUM_ELEMENTS],y[ NUM_ELEMENTS], u=0.25, m=0.5;
        double z[ NUM_ELEMENTS], w[ NUM_ELEMENTS], a[ NUM_ELEMENTS], b[ NUM_ELEMENTS];
    
    
        for(i=0;i<1000;i++)
            {
    
            x[i] = 2*((float)rand()/(float)RAND_MAX)-1;
            y[i] = 2*((float)rand()/(float)RAND_MAX)-1;
            a[i]= pow(x[i],2);
            b[i]= pow(y[i],2);
            w[i] = (a[i]+b[i]);
    
            }
        j=0;
        for(i=0;i<1000;i++)
    
            {
    
            if(w[i]<1)
                {(z[j]=((u*x[i])
                        *(sqrt((-1*2)
                               *(log(w[i])
                                    /(w[i])))))+m);
                                            j++;
                }
    
    
    
            }
            zed = j;
            for(j=0;j<zed;j++)
                {
                printf("%f\t",z[j]);
                }
    FILE *myfile;
    
        myfile = fopen("program.txt","w");
        if(!myfile)
        {
            puts("some kind of file error!");
            return(1);
        }
        zed = j;
            for(j=0;j<zed;j++)
                {
                fprintf(myfile, "%3d %f\n",j,z[j]);
                }
        fclose(myfile);
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Jonnyfurze View Post
    Thankyou so much for your help
    You're welcome.
    Is there a way to write the code so I don't have to go through this text import wizard?
    Nope. Excel does that whenever you import a delimited file. It forces you to say whether there's a header row, whether the file is comma delimited, tab delimited, etc. It has to know how to parse it, and you have to tell it. There's nothing you can do in your code to avoid that.

    Here is my new code:
    Much better! The only thing left I see to fix is your for loops. You need to use NUM_ELEMENTS in your for loops too. The idea of constants is that you use the name everywhere, and if you have to change the value, you only have to do it in one place, not 3.
    Code:
         for(i=0;i<NUM_ELEMENTS;i++)

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    If you call the output something like foo.csv and open it with excel, it should open without any prompts. I do it all the time. You can ether drag and drop the file or do "start foo.csv" from the command line or just do a file->open from excel and it opens as if it were any other spreadsheet. Be careful about editing and saving this file, though, since CSV can't handle a lot of the formatting you can do to a normal file. Use save as to save it as an xlsx/xlsb/xlsm/xls/whatever format if you want to modify and save the file for later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  2. Outputted as same as inputted
    By main() in forum C Programming
    Replies: 14
    Last Post: 05-12-2010, 03:08 PM
  3. Replies: 8
    Last Post: 03-26-2007, 05:48 PM
  4. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  5. Fraction outputted as decimal
    By blindman858 in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2005, 01:17 PM