Thread: Help needed c programming

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Help needed c programming

    Hello all,

    I have been given nineteen files, which represent voltage tests on AC electricity supplies. The RMS value of a satisfactory supply should be more than 65. I have to write a program which reads in the data, works out the number of samples, the RMS and maximum amplitude, and prints out the results. the samples of data are in .txt files. I have tried to create a code to open one files and read in the samples but am not having much luck. Any suggestions on codes to do this?

    code:

    Code:
    #include <stdio.h>
    int main (void)
    {
      FILE *input1;
      int counter = 0;
      float total = 0;
      int samples;
      int i;
      float n;
      
      input1 = fopen("data-01.txt", "r");
      if (input1 == NULL)
      {
        fprintf(stderr, "data-01.txt : Can't open file.\n");
      }
      
        else
        {   
          while (fscanf(input1, "%f", &n) == 1)
          {
            total += n;
            counter++;
          }
      
          fclose(input1);
        
      {
          char data01[20];
          sprintf(data01, "%d samples, Amplitude =  RMS = ", counter);
          printf("%s", data01);
      }
    }
      system("pause");
      return 0;
    }
    I'll be very grateful for any help you may offer on the matter!

    Kind Regards,

    David B.
    Last edited by d.bissell; 11-27-2010 at 03:53 PM. Reason: no code tagging

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    I am sorry, I'm fairly new to programming and this forum.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    char data01[20];
    sprintf(data01, "%d samples, Amplitude = RMS = ", counter);
    printf("%s", data01);

    So why didn't you just print the answer then?

    Your buffer is too small.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    I have fixed this problem and am now able to print the amount of samples and the maximum amplitude, however I am not sure where to start to put in some code to calculate the RMS value of all of the samples.

    Code:
    #include <stdio.h>
    int main (void)
    {
      FILE *input1;
      int counter = 0;
      float total = 0;
      int samples;
      int i;
      float n;
      float max_amplitude = 0;
      
      input1 = fopen("data-01.txt", "r");
      if (input1 == NULL)
      {
        fprintf(stderr, "data-01.txt : Can't open file.\n");
      }
        else
        {   
          while (fscanf(input1, "%f", &n) == 1)
          {
            total += n;
            counter++;
          }
      
      fclose(input1);
      {
          
          for(i = 0; i < counter; i++)
          {
                if(n > max_amplitude)
                {
                   max_amplitude = n;
                }
    }
          
          char data01[20];
          sprintf(data01, "%d samples, Amplitude = %f  RMS = ", counter, max_amplitude);
          printf("%s", data01);
      }
      
      
    }
      system("pause");
      return 0;
    }
    do you know how to calculate the RMS value of a large amount of samples?

    Thanks

    David
    Last edited by d.bissell; 11-27-2010 at 05:13 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    For calculating RMS... are the values given to you expressed as Peak, Peak to Peak or are they already RMS values?
    Most measuring equipment is calibrated to provide RMS voltages.

    RMS = peak * .707
    RMS = PeakToPeak * .3535

    Peak = RMS * 1.414

    The math is simple to encode.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    char data01[20];
    sprintf(data01, "%d samples, Amplitude = RMS = ", counter);
    printf("%s", data01);

    So why didn't you just print the answer then?

    Your buffer is too small.
    Did you even pay attention???

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    == Welcome to the forum, David! ==


    This loop needs to be sent to the gym for a work out:
    Code:
          while (fscanf(input1, "%f", &n) == 1)
          {
            total += n;
            counter++;
          }
    One puny n variable just won't hold all the data. You can either:

    1) process the data one by one, inside this loop (which will make it a bit complicated), or

    2) put the data into an array of floats, and process the other parts as you need to (simpler by far).

    Code:
        counter=0;
        while (fscanf(input1, "%f", &data01[counter]) == 1)
          {
            total += n;
            counter++;
          }
    Remembering that your last valid data will be at data01[counter-1], and not data01[counter], at the end of this loop.
    Last edited by Adak; 11-27-2010 at 09:54 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But why would they want to store all the samples, when the problem can be solved with counting and summation?

    Besides, after being totally ignored the first time around, I'm not going to bother suggesting anything else.
    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.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I thought he might have more work to do with the rms values.

    It appears that the "alligator ears" syndrome, is flourishing.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Salem, i did not mean to ignore you, i just didnt understand how to take your comments on board as the c programming language is still very new to me.

    I have now solved my problem.

    Thank you all for your kind help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 12-08-2009, 07:39 AM
  2. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  3. Urgent Help Needed In Writing Algorithm!!
    By Vikramnb in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2009, 12:46 PM
  4. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  5. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM

Tags for this Thread