Program crashes I suspect variable n.

This is a discussion on Program crashes I suspect variable n. within the C Programming forums, part of the General Programming Boards category; Program crashes I suspect variable n but I'm not a 100% sure. I showed this code to my teacher and ...

  1. #1
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Program crashes I suspect variable n.

    Program crashes I suspect variable n but I'm not a 100% sure. I showed this code to my teacher and we tried different things to solve the crash. But without a single warning and error from Bloodshed's C++ it's hard to pin point the cause of the problem.

    Still I'm pretty certain it has something to do with variable n. Since the array variable code worked in previous testings.

    My teacher says that I have overcomplicated the assignment and don't need to make the code so functional and pretty. So I could theoretically speaking solve this assignment in a really ugly way.
    But I've already spent over 2 weeks over-complicating things and like to know what horrible mistakes I've done. Before I ditch the code and do things the quick and ugly way.

    Why does this program crash?
    Have I forgotten to initialize variable n in main(void)?
    Code:
    /* Sum numbers 0 ... 9 two at a time            */
    /* index increments                             */ 
    /* sum() function uses "pass by reference"      */
    /* even if it's only about one "return value"   */   
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void sum(double a, double b, double* summa);
    void input(double* array, int* n);
    
    
    int main(void)
    {
        double* array;
        int n;
        int i;
    
    /*
    Teacher says that pointers are only necessary when you have to pass several values.
    Variable n only contains one value so it doesn't need a pointer!
    */
    
    //______________________________________________________________________________
    /* Input */
    
        input(array, &n);
    
        printf("%d", n);
    
    //______________________________________________________________________________
    /* Sum */
    
        for( i=0; i<n-1; i++)
        {
            sum(array[i], array[i+1], &array[i+1]); 
            printf("Partial sum %d equals: %.2lf\n", i+1, array[i+1]);     
        }
        
        printf("\nGrandtotal equals: %.2lf\n", array[n-1]); 
    
        printf("\n");
        printf("__________________________________________");
        printf("\n\n");
    
    
        free(array);
    
        system("PAUSE");	
        return 0;
    }
    
    
    /*************
    * Functions
    *************/
    //______________________________________________________________________________
    /* Sum */
    
    void sum(double a, double b, double* summa)
    {
        *summa = a + b;  /* return with "pass by reference" */           
    }
    
    //______________________________________________________________________________
    /* Input */
    
    void input(double* array, int* n)
    {
        int i;
    
    
        printf("How many numbers do you want to input? --> "); scanf("%d", &n);
        // calloc = Memory allocation for array.
        array = calloc(*n, sizeof *array);
    
    
        for(i=0 ; i<*n ; i++)
        {
            printf("\n\nInput a number, for element number <%d>. --> ", i);
            scanf("%lf", array+i);
        }
    
        // Confirm input values.
        for(i=0 ; i<*n ; i++)
            printf("\n< Element number %d > , = %.2lf", i, array[i]);
    
            printf("\n\n");
            printf("______________________________________________________________");
            printf("\n\n");
    }
    
    //______________________________________________________________________________

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Why array is of type double when it supposed to contain ints? Your pointer arithmatics are wrong. Don't use them.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Question

    If I understood you correctly. Then the reason for me using double instead of int on the array variable, is because this is the test program for my bigger complex numbers program. I just try to make troubleshooting as simple as possible.

    For the second thing. Regarding pointer arithmatics being wrong and not using it. Could you maybe explain it in a simple code snippet. Comparing right from wrong? My understanding of pointers & pointees is really confusing at this stage.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    One thing that's wrong is that you pass the pointer "array" by value. This would be ideal if you only modified the value pointed to by array, but here you try to modify pointer itself. Such modifications do not effect the original value passed pointer. To solve this, you can either pass array by reference, as a double**, or move the calloc call out of input().
    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.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    For the second thing. Regarding pointer arithmatics being wrong and not using it. Could you maybe explain it in a simple code snippet. Comparing right from wrong? My understanding of pointers & pointees is really confusing at this stage.
    You should avoid using pointer in C++ except where pointer can't be avoided or gives better performance.
    [EDIT] But its C forum, sorry.
    [EDIT] I detected one error
    Code:
        printf("How many numbers do you want to input? --> "); scanf("%d", &n);
    Should be
    Code:
        printf("How many numbers do you want to input? --> "); scanf("%d", n);
    [EDIT] Don't mention below
    Code:
        for(i=0 ; i<*n ; i++)
        {
            printf("\n\nInput a number, for element number <%d>. --> ", i);
            scanf("%lf", array+i);
        }
    Better be
    Code:
        for(i=0 ; i<*n ; i++) //EDIT
        {
            printf("\n\nInput a number, for element number <%d>. --> ", i);
            scanf("%lf", &array[i]); //Easier to understand?
        }
    Last edited by siavoshkc; 03-30-2010 at 06:57 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15
    Thanks for all the tips, unfortunately I'm still confused by pointers. I think I will have to revisit this subject at a later time instead when I have practiced more with C.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Your code is good. But in input() because n is a pointer you need to send itself to scanf not its address. scanf needs a place to write into. It needs "the address that pointer points to" or address of pointer itself? Of course the former.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Thumbs up [solved]

    After re-writing the program 5 times from scratch I finally found out how to make everything in the program connect. Not that I know what I have done I just guessed like crazy. But the code works now and I can finally understand half of the stuff you guys suggested, but still not every word.

    Anyways thanks for pointing me in the right direction.


    Example code 2 - [ OK ]
    Code:
    /* Sum numbers 0 ... 9 two at a time            */
    /* index increments                             */ 
    /* sum() function uses "pass by reference"      */
    /* even if it's only about one "return value"   */   
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void sum(double a, double b, double* summa);
    void input(double* array, int n);
    
    
    int main(void)
    {
        double* array = NULL;
        int n;
        int i;
    
    /*
    Teacher says that pointers are only necessary when you have to pass several values.
    Variable n only contains one value so it doesn't need a pointer!
    */
    
    //..................................................
    /* How many numbers? */
    
        printf("How many numbers do you want to input? --> "); scanf("%d", &n);
        // calloc = Memory allocation for array.
        array = calloc(n, sizeof *array);
    
    //..................................................
    /* Input */
    
        input(array, n);
    
        printf("\n\n Confirm input() variable n = %d.\n\n", n);
    
    //..................................................
    /* Sum */
    
        for( i=0; i<n-1; i++)
        {
            sum(array[i], array[i+1], &array[i+1]); 
            printf("Partial sum %d equals: %.2lf\n", i+1, array[i+1]);     
        }
        
        printf("\nGrandtotal equals: %.2lf\n", array[n-1]); 
    
        printf("\n");
        printf("__________________________________________");
        printf("\n\n");
    
    
        free(array);
    
        system("PAUSE");	
        return 0;
    }
    
    
    /*************
    * Functions
    *************/
    //______________________________________________________________________________
    /* Sum */
    
    void sum(double a, double b, double* summa)
    {
        *summa = a + b;  /* return with "pass by reference" */           
    }
    
    //______________________________________________________________________________
    /* Input */
    
    void input(double* array, int n)
    {
        int i;
    
    
        for(i=0 ; i<n ; i++)
        {
            printf("\n\nInput a number, for element number <%d>. --> ", i);
            scanf("%lf", &array[i]);  // Easier to understand?
        }
    
        // Confirm input values.
        for(i=0 ; i<n ; i++)
            printf("\n< Element number %d > , = %.2lf", i, array[i]);
    
            printf("\n\n");
            printf("______________________________________________________________");
            printf("\n\n");
    }
    
    //______________________________________________________________________________
    Last edited by Witch; 04-01-2010 at 03:43 AM. Reason: testing sig

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes any ideas
    By auryc in forum C Programming
    Replies: 6
    Last Post: 11-25-2009, 05:50 AM
  2. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 03:50 PM
  3. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM

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