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?
#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;
elseif (i == 1 && num[i] == 1) s = s + 2;
elseif (i == 2 && num[i] == 1) s = s + 4;
elseif (i == 3 && num[i] == 1) s = s + 8;
elseif (i == 4 && num[i] == 1) s = s + 16;
elseif (i == 5 && num[i] == 1) s = s + 32;
elseif (i == 6 && num[i] == 1) s = s + 64;
elseif (i == 7 && num[i] == 1) s = s + 128;
elseif (i == 8 && num[i] == 1) s = s + 1;
elseif (i == 9 && num[i] == 1) s = s + 2;
elseif (i == 10 && num[i] == 1) s = s + 4;
elseif (i == 11 && num[i] == 1) s = s + 8;
elseif (i == 12 && num[i] == 1) s = s + 16;
elseif (i == 13 && num[i] == 1) s = s + 32;
elseif (i == 14 && num[i] == 1) s = s + 64;
elseif (i == 15 && num[i] == 1) s = s + 128;
elseif (i == 16 && num[i] == 1) s = s + 1;
elseif (i == 17 && num[i] == 1) s = s + 2;
elseif (i == 18 && num[i] == 1) s = s + 4;
elseif (i == 19 && num[i] == 1) s = s + 8;
elseif (i == 20 && num[i] == 1) s = s + 16;
elseif (i == 21 && num[i] == 1) s = s + 32;
elseif (i == 22 && num[i] == 1) s = s + 64;
elseif (i == 23 && num[i] == 1) s = s + 128;
elseif (i == 24 && num[i] == 1) s = s + 1;
elseif (i == 25 && num[i] == 1) s = s + 2;
elseif (i == 26 && num[i] == 1) s = s + 4;
elseif (i == 27 && num[i] == 1) s = s + 8;
elseif (i == 28 && num[i] == 1) s = s + 16;
elseif (i == 29 && num[i] == 1) s = s + 32;
elseif (i == 30 && num[i] == 1) s = s + 64;
}
elseif (num[31] == 1){
if (i == 0 && num[i] == 1) s -= 1;
elseif (i == 1 && num[i] == 1) s = s - 2;
elseif (i == 2 && num[i] == 1) s = s - 4;
elseif (i == 3 && num[i] == 1) s = s - 8;
elseif (i == 4 && num[i] == 1) s = s - 16;
elseif (i == 5 && num[i] == 1) s = s - 32;
elseif (i == 6 && num[i] == 1) s = s - 64;
elseif (i == 7 && num[i] == 1) s = s - 128;
elseif (i == 8 && num[i] == 1) s = s - 1;
elseif (i == 9 && num[i] == 1) s = s - 2;
elseif (i == 10 && num[i] == 1) s = s - 4;
elseif (i == 11 && num[i] == 1) s = s - 8;
elseif (i == 12 && num[i] == 1) s = s - 16;
elseif (i == 13 && num[i] == 1) s = s - 32;
elseif (i == 14 && num[i] == 1) s = s - 64;
elseif (i == 15 && num[i] == 1) s = s - 128;
elseif (i == 16 && num[i] == 1) s = s - 1;
elseif (i == 17 && num[i] == 1) s = s - 2;
elseif (i == 18 && num[i] == 1) s = s - 4;
elseif (i == 19 && num[i] == 1) s = s - 8;
elseif (i == 20 && num[i] == 1) s = s - 16;
elseif (i == 21 && num[i] == 1) s = s - 32;
elseif (i == 22 && num[i] == 1) s = s - 64;
elseif (i == 23 && num[i] == 1) s = s - 128;
elseif (i == 24 && num[i] == 1) s = s - 1;
elseif (i == 25 && num[i] == 1) s = s - 2;
elseif (i == 26 && num[i] == 1) s = s - 4;
elseif (i == 27 && num[i] == 1) s = s - 8;
elseif (i == 28 && num[i] == 1) s = s - 16;
elseif (i == 29 && num[i] == 1) s = s - 32;
elseif (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?
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);
}
elseif (num[31] == 1)
{
s -= pow(2,i);
}
elseif (i == 30)
{
s -= 64;
}
}
}