You are not logged in. Please login at www.codechef.com to post your questions!

×

Why am I getting a segmentation error on this?

I am pretty new to programming, so please ignore the inefficiency of the program.

This is a program to arrange numbers in increasing order, and it runs perfectly in code::blocks, but submitting this as an answer to one of the practice problems gives a segmentation error.

Language is C++.

#include<cstdio>
signed int a[100000];

int main()
{
    int t=0,num=0;
    scanf("%d%",&t);
    for(int i=0;i<100000;++i)
    {
        a[i]=-1;
    }
    for(int j=0;j<t;++j)
    {
        scanf("%d",&num);
        a[num]++;

    }
    for(int k=0;k<100000;++k)
    {
        if(a[k]!=-1)
        {
            for(int l=0;l<=a[k];++l)
            {
                printf("%d\n",k);
            }
        }
    }
    return 0;
}

asked 29 Jul '14, 03:18

c1_6's gravatar image

6★c1_6
81110
accept rate: 0%


Each line contains one integer: N [0 <= N <= 10^6]

Just change the size of the array.

link

answered 29 Jul '14, 08:34

gautam94's gravatar image

3★gautam94
474142646
accept rate: 11%

Thanks... I did both, changed array size and declared loop variables outside, don't know which but one worked.

link

answered 29 Jul '14, 13:36

c1_6's gravatar image

6★c1_6
81110
accept rate: 0%

Declaring loop variables inside the loop does not cause runtime error.

(29 Jul '14, 13:42) gautam943★
-1

I did not got any segmentation fault on gcc compiler. Just try to declare the loop variable declarations outside the loop.

link

answered 29 Jul '14, 12:25

rajatsr's gravatar image

0★rajatsr
0
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,911
×23

question asked: 29 Jul '14, 03:18

question was seen: 1,060 times

last updated: 29 Jul '14, 13:42