A program calculating sequence

This is a discussion on A program calculating sequence within the C Programming forums, part of the General Programming Boards category; Hello I did a on-line judge problem and the error msg is " segmentation fault " But the code goes ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    18

    A program calculating sequence

    Hello

    I did a on-line judge problem and the error msg is "segmentation fault"

    But the code goes well in my NB, using DEV-C++ 4.9.9.2.

    Could somebody give me some hints?

    Thanks a lot!

    Spec.
    1. Input number of sequence
    2. Input sequence of 4 elements
    3. Determine they are arithmetic or geometric sequence
    4. Calculate the 5th element and print the sequences

    Sample Input
    2
    1 2 3 4
    1 2 4 8

    Sample Output
    1 2 3 4 5
    1 2 4 8 16


    Code:
    #include <stdio.h>
    #include <memory.h>
    
    
    int main(void){
    
    
        int LoS=5;
        
        unsigned int n=0,*S[n],i,j;
        
        while(scanf("%d",&n) != EOF){
    
    
             
            for(i=0;i<n;i++){
    
    
                S[i] = (int*)malloc(LoS*sizeof(int));
                
                }
                
            
            for(i=0;i<n;i++){
    
    
                scanf("%d %d %d %d",&S[i][0],&S[i][1],&S[i][2],&S[i][3]);
    
    
                }
            
            for(i=0;i<n;i++){
                             
                if((S[i][1]-S[i][0])==(S[i][2]-S[i][1])){
    
    
                    S[i][LoS-1] = S[i][LoS-2]+(S[i][1]-S[i][0]);
                    for(j=0;j<LoS;j++) printf("%d ",S[i][j]);
    
    
                    }
                else{
    
    
                    S[i][LoS-1] = S[i][LoS-2]+(S[i][1]/S[i][0]);
                    for(j=0;j<LoS;j++) printf("%d ",S[i][j]);
    
    
                    }
                    
                printf("\n");    
                    
            }        
    
    
        }
    
    
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Excuse me.

    Besides the "segmentation fault" error, I have another different question.

    I dynamically assign the number of interested sequence of fixed 4 elements in this code.

    If I want to dynamically assign the number of element in each sequence.

    Besides prompting user to input the number of elements in a sequence, how can I keep the sample input format?
    That is:
    1 2 3 4
    not
    1
    2
    3
    4

    Am I describing my problem clearly?

    Many thanks.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Sorry for the bothering.

    I did the function desired.

    Code:
    #include <stdio.h>
    #include <memory.h>
    
    
    //#define LoS 5;
    
    
    int main(void){
    
    
        
        unsigned int n=0,*S[n],i,j;
        unsigned int* TS=NULL;
        
        int LoS_temp=5;
        int LoS[n];
        
        //char format[3*(LoS-1)];
        //format = "%d %d %d %d";
        //format = "I Love You";
        /*
        for(i=0;i<LoS;i++){
            
            format[3*i] = '%';
            format[3*i+1] = 'd';
            if((3*i+2) != (3*(LoS-1)))
            format[3*i+2] = ' ';
            else
            format[3*i+2] = 0;
            
            }
          */  
             
        
        while(scanf("%d",&n) != EOF){
    
    
            printf("You are going to input %d sequence(s).",n);
            printf("\n");
            
            /* 動態配置陣列記憶體空間 */ 
            for(i=0;i<n;i++){
    
    
                printf("Please enter the number of element in sequence NO.%d :",i+1);
                scanf("%d",&LoS_temp);
                LoS[i] = LoS_temp;
                S[i] = (int*)malloc(LoS[i]*sizeof(int));
                printf("You have %d elements in sequence NO.%d.",LoS[i]-1,i+1);
                printf("\n");
                
                }
             printf("Memory allocation success!");
             printf("\n");
                
            /* 動態配置陣列記憶體空間 */
            for(i=0;i<n;i++){
                printf("Please enter the element(s) of squence %d",i+1);
                printf("\n");             
                for(j=0;j<LoS[i]-1;j++){
                                 
                    scanf("%d",&S[i][j]);
                    //getchar();
                    
                }
            }
            
            for(i=0;i<n;i++){
                             
                if((S[i][1]-S[i][0])==(S[i][2]-S[i][1])){
    
    
                    S[i][LoS[i]-1] = S[i][LoS[i]-2]+(S[i][1]-S[i][0]);
                    for(j=0;j<LoS[i];j++) printf("%d ",S[i][j]);
    
    
                    }
                else{
    
    
                    S[i][LoS[i]-1] = S[i][LoS[i]-2]*(S[i][1]/S[i][0]);
                    for(j=0;j<LoS[i];j++) printf("%d ",S[i][j]);
    
    
                    }
                    
                printf("\n");    
                    
            }        
    
    
        }
    
    
        getchar();
        return 0;
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    unsigned int n=0;
    int LoS[n];

    you create array of zero size and expect it to hold any number of values? It does not work this way. If array has no place to store values - you cannot store them in it.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Quote Originally Posted by vart View Post
    unsigned int n=0;
    int LoS[n];

    you create array of zero size and expect it to hold any number of values? It does not work this way. If array has no place to store values - you cannot store them in it.
    Is it incorrect even if I let user input non-zero n with a following scanf in while()?

    So I shouldn't semi-dynamically assign the size of LoS[n] with an initialized 0 then change it afterward?

    I am just confused cause this code pass using gcc. That's why I am not aware of this problem.

    BTW, I initialized n as 20 (whatever is a non-zero integer) and on-line judge past without segmentation fault.

    If I would like declare exactly the request size of int array and pointer array without an initially larger size, how can I do?
    Should I declare the pointer array inside
    Code:
    while(scanf("%d",&n) != EOF){...here}?
    But it seems the pointer array variable wouldn't exist after the while loop execution?

    Have many details to be fixed...

    Many thanks

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you could use malloc to allocate dynamic array of exact size
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    18
    Quote Originally Posted by vart View Post
    you could use malloc to allocate dynamic array of exact size
    Hello~

    I do use malloc to allocate array dynamically, which is to assign the number of unit cell(sizeof(unsigned int)) of the memory space corresponding to the element number in a sequence.

    And my question is how to dynamically allocate the number of array(i.e. sequence in the spec.) with dynamically allocated element number.

    If I skip the idea of dynamic allocation, it is simply a 2-dimensional array problem.(or even using 1D array if I won't confuse myself)

    Since I would like to dynamically set the number of sequence and number of element in a sequence, it seems I have to allocate the memory for number of array and number of element in each array.

    That's why I try to semi-dynamically determine the number of pointer array by variable n, and dynamically allocate space for corresponding element number in each array pointed by pointer array element. (I almost get confused by what I describe.......)

    I think maybe I can move the declaration of pointer array into the while loop (after the value input of n) to prevent from zero size pointer array.

    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program Not Properly Calculating
    By andynov123 in forum C Programming
    Replies: 16
    Last Post: 09-18-2011, 03:27 PM
  2. C Program Help - Calculating Federal Tax
    By MetDude21 in forum C Programming
    Replies: 10
    Last Post: 02-18-2011, 11:51 AM
  3. Calculating FPS for my OpenGL program
    By C+noob in forum Game Programming
    Replies: 1
    Last Post: 10-09-2005, 01:22 PM
  4. Replies: 2
    Last Post: 11-10-2003, 08:12 PM
  5. Replies: 8
    Last Post: 02-23-2002, 08:50 PM

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