Thread: help in writing a C-Progrm

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    help in writing a C-Progrm

    Dear members,
    i am new to this forum...in fact i am not professionally a C-Programmer..I need a little help in writing a C-Program...
    I have a file (like notepad or wordpad) which contains 3 columns...each column separated by 'tab'...
    All 3 columns contains: may be positive or negative number (for e.g., 0.3345, -0.3119, 1.6654, sometimes up to 7 decimal points, etc) - (say 60 numbers each columns)...

    Now i want to create another file using C as follows:
    column 1: (each number in column 1 in my input file multiplied by 'a')
    +(each number in column 2 in my input file multiplied by 'm')
    +(each number in column 3 in my input file multiplied by 'n')

    column 2: (each number of column 2 in my input file multiplied by 'g'
    +(each number of column 3 in my input file multiplied by 'h'

    column 3: (each number of column 3 in my input file multiplied by 't')

    'a'= A positive number up to 7 decimal points
    'm'= b*cosine(gamma)
    'n'=c*cosine(beta)
    'g'=b*sine(gamma)

    'h'=(c/sine(gamma))*(cos(alfa)-((cos(beta)*cos(gamma))

    't'=square root of [(c^2)-(n^2)-(h^2)]

    b and c = positive number up to 7 decimal points
    alfa, beta and gamma are angles in degrees up to 6 decimal points...

    can any kind person help me to make such a huge C-program!!!!Actually i did it with Excel...but...really a horrible job for me to do it for many files...C-program should be better and easy....
    i know this should be a big work.......
    please help me....I know only very little C-program....but i can edit your program for my future changes.....if you want to know some more info..pls. ask me

    Advance thanks,
    Kumar.

  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    no

    Hi, i dont have any programming experience...just started 1 week before...just for this work which i posted.... i started reading....
    Kumar

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Well i dont hnow what exactly you want to calc. I coded a bit, but i am not responsible for what the resulst are or what you really want to do with it. Look at the code and tell me what you dont understand.
    Code:
    //---------------------------------------------------------------------------
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    
    #define PI 3.14159265
    
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    //Functions Prototypes.
    double mFunction(double b, double gamma)
    {
    	return b * cos(gamma * PI / 180);
    }
    double nFunction(double c, double beta)
    {
    	return c * cos(beta * PI / 180);
    }
    double gFunction(double b, double gamma)
    {
    	return b * sin(gamma * PI / 180);
    }
    double SquareRootT(double c, double n, double h)
    {
    	double R = pow(c,2) - pow(n,2) - pow(h,2);
    	if(R < 0)
    		R = -R;
    	return sqrt(R);
    }
    double hFunction(double c, double alfa, double beta, double gamma)
    {
    	return (c/ sin(gamma * PI / 180)) * (cos(alfa * PI /180)) - (cos(beta * PI / 180) * cos(gamma * PI / 180));
    }
    
    void CreateNewFile(char *oldFile, char *nFile,double a, double b, double c, double alfa, double beta, double gamma)
    {
    	if(!oldFile || !nFile)
    	{
    		printf("Error, NULL parametres.\n");
    		return;
    	}
    	else
    	{
    		FILE *of = fopen(oldFile, "r");
    		FILE *nf = fopen(nFile, "w");
    		//Variables.
    		double Col1 = 0.0;
    		double Col2 = 0.0;
    		double Col3 = 0.0;
    		//Array to hold the numbers.
    		double arr[3] = {0.0,0.0,0.0};
    		if(!of)
    		{
    			fprintf(stderr, "Error can not open '&#37;s' file.\n", oldFile);
    			return;
    		}
    		if(!nf)
    		{
    			fprintf(stderr, "Error can not open '%s' file.\n", nFile);
    			return;
    		}
    		while(fscanf(of,"%lf\t%lf\t%lf\n", &arr[0], &arr[1], &arr[2]) != EOF)
    		{
    			//Weird numbers.
    			Col1 = a * arr[0] + arr[1] * mFunction(b, gamma) + arr[2] * nFunction(c, beta);
    			Col2 = arr[1] * gFunction(b, gamma) + arr[2] * hFunction(c, alfa, beta, gamma);
    			Col3 = arr[2] * SquareRootT(c, nFunction(c, beta), hFunction(c, alfa, beta, gamma));
    
    			//Got the numbers write them down to file.
    			fprintf(nf,"%lf\t%lf\t%lf\n", Col1,Col2,Col3);
    			//Zero the values for next iteration.
    			arr[0] = 0.0;
    			arr[1] = 0.0;
    			arr[2] = 0.0;
    		}
    		fclose(nf);
    		fclose(of);
    	}
    
    }
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
    	CreateNewFile("numbers.txt", "result.txt", 1.234, 3.456, 3.4534, 2.56, 4.78, 8.98);
    	printf("Hit any key to continue......");
    	getch();
    	return 0;
    }
    //---------------------------------------------------------------------------
    If you run the program it will create a new file in the debug folder that will hold the results. Have a look at them and tell me,
    Bokarinho!!!
    /Edit: Dont forget that the numbers.txt is the input file.
    Last edited by Bokarinho; 09-22-2007 at 04:41 PM. Reason: Forgot!!!

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    hi

    Hi Bokarinho,
    thanks...i have the correct converted values...i made it by excel...i think i can understand a bit of ur program...now i am in home...may be in 2 hrs will go to my inst. and test and tell u the results....actually my need is to convert Cartesian coordinates in to lattice coordinate...that my purpose...
    thanks again..
    Kumar

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    hi

    Hi Robwhit,
    i am interested in learning...next month planned to buy a book by yeswhat kanetkar...

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    hi Bokariho

    Hi got a problem..when i compile, it says 'lattice.c:5:19: error: conio.h: No such file or directory.
    My operating system is suse linux...I copied ur program in to a file and named as lattice.c
    and when i compiled....using cc -lm -o ~/bin/lattice lattice.c [here lattice is my exe file and lattice.c is your source code....so that exe file will be copied in to my bin folder]...
    can you suggest me what to do!!!!
    Kumar

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    - remove all the #pragma lines, they're of no use to you
    - remove the conio.h header file
    - remove the printf and getch lines from main

    Changing all the
    // this is a comment
    into
    /* this is a comment */
    would be a good idea as well, since this is supposed to be a C program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    hi again...after searching some details for conio.h....i just removed #include <conio.h> and replaced getch by getchar....it worked fine...is this correct????also i replaced all 6 numbers in 'pragma argsused' by a,b,c,alfa,beta,gamma (in order)...i checked with my excel file...its absolutely correct for first 2 columns...but 3rd column is somewhat near to my value...but all r correct!....
    i just want to know whether i am correct with conio.h or not!!!
    however i got corect values..
    Now i am trying to modify ur program...so that one can enter input file and result file and a,b,c,alfa,etc values from the command line....
    thanks again
    Kumar

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Thats an easy part so do it yourself, remember that this was the first and the last time you get something done so try to regard it and learn something from it.
    Any further probs tell us,
    Bokarinho!!!

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    Hi Bokarinho..i need ur email address...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. Writing Bitmap Files
    By HappyDude in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 05:48 PM