C newbie-need help

This is a discussion on C newbie-need help within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm very new to C programming and am trying to learn because I'm in molecular biology (x-ray crystallography) ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    Question C newbie-need help

    Hi guys, I'm very new to C programming and am trying to learn because I'm in molecular biology (x-ray crystallography) and being able to write simple programs is very useful for me.
    here's the question:
    I want to write a program that reads in a file having two columns of numbers. it will save each column of numbers in a separate array and then perform a math operation on the two paired numbers per row i.e. the data is:
    2 1
    3 4
    2 5
    2 8
    9 10
    it would read this file save the numbers in arrays and perform a math operation for each pair (i.e 2 1, 3 4, 2 5).
    Once the operation is comuted it will write two new numbers from the math operations out into a new file.

    Thanks for any help in advance.
    Rob

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and what have you tried so far?

    Lookup:
    fopen()
    fgets()
    strtol()
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Post something you've done, so we can guage what level of help you need.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    6
    Here's waht I've written so far:
    /*Converts coordinates between Cartesian and Polar */

    #include <stdio.h>
    #include <math.h>
    main()
    {

    FILE *InFile;
    FILE *OutFile;
    char InFileName[50];
    char OutFileName[50];
    int cmd;
    float x, y, r, theta;
    printf("***Welcome to my Coordinate Converting Progam***\n");
    printf("Input file name: ");
    scanf("%s", InFileName);
    printf("Output file name: ");
    scanf("%s", OutFileName);
    printf("Conversion type: 1=cartesian to polar, 2=polar to cartesian, 3=end program \n");
    for (; {
    printf("Enter command: ");
    scanf("%d", &cmd);
    switch (cmd) {
    case 1:
    InFile = fopen(InFileName, "r");
    fscanf(InFile, "%f %f", &x, &y);
    r = sqrt(x*x+y*y);
    theta = atan (y/x);
    OutFile = fopen(OutFileName, "w");
    fprintf(OutFile, "\n r = %f\n theta = %f", r, theta);
    fclose(InFile);
    fclose(OutFile);
    break;
    case 2:
    InFile = fopen(InFileName, "r");
    fscanf(InFile, "%f %f", &r, &theta);
    x = r * cos (theta);
    y = r * sin (theta);
    OutFile = fopen(OutFileName, "w");
    fprintf(OutFile, "\n x = %f\n y = %f", x, y);
    fclose(InFile);
    fclose(OutFile);
    break;
    case 3:
    printf("Thank you for using Coordinate Converter\n");
    return 0;
    default:
    printf("Please input an option from the menu\n");
    printf("Conversion type: 1=cartesian to polar, 2=polar to cartesian, 3=end program \n");
    break;
    }
    }
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    and another one to read:
    << !! Posting Code? Read this First !! >>

    Now, which part of the program is giving you trouble?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    6

    sorry

    Hammer:
    Sorry I keep I am sillyI am sillyI am sillyI am sillying up the basics, it's just that I'm getting frustrated writing this code (I know it's pathetic).
    It seems apparent to me that I'm going to have to input a while loop to keep reading in values from the input file but where to insert the loop is my major problem.
    What I just posted compiles fine so I didn't want to mess with it too much before asking for help.
    Thanks again

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    6
    Here's the code with code tags:
    Code:
     /*Converts coordinates between Cartesian and Polar */
    
    #include <stdio.h>
    #include <math.h>
    main()
    {
      
      FILE *InFile;
      FILE *OutFile;
      char InFileName[50];
      char OutFileName[50];
      int cmd;
      float x, y, r, theta;
      printf("***Welcome to my Coordinate Converting Progam***\n");
      printf("Input file name: ");
    	 scanf("%s", InFileName);
    	printf("Output file name: ");
    	 scanf("%s", OutFileName); 
      printf("Conversion type: 1=cartesian to polar, 2=polar to cartesian, 3=end program \n");
      for (;;) {
        printf("Enter command: ");
        scanf("%d", &cmd);
        switch (cmd) {
          case 1:
            InFile = fopen(InFileName, "r");
    	fscanf(InFile, "%f %f", &x, &y);
    	r = sqrt(x*x+y*y);
    	theta = atan (y/x);
    	OutFile = fopen(OutFileName, "w");
    	fprintf(OutFile, "\n r = %f\n theta = %f", r, theta);
    	fclose(InFile);
    	fclose(OutFile);
    	break;
          case 2:
            InFile = fopen(InFileName, "r");
    	fscanf(InFile, "%f %f", &r, &theta);
    	x = r * cos (theta);
    	y = r * sin (theta);
    	OutFile = fopen(OutFileName, "w");
    	fprintf(OutFile, "\n x = %f\n y = %f", x, y);
    	fclose(InFile);
    	fclose(OutFile);
    	break;
          case 3:
          printf("Thank you for using Coordinate Converter\n");
          return 0;
          default:
          printf("Please input an option from the menu\n");
          printf("Conversion type: 1=cartesian to polar, 2=polar to cartesian, 3=end program \n");
          break;
        }  	
      }
    }

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    6

    Please help?

    help...?

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    intead of using an infinite for loop use a
    Code:
    while (cmd != 3){...
    Its personal preference really but I think infinte for loops are bad practice.

  10. #10
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    This could be written in multiple ways.

    This leads me to a few questions:

    Q. 1) After you open the file, do you want to set the data to its own array per row? Speaking of which would look like:

    2 1
    3 4
    2 5
    2 8
    9 10

    As [2] is on row one and is the first index and [1] is also on row one but is index 2. Also, [3] is row two and index one while [4] is row two and index two?

    Q. 2) Once you get the values, and do your operation, are you wanting to write the new data to a file?


    I have done a similar program like this, excluding the mathematical operation and writing it to a new file, which is quite simple. Though, I would recommend using the C string library <string.h> for accomplishing this.

    First I would write a funciton that splits the data from the file to its own lines, then read each line splitting per line to two numbers.

    All you need is strchr(), just search for a new line, take the string and split it writing it to its own char[][] where that can hold words per array index.

    I have some code examples if you'd like to see.


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Code:
            InFile = fopen(InFileName, "r");
    	fscanf(InFile, "%f %f", &x, &y);
    	r = sqrt(x*x+y*y);
    	theta = atan (y/x);
    	OutFile = fopen(OutFileName, "w");
    	fprintf(OutFile, "\n r = %f\n theta = %f", r, theta);
    	fclose(InFile);
    	fclose(OutFile);
    My guess is, you want to process the whole file, not just repeatedly the first line of the file

    Code:
            InFile = fopen(InFileName, "r");
    	OutFile = fopen(OutFileName, "w");
    	while ( fscanf(InFile, "%f %f", &x, &y) == 2 ) {
    		r = sqrt(x*x+y*y);
    		theta = atan (y/x);
    		fprintf(OutFile, "\n r = %f\n theta = %f", r, theta);
    	}
    	fclose(InFile);
    	fclose(OutFile);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    6
    thanks for the help guys, I'll give it a try and get back to you if I'm still having problems.
    Sand_man, I agree with the infinte for loop comment, just seemed easier for some reason.

  13. #13
    ---
    Join Date
    May 2004
    Posts
    1,379
    you dont need to listen to me im just a drunk n00b

  14. #14
    Nosepicker DrakkenKorin's Avatar
    Join Date
    Nov 2001
    Posts
    407
    may also want to error trap, esp when opening and closing files
    DrakkenKorin

    Get off my Intarweb!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 07:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 08:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21