Troubleshooting this program. Assistance would be appreciated.

Error: Segmentation Fault (Core Dumped)

Explanation: I noticed that the program (that is presented below) compiles fine without any problems, but when I attempt to actually execute it, the above error results. I've been scouring most of the internet for a way to resolve this, but frankly, I'm almost at my wit's end here. Thoughts?

CODE:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <cstdio>
#include <math.h>

int main()
{
    int s, i;
    int num[32] = {0};
    int n, m;

    while (1) {
        s = 0;
        for (i = 30; i >=0; i++){
            printf("%d /n", num[i]);
            if (num[31] == 0){
                if (i == 0 && num[i] == 1) s = s + 1;
                else if (i == 1 && num[i] == 1) s = s + 2;
                else if (i == 2 && num[i] == 1) s = s + 4;
                else if (i == 3 && num[i] == 1) s = s + 8;
                else if (i == 4 && num[i] == 1) s = s + 16;
                else if (i == 5 && num[i] == 1) s = s + 32;
                else if (i == 6 && num[i] == 1) s = s + 64;
                else if (i == 7 && num[i] == 1) s = s + 128;
                else if (i == 8 && num[i] == 1) s = s + 1;
                else if (i == 9 && num[i] == 1) s = s + 2;
                else if (i == 10 && num[i] == 1) s = s + 4;
                else if (i == 11 && num[i] == 1) s = s + 8;
                else if (i == 12 && num[i] == 1) s = s + 16;
                else if (i == 13 && num[i] == 1) s = s + 32;
                else if (i == 14 && num[i] == 1) s = s + 64;
                else if (i == 15 && num[i] == 1) s = s + 128;
                else if (i == 16 && num[i] == 1) s = s + 1;
                else if (i == 17 && num[i] == 1) s = s + 2;
                else if (i == 18 && num[i] == 1) s = s + 4;
                else if (i == 19 && num[i] == 1) s = s + 8;
                else if (i == 20 && num[i] == 1) s = s + 16;
                else if (i == 21 && num[i] == 1) s = s + 32;
                else if (i == 22 && num[i] == 1) s = s + 64;
                else if (i == 23 && num[i] == 1) s = s + 128;
                else if (i == 24 && num[i] == 1) s = s + 1;
                else if (i == 25 && num[i] == 1) s = s + 2;
                else if (i == 26 && num[i] == 1) s = s + 4;
                else if (i == 27 && num[i] == 1) s = s + 8;
                else if (i == 28 && num[i] == 1) s = s + 16;
                else if (i == 29 && num[i] == 1) s = s + 32;
                else if (i == 30 && num[i] == 1) s = s + 64;
            }

            else if (num[31] == 1){
                if (i == 0 && num[i] == 1) s -= 1;
                else if (i == 1 && num[i] == 1) s = s - 2;
                else if (i == 2 && num[i] == 1) s = s - 4;
                else if (i == 3 && num[i] == 1) s = s - 8;
                else if (i == 4 && num[i] == 1) s = s - 16;
                else if (i == 5 && num[i] == 1) s = s - 32;
                else if (i == 6 && num[i] == 1) s = s - 64;
                else if (i == 7 && num[i] == 1) s = s - 128;
                else if (i == 8 && num[i] == 1) s = s - 1;
                else if (i == 9 && num[i] == 1) s = s - 2;
                else if (i == 10 && num[i] == 1) s = s - 4;
                else if (i == 11 && num[i] == 1) s = s - 8;
                else if (i == 12 && num[i] == 1) s = s - 16;
                else if (i == 13 && num[i] == 1) s = s - 32;
                else if (i == 14 && num[i] == 1) s = s - 64;
                else if (i == 15 && num[i] == 1) s = s - 128;
                else if (i == 16 && num[i] == 1) s = s - 1;
                else if (i == 17 && num[i] == 1) s = s - 2;
                else if (i == 18 && num[i] == 1) s = s - 4;
                else if (i == 19 && num[i] == 1) s = s - 8;
                else if (i == 20 && num[i] == 1) s = s - 16;
                else if (i == 21 && num[i] == 1) s = s - 32;
                else if (i == 22 && num[i] == 1) s = s - 64;
                else if (i == 23 && num[i] == 1) s = s - 128;
                else if (i == 24 && num[i] == 1) s = s - 1;
                else if (i == 25 && num[i] == 1) s = s - 2;
                else if (i == 26 && num[i] == 1) s = s - 4;
                else if (i == 27 && num[i] == 1) s = s - 8;
                else if (i == 28 && num[i] == 1) s = s - 16;
                else if (i == 29 && num[i] == 1) s = s - 32;
                else if (i == 30 && num[i] == 1) s = s - 64;
            }
        }
        printf("%d", s);
        printf("Enter Your Command:");
        printf("1. Set a bit");
        printf("2. Clear a bit");
        printf("3. Exit");
        scanf("%d", &n);

        if (n == 1) {
            printf("Enter the bit");
            printf("The far right bit is known as zero.");
            printf("The far left bit is known as thirty-one.");
            scanf("%d", &m);
            num[m] = 1;
        }

        if (n == 2) {
            printf("Enter the bit");
            printf("The far right bit is known as zero.");
            printf("The far left bit is known as thirty-one.");
            scanf("%d", &m);
            num[m] = 0;
        }

        if (n == 3) {
            break;
        }
    }
}


CODE PURPOSE: The purpose is to allow a user to specifically change certain bits within these four bytes, then return the result (two's compliment model). The process is supposed to perpetually continue until the user decides to actually exit the program. Suggestions?


The problem is in this line:

for (i = 30; i >=0; i++)

You want i to count from 30 down to 0 but it is counting from 30 upwards so it very quickly goes beyond the 32 elements you defined in your array, hence the seg fault

By the way you could really tidy up your if statements - all your comparisons are against num[i] == 1 so why not make this the main if statement and then use some 2^x maths to handle any i value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  for (i = 30; i >=0; i--)
	{
            printf("%d\n", num[i]);
            if(num[i] == 1)
	    {
                if (num[31] == 0)
	        {
		    s += pow(2,i);
		}
	        else if (num[31] == 1)
	        {
		    s -= pow(2,i);
		}
                else if (i == 30)
		{
		    s -= 64;
                }
	    }
        }
Gentlemen,

Thank you very much for your assistance. The program now works as I had originally intended after the modifications you have suggested.
Topic archived. No new replies allowed.