Illegal instruction (core dumped) error: scanf in a while loop until EOF - c

So I wrote a function in C which gets input until EOF
while (scanf(" %c", &input) != EOF) {
// do stuff
}
I tried to use the following test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
10 20 30 40 50 _66_ 15 66bb
As an input to the program. It actually runs the program but at the end it finishes with the following error: Illegal instruction (core dumped).
Other tests work fine, and after a quick check I found out that the problem begins after the last 50 (if I use only the numbers until 50 - it works fine). How to solve this problem?

Related

how the broker in cloudsim works

I am working on chicken swarm optimization when the algorithm return the best solution the broker seem to have a different ordering
best solution
Cloudlet Vm
-------- ---------
99 1
52 2
62 7
63 13
64 3
68 14
71 6
94 0
97 8
13 9
53 10
91 11
16 12
1 4
61 5
15 15
33 16
34 17
35 18
43 19
44 1
45 2
46 7
47 13
49 3
42 14
48 6
50 0
51 8
37 9
40 10
36 11
39 12
41 4
38 5
6 15
31 16
19 17
65 18
88 19
89 1
66 2
9 7
92 13
93 3
18 14
98 6
14 0
30 8
12 9
2 10
96 11
11 12
29 4
77 5
87 15
70 16
76 17
78 18
81 19
82 1
69 2
56 7
57 13
32 3
85 14
75 6
80 0
83 8
60 9
86 10
79 11
74 12
84 4
67 5
28 15
90 16
27 17
21 18
72 19
95 1
73 2
26 7
23 13
25 3
24 14
3 6
20 0
59 8
58 9
0 10
55 11
54 12
4 4
22 5
17 15
8 16
10 17
7 18
5 19
printing the result after receiving from broker
Cloudlet ID STATUS Data center ID VM ID
99 SUCCESS 2 1
52 SUCCESS 2 2
62 SUCCESS 2 7
64 SUCCESS 2 3
71 SUCCESS 2 6
97 SUCCESS 2 8
63 SUCCESS 2 13
68 SUCCESS 2 14
94 SUCCESS 2 0
53 SUCCESS 2 10
13 SUCCESS 2 9
91 SUCCESS 2 11
16 SUCCESS 2 12
1 SUCCESS 2 4
61 SUCCESS 2 5
15 SUCCESS 2 15
35 SUCCESS 2 18
44 SUCCESS 2 1
33 SUCCESS 2 16
34 SUCCESS 2 17
45 SUCCESS 2 2
46 SUCCESS 2 7
49 SUCCESS 2 3
48 SUCCESS 2 6
51 SUCCESS 2 8
47 SUCCESS 2 13
42 SUCCESS 2 14
50 SUCCESS 2 0
43 SUCCESS 2 19
40 SUCCESS 2 10
37 SUCCESS 2 9
36 SUCCESS 2 11
39 SUCCESS 2 12
41 SUCCESS 2 4
38 SUCCESS 2 5
6 SUCCESS 2 15
31 SUCCESS 2 16
19 SUCCESS 2 17
65 SUCCESS 2 18
89 SUCCESS 2 1
88 SUCCESS 2 19
66 SUCCESS 2 2
9 SUCCESS 2 7
93 SUCCESS 2 3
92 SUCCESS 2 13
18 SUCCESS 2 14
98 SUCCESS 2 6
14 SUCCESS 2 0
30 SUCCESS 2 8
2 SUCCESS 2 10
12 SUCCESS 2 9
96 SUCCESS 2 11
any ideas why this this happened ?

Weird Output in for loop in C (Repetitive numbers)

This is my code for finding prime numbers.
int main (void){
int p, d;
_Bool isprime;
for (p=2;p<=50;++p){
isprime = 1;
for (d=2;d<p;++d){
if (p%d == 0)
isprime = 0;
if (isprime != 0)
printf("%i ", p);
}
}
return 0;
}
The output of this program is
3 5 5 5 7 7 7 7 7 9 11 11 11 11 11 11 11 11 11 13 13 13 13 13 13 13 13 13 13 13 15 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 21 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 25 25 25 27 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 33 35 35 35 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 39 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 43 45 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 49 49 49 49 49
The numbers seem to be right, but they are repetitive. Is there something wrong with my code? Or is there something wrong with the compiler? Every time I use a for loop (not just this program) the output is like this. How do I fix this?
Thanks in advance!(Sorry for my bad English)
The problem with your code, is that it prints p multiple times, for the same value of p. Like Eugene Sh. stated in a comment, it's easily fixed by moving the if statement up a level, to the outer for loop. I also changed is_prime to a bool value, and edited how it's set.
int max_p = 50;
int p, d;
bool is_prime;
for (p = 2; p <= max_p; ++p) {
is_prime = true;
for (d = 2; d < p; ++d) {
if (p % d == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
printf("%i ", p);
}
}
I will assume that I used C++ doesn't make a difference.
Others have given tips. Some of them are useful, some of them are not. The end goal is to print all prime numbers only. So checking if the number 6 is prime, we start with d = 2. So we check 6 % 2. Well that is equal to 0. We know that 6 is NOT prime. We should move on to 7. Instead we have incremented 2 to 3 and now we check again. 6 % 3... we do this for every number less than the number we are trying to check. This greatly adds to the computational complexity. This program runs very close to BigTheta(n^2). An easier way to run this would be a do-while loop or through the use of a break statement. Since you're using C, use an int as a bool marker (to prevent another lib import). Your inner loop would look like this:
int is_prime = 1;
for (d = 2; d < p; d++)
{
if (p % d == 0)
{
is_prime = 0
break;
}
}
if (is_prime)
printf("%d ", p);
There are additional methods of reducing the complexity, but those rely on better algorithm design moreso than more efficient code. For a beginner's project, think about why the current version of your code is inefficient (even if it was working properly). A lot of it comes with experience, but you shouldn't neglect individual code- and algorithm review.
You need to put your printf lines outside your inner loop.
Like this:
int main (void)
{
int p, d;
_Bool isprime;
for (p=2;p<=50;++p){
isprime = 1;
for (d=2;d<p;++d){
if (p%d == 0) {
isprime = 0;
break;
}
}
if (isprime)
printf("%i ", p);
}
return 0;
}
You have to use break statement. Without break, every time the calculation is performed, thus so many repetition in your original code. But even with break statement your code will generate many non desirable numbers/output instead of just generating prime numbers. So i have written following code for you. It will generate prime numbers as per your question.
Use the following code for generating prime numbers upto 50.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int prime,i,k,j,max;
printf("2\n");
printf("3");
for(i=4; i<max; i++)
{
if(i%2!=0 && i%3!=0 && i%5!=0 && i%7!=0)
{
prime=i;
printf("\n%d",prime);
k++;
}
}
}

Replicating a vector while summing an increasing value

I have this vector:
a = [ 7 8 9 7 8 9];
and I would like to obtain the following vector:
b= [ 7 8 9 7 8 9;
17 18 19 17 18 19;
27 28 29 27 28 29;
37 38 39 37 38 39 ...]
I am replicating the vector and then summing 10 for each line (for n lines). I would like to do this without using loop iterations. How can I do it?
Thank you so much.
My first bsxfun solution.
a = [ 7 8 9 7 8 9];
b = 0:10:(10+10*n);
c = bsxfun(#plus,a,b.');
c =
7 8 9 7 8 9
17 18 19 17 18 19
27 28 29 27 28 29
37 38 39 37 38 39
47 48 49 47 48 49
57 58 59 57 58 59
67 68 69 67 68 69
77 78 79 77 78 79
87 88 89 87 88 89
97 98 99 97 98 99
107 108 109 107 108 109
Bit of explanation, though a full and complete introduction to bsxfun can be found in this answer by Divakar.
What happens is that your row array a gets piece-wise added to the column vector b. Thus the first element of a, being 7 in this case, gets added to the column vector b=[10;20;30;...] and becomes the first column of your output matrix c. The second entry of a is summed with the same column vector b and becomes the second column of c. This gets repeated to fill the entire matrix c to a size of numel(b) x numel(a).
This is becoming quite the coding fest. I ran some bench test, running a loop with n=1000 a hundred times and averaged the results. Windows 7, MATLAB R2012a, i5-750 CPU. Actually, the for loop is not even the worst in terms of timing:
bsxfun: 0.00003556 s.
repmat: 0.00048514 s.
cumsum: 0.00015726 s.
for : 0.00033096 s.
Timing revisited on the same system, but with MATLAB R2015a. for is now the slowest, with the others edging towards one another, but bsxfun prevails!
bsxfun: 0.00002030 s.
repmat: 0.00005213 s.
cumsum: 0.00002180 s.
for : 0.00019560 s.
Where I used this for loop implementation:
base = [7 8 9 7 8 9];
A = zeros(1e3,length(base));
for n = 1:1e3;
A(n,:) = base+10*n;
end
For completeness, you could also obtain the desired result through the use of the repmat command:
n = 3;
a = [7 8 9 7 8 9];
addingMatrix = repmat([0:10:10*n+1]',1,size(a,2));
b = addingMatrix + repmat(a,n+1,1);
Result:
b =
7 8 9 7 8 9
17 18 19 17 18 19
27 28 29 27 28 29
37 38 39 37 38 39
Since repmat and bsxfun are already taken, another approach is to create a temporary array where [7 8 9 7 8 9] is the first row, followed by a matrix completely full of 10s where the number of rows is the desired number you want and the number of columns is the total number of elements in the base vector ([7 8 9 7 8 9]). You'd then apply a cumulative sum via cumsum along the rows:
n = 11; %// Number of rows - including the first row of [7 8 9 7 8 9]
base = [7 8 9 7 8 9];
A = [base; 10*ones(n-1, numel(base))];
B = cumsum(A, 1);
We get:
>> B
B =
7 8 9 7 8 9
17 18 19 17 18 19
27 28 29 27 28 29
37 38 39 37 38 39
47 48 49 47 48 49
57 58 59 57 58 59
67 68 69 67 68 69
77 78 79 77 78 79
87 88 89 87 88 89
97 98 99 97 98 99
107 108 109 107 108 109

Runtime error in codechef practice questions

I'm getting "sigsegv" , a runtime error from the following code when i try to run it on codechef while the code works fine on my computer with various test inputs.I've also kept in mind the constraints given in the problem but I'm still unable to debug it. The question is not from any contest but a practice problem . Please point out any mistake you can find .
Actual codechef question
#include<stdio.h>
int cash[101][101]={0};
int rec[101][2];
int ri=0;
int sumx(int mat[101][101],int i,int j,int lines)
{
int n=0,a=0,b=0;
if(cash[i][j]!=0)
{
return cash[i][j];
}
else if(i==lines-2)
{
n=(mat[i+1][j]>mat[i+1][j+1])?mat[i+1][j]:mat[i+1][j+1];
cash[i][j]=n+mat[i][j];
rec[ri][0]=i;
rec[ri++][1]=j;
return n+mat[i][j];
}
else
{
a=sumx(mat,i+1,j,lines);
b=sumx(mat,i+1,j+1,lines);
n=(a>b)?a:b;
cash[i][j]=n+mat[i][j];
rec[ri][0]=i;
rec[ri++][1]=j;
return n+mat[i][j];
}
}
int main()
{
int i=0,k=0;
int lines=0,n=0;
int r=0;
int tc=0;
int mat[101][101];
scanf("%d",&tc);
while(tc--)
{
scanf("%d",&lines);
i=0;
k=0;
while(i<lines)
{
while(k<=i)
{
scanf("%d",&mat[i][k]);
k++;
}
k=0;
i++;
}
if(lines==1)
{
r=mat[0][0];
}
else
{
r=sumx(mat,0,0,lines);
}
i=0;
while(i<ri)
{
cash[(rec[i][0])][(rec[i][1])]=0;
rec[i][0]=0;
rec[i][1]=0;
i++;
}
ri=0;
printf("%d\n",r);
}
return 0;
}
The error is with lines
while(i<ri)
{
cash[(rec[i][0])][(rec[i][1])]=0;
rec[i][0]=0;
rec[i][1]=0;
i++;
}
the values of rec[i][0] rec[i][1] can be undefined in some cases ie they may return garbage values
You can Use memset instead to change the values to 0
memset(rec,0,sizeof(rec));
memset(cash,0,sizeof(cash));
I ran your solution , there's a bug in your algorithm implementation try finding it yourself
I can provide you a test case(using a testcase generator) for which it fails
21
79
89 28
14 6 63
96 58 67 48
80 8 22 27 8
24 21 23 96 97 72
38 90 95 83 57 60 94
13 96 9 24 65 27 67 40
26 20 58 42 29 8 52 49 37
80 65 65 34 79 10 89 11 20 84
57 59 72 79 51 67 84 70 43 62 96
16 4 18 9 5 40 34 2 15 4 28 50
29 1 60 39 28 92 38 65 95 57 10 71 37
25 78 96 43 17 51 88 19 0 30 20 80 39 35
55 41 63 76 4 20 97 72 43 93 76 11 82 33 25
61 85 41 77 42 90 20 5 69 51 4 54 41 18 83 72
12 56 21 82 7 1 84 26 47 26 22 52 84 39 75 70 89
12 39 83 92 49 20 35 20 31 96 66 75 48 79 13 51 49 50
42 81 0 58 70 40 16 83 27 34 79 64 14 26 19 22 38 55 93
64 81 26 29 47 22 73 61 3 2 61 99 18 43 33 10 13 46 24 53
5 56 0 0 3 0 71 12 82 34 17 11 14 51 1 82 73 53 85 75 89
correct answer is 1431 while your code returns 1299
#Randomizer : The code runs fine with the output as 1431 , not 1299 . Anyways , as you suggested about the garbage being generated from rec[i][0] rec[i][1], It never appeared to me that rec[i][0] rec[i][1] could be generating garbage as I only accessed the used up cells . However , memset eliminated the need of rec , so cash was easily reset to zero . All i did was remove the rec matrix and reset cash using memset and the code ran fine on codechef. I couldn't figure out how it could be generating garbage , still , its accepted on codechef since I removed rec part . Thank you for the help.

Why would this code count different number of lines from linux and windows text files?

This code,
lineCount = 1;
do{ //find the line count in the file
c = fgetc(fp);
if(c == '\n') lineCount++;
}
while(c != EOF);
fclose(fp);
counts 6 lines from this file
5 7 9 3 2 10 1 11 6 4 14 0 12 8 13
3 4 10 8 0 12 13 2 7 1 9 5 6 14 11
12 14 11 8 0 7 3 5 1 6 4 13 10 9 2
14 11 13 0 2 12 9 3 5 7 1 6 8 4 10
0 1 8 6 5 3 11 2 7 9 4 12 10 14 13
and counts 5 lines from this file
39 47 37 30 7 38 17 49 11 1 29 41 25 19 10 45 23 0 32 15 2 9 4 6 21 40 20 24 5 31 34 3 33 48 44 27 14 26 28 35 16 42 46 36 12 8 22 13 18 43
37 13 24 28 34 27 5 41 36 29 44 26 0 15 40 31 23 35 9 8 4 33 21 6 11 49 2 7 43 32 16 1 30 42 39 14 45 10 38 22 19 17 20 25 18 47 48 46 3 12
0 49 26 20 14 12 10 3 9 23 15 37 5 32 4 42 25 46 38 45 40 19 22 1 39 29 7 41 33 13 30 35 11 6 18 31 21 28 24 36 16 43 27 34 44 17 2 8 47 48
0 1 20 11 28 3 43 9 15 25 45 29 33 19 48 18 17 16 14 34 10 7 42 4 37 41 22 30 23 21 32 39 2 46 8 36 40 27 31 13 6 38 12 5 44 26 35 24 49 47
15 30 18 7 34 25 43 14 38 48 40 9 33 26 28 27 21 0 20 10 47 8 11 32 12 5 36 4 46 42 6 29 13 31 23 17 39 35 19 49 24 41 44 16 37 45 2 1 22 3
What would the reason be?
EDIT
I created a file with 5 lines in Windows and 5 lines in Linux, Windows file counts 5 and Linux file counts 6. Why?
You should verify that the two files actually only have four newline characters. In Linux, use the xxd command to dump the contents of your files in hexadecimal and then count the number of a characters (because newline characters are the letter 'a' in hex):
$ cat foo.txt
5 7 9 3 2 10 1 11 6 4 14 0 12 8 13
3 4 10 8 0 12 13 2 7 1 9 5 6 14 11
12 14 11 8 0 7 3 5 1 6 4 13 10 9 2
14 11 13 0 2 12 9 3 5 7 1 6 8 4 10
0 1 8 6 5 3 11 2 7 9 4 12 10 14 13
$ xxd -p foo.txt
352037203920332032203130203120313120362034203134203020313220
38203133200a332034203130203820302031322031332032203720312039
20352036203134203131200a313220313420313120382030203720332035
20312036203420313320313020392032200a313420313120313320302032
20313220392033203520372031203620382034203130200a302031203820
362035203320313120322037203920342031322031302031342031330a <- note the 'a' at the end
$ xxd -p foo.txt | grep -o a | wc -l
5
You'll likely find that there are indeed five newline characters in your "6 line" file.
You start the count at 1 and increment for each newline encountered. If a file has one line—one string of non-newline characters followed by one newline—and then ends, you start the count at 1 and then increment it once, giving a line count of 2.
Perhaps want you want to do is start the count at 0, increment for each newline, and take note of any final non-newline-terminated-line? (That's a little bit harder.)
If test data were to be assumed to be the same as posted above and fp were to be opened as a text file:
I tested this on Linux and Windows. Both print 6(incorrect of-course because of the logic, which assumes that there is atleast 1 line)
gcc - 4.5.3
microsoft compiler cl for VS2010.
Also, linecount must start from 0 instead of 1.

Resources