1 / 7
May 2014

first of all, it's my First time in this site. when i submitted this code it says "runtime error (SIGSEGV) "

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
  int num[1000] ,y=0 , i , x , z , flag=0;
  printf("Input:\n");
  for(i=0;i<10;i=i+clock()/CLOCKS_PER_SEC)
  {
                scanf("%d",&x);
                if ( x == 42 )
                {
                      z=y;
                      flag=1;    
                }
                else
                {
                    num[y]=x;
                    y=y+1;
                }
  }
  printf("Output:\n");
  if ( flag == 1 )
  {
       for(i=0;i<z;i++)
       {
                   printf("%d\n",num[i]);
       }
  }
  else
    for(i=0;i<y;i++)
  {
                   printf("%d\n",num[i]);
  }	
  return 0;
}
  • created

    May '14
  • last reply

    May '14
  • 6

    replies

  • 1.5k

    views

  • 2

    users

  • 3

    links

Welcome! I have a few suggestions for your code smile

for(i=0;i<10;i=i+clock()/CLOCKS_PER_SEC)

I assume that you are trying to keep track of the "time limit". You don't need to do this. If your code executes for too long the judge will tell you by returning TLE (Time Limit Exceeded)

printf("Input:\n");
printf("Output:\n");

Everything that you print out will be considered in the answer. Your stdout using this code with the sample input would be:

Input:
Output:
1
2
88
42
99

This does not match the sample output. You can alleviate this stress by redirecting stdin and stdout to files. If you can figure this out I suggest it, but if you need help feel free to post back.

Now on to your SIGSEGV. You are assuming that there will not be more numbers in the input than 1000. This assumption is incorrrect. The problem statement does not mention a maximum amount of numbers simply that we read until we reach the number 42 and then abort. stdin and stdout are seperate streams so we can read from stdin and then print immediately to stdout. This does seem awkward since you are typing the input and receiving the output in the same console window at this time.

Please post back if you have any questions.

Cheers!

I'm a student in computer science second term using Dev-C++ 4.9.9.2 (Bloodshed software), totally new in C, so yea any help will be appreciated. I don't know how to use files to be honest but i can try to learn it if you have a good guide also

I don't quite get it.

This code works fine i guess because every time i try it on Dev-C++ it works fine. there's an image for prove.

All of your suggestions is accepted.

The console is serving a dual purpose for you and confusing you all at the same time.

en.wikipedia.org/wiki/Standard_streams7

Your compiled executable reads input from a stream that is called "Standard Input" or "stdin". It writes it's output to "Standard Output" or "stdout".

Since you are using the console it is reading the input you type and writing it to the stdin stream for your application to then read it. Your program is then writing it's output to stdout and the console is reading that and displaying it on the screen for you to read.

I don't use Dev-C++ but I assume that you are clicking a button that is compiling and launching this console window.

If we take the text that you posted in your image and assign it to the different streams:

Input: (stdout)
1 (stdin)
2 (stdin)
88 (stdin)
42 (stdin)
99 (stdin)
Output: (stdout)
1 (stdout)
2 (stdout)
88 (stdout)

If this were broken into two seperate files:
stdin:

1
2
88
42
99

stdout:

Input:
Output:
1
2
88

When your code is executed on the judge it reads everything that is written to stdout. Since "Input:" and "Output:" are written to stdout, your code would return a wrong answer if it were not getting SIGSEGV.

Here is a link on how to redirect stdin and stdout to a file within the code:
stackoverflow.com/questions/5848 ... out-from-c12

If you have more questions, please post back.

Yes, Sir your assumption is right.

Anyway, I removed the timer to stop tracking the time just like you said and used "FILES" to store numbers instead of arrays.

so can you tell me am i even close or not yet ? ( i know that it's still not right)

    #include <stdio.h>
    #include <stdlib.h>
    int main()
{
  FILE *fp;
  fp=fopen("c:\\test.txt","w");
  int i , x , z , n;
  printf("Input:\n");
 for(i=0;i<1;i=0)
  {
                scanf("%d",&x);
                if ( x == 42 )
                {
                      break;   
                }
                else
                {
                    fprintf(fp,"%d\n",x);
                }
                                   
  }
  fclose(fp);
  fp=fopen("c:\\test.txt","r");
  for(i=0; i<1 ;i=0)
  {
                  n=fscanf(fp,"%d",&z);
                  if ( n != 1)
                     break;
                  printf("%d\n",z);
                  

  }
  fclose(fp);
  return 0;
}

I was more hoping that you would use freopen to redirect stdin and stdout to your input and output files. When you submit to the judge you will not have access to the filesystem, you only have access to read from stdin and write to stdout. Because of this you can use fscanf, you need to use scanf. You will need to comment out these freopen statements when you submit to the judge.

Here's a better link:
cplusplus.com/reference/cstdio/freopen5

Once you have that working you can realize that you don't need to read all of the input before you start making output. You can read a number, print a number, read, print, etc.

Here's how to use freopen. I store my files in the directory "c:\oj" #ifndef is a preprocessor flag. SPOJ defines ONLINE_JUDGE so it will ignore all of the freopen commands when you submit to SPOJ but will use them when you run it in DEV-C++.

#include <cstdio>
using namespace std;
int main(void) {
    #ifndef ONLINE_JUDGE
    freopen("c:\\oj\\test.in", "r", stdin);
    freopen("c:\\oj\\test.out", "w", stdout);
    #endif
	// Put your solution here
    #ifndef ONLINE_JUDGE
    fclose(stdout);
    fclose(stdin);
    #endif
}

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 23 14d

Want to read more? Browse other topics in C and C++ or view latest topics.