Compiled code crashing

This is a discussion on Compiled code crashing within the C Programming forums, part of the General Programming Boards category; Have been trying to learn C and complete an assignment that gives the RMS of a set of input numbers ...

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

    Compiled code crashing

    Have been trying to learn C and complete an assignment that gives the RMS of a set of input numbers but the program crashes when run. Any advice appreciated on what's wrong with the code?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    
    
    double RMS(double *x, int N)
    
    
    {
    int i;
    double sum =0.0;
    for (i=0; i<N; i++)
    { 
    sum = sum + pow(x[i],2);
    }
    double question = sum/N;
    sqrt (question);
    return question;
    }
    int main()
    {
    int i, N;
    double question;
    
    
    double x [N];
    printf("Enter the number of values in the sequence please: \n");
    scanf("%d", &N);
    for (i=0;i<N;i++)
    {
    printf("Enter the numbers in the sequence please: \n");
    scanf("%lf", &x[i]);
    }
    question = RMS(x,N);
    printf(" The RMS of your selected numbers is: %f. \n", RMS);
    
    
    
    
    
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    102
    Quote Originally Posted by cMacD View Post
    Have been trying to learn C and complete an assignment that gives the RMS of a set of input numbers but the program crashes when run. Any advice appreciated on what's wrong with the code?
    Code:
    int i, N;
    double question;
    
    
    double x [N];
    First glance, the statement double x[N] happens before 'N' has any value.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    How could I change this so i doesn't give me errors, because when i move the double x[N], after the part below it i get a list of errors about x being undeclared
    Code:
    printf("Enter the number of values in the sequence please: \n");scanf("%d", &N);
    for (i=0;i<N;i++)
    doublex [N];

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Putting it inside the for() loop means you're declaring x[N] every iteration.

    And doublex [N] != double x[N]

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Could this cause the program crash? It doesn't even ask for input before shutting down.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    You likely need to use dynamic memory allocation via malloc or calloc.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    67
    the sqrt(question) on line 18 does nothing.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Variable Length Arrays or arrays defined at runtime require a C99 compliant compiler. Guessing yours isn't.

    Hard code the size of the array, just use N as last position to go to in your array, and test that user input N doesn't exceed size of your array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why DOS prompt crashes when I run this compiled code?
    By jackson6612 in forum C++ Programming
    Replies: 7
    Last Post: 06-03-2011, 12:58 AM
  2. Replies: 1
    Last Post: 03-12-2008, 12:10 AM
  3. crashing code
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 10:09 AM
  4. oops ! code crashing.. :(
    By Luigi in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2002, 10:38 PM
  5. C# Compiled to Machine Code
    By FwyWice in forum C# Programming
    Replies: 1
    Last Post: 12-06-2002, 03:22 AM

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