Thread: I get segmentation error

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    15

    Question I get segmentation error

    This program asks user to enter numbers between 1 and 100. After i run it i get segmentation error. The program doesnt repeat number twice. Can someone help me.
    Code:
    #include<stdio.h>
    #define SIZE 20
    int main(void)
    {
     int a[SIZE];
     int i;
     int x;
     int j;
     int flag;
     int idx=0;
    
    
      printf("Enter 20 numbers between 1 and 100\n");
      scanf("%d",&a[i]);
    
    //this is how to accept multiple numbers
      for (int i = 0; i < 20;i++){
            scanf("%d",&x);
            a[i]=x;
    
            // this is to determine that numbers dont repeat
            if(i==0){
                    a[i]=x;
                    idx++;
            }
            else{
                    for( int j=0; j<idx; j++){
    
    
                    }
    
            }
    
            if(flag !=1){
                    a[idx]=x;
                    idx++;
            }
    
      printf("The no duplicate values are:\n");
      printf("\n%d",x);
     }
    
    
    
    return 0;
    }

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    scanf("%d",&a[i]);
    You need to initialize i before you can use it. You initialize it after this point in your for loop.

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    The error is probably coming from this though:

    Code:
    for (int i = 0; i < 20;i++){
    you already declared i as an int before the for loop, this should just be i = 0 (actually it shouldn't since you've already taken one value for your a array )

    You also declare j twice.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    can you specify, what you mean?

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
     int a[SIZE];
     int i;
     int x;
     int j;
     int flag;
     int idx=0;
    
    
      printf("Enter 20 numbers between 1 and 100\n");
      scanf("%d",&a[i]);
    You have not given a value to i, so where is scanf going to place the inputted value? &a[i] needs a value for i.

    Code:
    for (int i = 0; i < 20;i++){
    ...
    for( int j=0; j<idx; j++){
    You have already declared these variables at the beginning:

    Code:
     int a[SIZE];
     int i;
     int x;
     int j;
     int flag;
     int idx=0;
    you don't need to declare them again, just give them values, i.e. i = 0, j = 0

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    Like this... i still keep getting the segmentation error
    Code:
    #include<stdio.h>
    #define SIZE 20
    
    int main(void)
    {
     int a[SIZE];
     int i;
     int x;
     int j;
     int flag;
     int idx=0;
    
    
      printf("Enter 20 numbers between 10 and 100\n");
      scanf("%d",&a[i]);
    
    //this is how to accept multiple numbers
      for (i = 0; i < 20;i++){
            scanf("%d",&x);
            a[i]=x;
    
            // this is to determine that numbers dont repeat
            if(i==0){
                    a[i]=x;
                    idx++;
            }
            else{
                    for( j=0; j<idx; j++){
    
    
                    }
    
            }
    
            if (a[j]==x){
                    flag=1;
            }
    
            if(flag !=1){
                    a[idx]=x;
                    idx++;
            }
    
    
      printf("The no duplicate values are:\n");
      printf("\n%d",x);
     }
    
    
    
    return 0;
    }

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
     int a[SIZE];
     int i;
     int x;
     int j;
     int flag;
     int idx=0;
    
    
      printf("Enter 20 numbers between 10 and 100\n");
      scanf("%d",&a[i]);
    &a[i] is telling scanf to place the input at the address of the 'i'th term in the 'a' array. Again, you have not supplied a value for i...so it doesn't know what part of the array you want to use. Try:

    Code:
     int a[SIZE];
     int i;
     int x;
     int j;
     int flag;
     int idx=0;
    
    
      printf("Enter 20 numbers between 10 and 100\n");
      scanf("%d",&a[0]);
    
    //this is how to accept multiple numbers
    //i = 1 because a[0] has already been filled
      for (i = 1; i < 20;i++){

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    I tried everything you told me, it still gives me segmentation error.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Isn't the problem that you shouldn't use the addressof operator on the array?

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    15
    what do you mean?

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Nevermind, I think I'm confusing things here.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Warning 3 warning C6001: Using uninitialized memory 'i': Lines: 6, 7, 8, 9, 10, 11, 14, 15 15
    Warning 4 warning C6001: Using uninitialized memory 'j': Lines: 6, 7, 8, 9, 10, 11, 14, 15, 18, 19, 20, 23, 24, 25, 35 35
    Warning 5 warning C6001: Using uninitialized memory 'flag': Lines: 6, 7, 8, 9, 10, 11, 14, 15, 18, 19, 20, 23, 24, 25, 35, 39 39
    Warning 6 warning C4700: uninitialized local variable 'i' used 15

    What this is saying is that you are using a lot of uninitialized variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM