Disallowed system call: SYS_socketcall - c

I'm very unexperienced in programming and were trying to create a code for a homework to generate 6 random numbers (like in a dice) in two groups of three, and compare them, lower with lower, higher with higher, etc., and register the results, much like "war" game, but in a great number of plays.
So, i ended up with the following code:
rodadas = 3;
for (i = 0; i < 1000; i++) {
ataque_vitorias=0;
ataque[1] = rand() % 6 + 1;
ataque[2] = rand() % 6 + 1;
ataque[3] = rand() % 6 + 1;
defesa[1] = rand() % 6 + 1;
defesa[2] = rand() % 6 + 1;
defesa[3] = rand() % 6 + 1;
j=0;
k=0;
for (j=0 ; j < 2 ; j++)
{
for (k=0 ; k < 2 ; k++)
{
if (ataque[k+1] < ataque[k])
{
t = ataque[k];
ataque[k] = ataque[k + 1];
ataque[k + 1] = t;
}
}
}
j=0;
k=0;
for (j=0 ; j < 2 ; j++)
{
for (k=0 ; k < 2 ; k++)
{
if (defesa[k+1] < defesa[k])
{
t = defesa[k];
defesa[k] = defesa[k + 1];
defesa[k + 1] = t;
}
}
}
if (ataque[1] > defesa[1])
ataque_vitorias++;
if (ataque[2] > defesa[2])
ataque_vitorias++;
if (ataque[3] > defesa[3])
ataque_vitorias++;
if (ataque_vitorias == 0) total_0++;
if (ataque_vitorias == 1) total_1++;
if (ataque_vitorias == 2) total_2++;
if (ataque_vitorias == 3) total_3++;
}
printf("total_0: %d\n", total_0);
printf("total_1: %d\n", total_1);
printf("total_2: %d\n", total_2);
printf("total_3: %d\n", total_3);
return 0;
And defined all the variables ind the main void. The problem is when i try to run it, i keep getting the error:
Disallowed system call: SYS_socketcall
I don't have any idea of how to fix it so the code can run.
Sorry for the bad english (i'm not native) and any help would be much appreciated!

Related

Increased Pascal Triangle printed with wrong values - C

I want to print a triangle that is similar to Pascal Triangle but the sides are increased instead of containing the value 1.
Regular Pascal Triangle:
Wanted Triangle:
Regular Pascal method:
void PascalTriangle(int rows) {
int i =0,j = 0,space,coef = 0;
for (i = 0 ; i<rows ; i++){
for (space = 1 ; space <= rows - i ; space++)
printf(" ");
for (j = 0 ; j <= i ; j++) {
if (i == 0 || j == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d",coef);
}
printf("\n");
}
}
what I was trying to do:
void PascalTriangle(int rows) {
int i =0,j = 0,space,coef = 0;
for (i = 0 ; i<rows ; i++){
for (space = 1 ; space <= rows - i ; space++)
printf(" ");
for (j = 0 ; j <= i ; j++) {
if (i == 0 || j == 0)
coef ++;
else
coef = coef * (i - j + 1) / j;
printf("%4d",coef);
}
printf("\n");
}
}
When increasing coef my output looks good only on the sides of the triangle:
I would like to clarify - I am not looking for a solution but to learn where I went wrong, I will appreciate any help.
As you ask for directions and not for a solution, I'll say that you cannot use the same index algebra as in the standard Pascal's triangle formula when the outer coefficients aren't equal to one.

Sorting a 2D Array c++

I am bubble sorting a 2D array that looks like this. I am confuse on how to make my largest value as 1 and make the 2nd row's value follow to 1st row's counterpart.
Input:
13 9 1 8 5
1 2 3 4 1
Actual output:
1 5 8 9 13
1 2 3 4 1
This is the expected output that i am trying to make.
Output:
5 8 9 13 1
1 4 2 1 1
Here is my code for sorting the cards (col = 5 and row = 2):
void sortedCards(int card[][col])
{
int i, j, k, temp;
printf("\n\nSorted Cards\n");
for (k = 0; k < 10; k++)
{
for (i = 0; i < row - 1; i++)
{
for (j = 0; j < col - 1; j++)
{
if (card[i][j] > card[i][j + 1])
{
temp = card[i][j];
card[i][j] = card[i][j + 1];
card[i][j + 1] = temp;
}
}
}
}
for (i = 0; i < row; i++)
{
if (i == 1)
{
printf("\n");
}
for (j = 0; j < col; j++)
{
printf("%i ", card[i][j]);
}
}
}
If your sorting is only dependent on the first row, there is no need to iterate through the second row. Just set both rows at the same time while checking the first row.
Also, if you want 1 to be treated as larger than all other numbers, you need to add that to your Boolean logic. Adjusting your for loop like below should do it.
int j, k, temp, temp2;
for (k = 0; k < 10; k++)
{
for (j = 0; j < col-1; j++)
{
//here we only test row 0, and we check if the value is 1
if (card[0][j] == 1 || (card[0][j] > card[0][j+1] && card[0][j+1] != 1))
{
//all other reassignment is the same but you do both rows at the same time
temp = card[0][j];
temp2 = card[1][j];
card[0][j] = card[0][j + 1];
card[1][j] = card[1][j + 1];
card[0][j + 1] = temp;
card[1][j + 1] = temp2;
}
}
}

Why is my binary output different than the expected output?

I am having issue with my code giving me the correct output. For example, I am trying to do binary subtraction. The first test case is suppose to test 0x00CD - 0x00AB and the correct output is 0000000000100010 (0x0022). I am getting output of 0000000001100110 (0x0066), which is a few digits off. Can someone help me debug my problem?
#include <stdio.h>
int main(void)
{
int borrow, i, j;
int x[16] = {0}, y[16] = {0}, difference[16] = {0};
int n= 16;
unsigned int hex1, hex2;
scanf("%x %x", &hex1, &hex2);
i = 0;
while (hex1 != 0)
{
x[i] = hex1 % 2;
hex1 = hex1 / 2;
i = i + 1;
}
i = 0;
while (hex2 != 0)
{
y[i] = hex2 % 2;
hex2 = hex2 / 2;
i = i + 1;
}
borrow = 0;
for (i = 15; i >= 0; i--)
{
if(y[i] <= x[i])
{
difference[i] = (x[i] - y[i]);
}
else if (i == n - 1)
{
borrow = 1;
x[i] = x[i] + 2;
difference[i] = (x[i] - y[i]);
}
else
{
j = i + 1;
while (x[j] == 0)
{
j = j + 1;
}
x[j] = x[j] - 1;
j = j - 1;
while (j > i)
{
x[j] = x[j] + 2 - 1;
j = j - 1;
}
difference[i] = (2 + x[i] - y[i]);
}
}
for (i = 15; i >= 0; i--)
printf("%d", difference[i]);
return 0;
}
The logic for your borrow code is broken. When x[i] is greater than y[i] and i is not n-1, you go into this code that looks for a non-zero x[j] to borrow from. But the higher-indexed x[j] were already operated on (because i runs from 15 to 0). You should not be borrowing from them.
Usually subtraction proceeds from the low digits to the high digits (i from 0 to 15). Then, if we need to borrow, we calculate the current digit as if we borrowed, set a counter or flag to remember that we borrowed, and go on to the next digit, incorporating the borrow into the calculations for it.
Alternately, if working from high to low, then, when a borrow is needed, we need to take it from the previously calculated digits (in difference), not from the minuend (x). (And the code for that would have to be alert for running off the high end.)

C programming code error

So the program is still incomplete, i cant go any further cause there is an error right after the first input, i tried using visual studio 2010 and 2015, both with the same problem:
unhandled exception at 0x60eae42e (msvcr100d.dll) in asd.exe: 0xc0000005: Access violation writing location 0xccccccccc
so can any find the problem in this? or test and see if its working on your pc? this code is supposed to be c
int main()
{
int y[3][3], inv[3][3], co[3][3], d[3], sol[3], D = 0,i=0, j = 0;
char z;
start: // Used to restart the program when the persons want to do more work or has done an error
printf("The format for the linear equation is\na1.X + b1.Y + c1.Z = d1\na2.X + b2.Y + c2.Z = d2\na3.X + b3.Y + c3.Z = d3\n");
for (i = 0;i < 3;i++)
{
for (z = 'a';z < 'd';z++,j++)
{
printf("Enter the value for %c%i\n", z, i + 1);
scanf("%i", y[i][j]);
}
printf("Enter the valie for D%i\n", i + 1);
scanf("%i", d[i]);
j = 0;
}
for (i = 0;i < 3;i++)
for (j = 0;j < 3;j++)
co[i][j] = (y[(i + 1) % 3][(j + 1) % 3] * y[(i + 2) % 3][(j + 2) % 3]) - (y[(i + 1) % 3][(j + 2) % 3] * y[(i + 2) % 3][(j + 1) % 3]);
for (i = 0;i < 3;i++)
D += y[i][0] * co[i][0];
if (D == 0)
{
printf("\nThese equations cannot be solved!\n");
}
for (i = 0;i < 3;i++)
for (j = 0;j < 3;j++);
for (i = 0;i < 3;i++)
for (j = 0;j < 3;j++)
inv[i][j] = co[i][j] / D;
for (i = 0;i < 3;i++)
{
sol[i] = 0;
for (j = 0;j < 3;j++)
sol[i] += inv[i][j] * d[j];
}
printf("The solutions are\nX=%i\nY=%i\nZ=%i\n", sol[0], sol[1], sol[2]);
getch();
goto start;
}
These:
scanf("%i", y[i][j]);
scanf("%i", d[i]);
needs to be:
scanf("%i", &y[i][j]);
scanf("%i", &d[i]);
as %i in the scanf expects an int*(address of the variable), not an int(value of the variable).
Another problem is that you do division by zero here:
inv[i][j] = co[i][j] / D;
when D is zero.

What's wrong with my code? Generate Unique Random Number

I'm trying to generate 10 unique random numbers between 1 and 10. I keep getting duplicate numbers. Can someone tell me what the problem is? What am I missing or need to fix in my code? Thank you!
students[0].id = rand() % 10 + 1;
for (int i = 1; i < 10; i++)
{
students[i].id = rand() % 10 + 1;
for (int j = 0; j < i; j++)
{
if (students[i].id == students[j].id)
{
students[i].id = rand() % 10 + 1 ;
}
}
}
for (int i = 0; i < 10; i++)
{
printf("%d\n", students[i].id);
}
if (students[i].id == students[j].id)
{
students[i].id = rand() % 10 + 1 ;
}
In this line , you may get duplicate.
if students[i].id & students[j].id = 5 means it will get true. But in this line students[i].id = rand() % 10 + 1 ; ,you may get again 5
Instead of above line, you may use this code.
students[0].id = rand() % 10 + 1;
for (int i = 1; i < 10; i++)
{
students[i].id = rand() % 10 + 1;
for (int j = 0; j < i; j++)
{
if (students[i].id == students[j].id)
{
i--;
break;
}
}
}
The function rand() isn't guaranteed to generate unique random numbers. Moreover, your way of limiting the range (mod 10) is especially bad and is likely to generate many duplicates.
The simplest way to generate your sequence is probably to shuffle an array of 10 values from 1 to 10.
rand() is random in nature, so there is no guarantee to give you unique results.
You need to do something like this - keep track of all the numbers encountered so far and call the rand() till you find unique numbers
Random does not mean "No Duplicates".
After you entered this if clause if (students[i].id == students[j].id) and modified students[i].id, you need to check for duplicates again.
Try This One: replace if with while. I hope you get your answer.
students[0].id = rand() % 10 + 1;
for (int i = 1; i < 10; i++)
{
students[i].id = rand() % 10 + 1;
for (int j = 0; j < i; j++)
{
while (students[i].id == students[j].id)
{
students[i].id = rand() % 10 + 1 ;
}
}
}
for (int i = 0; i < 10; i++)
{
printf("%d\n", students[i].id);
}
I think you want to sample without replacement. You can store the indices in an array, randomly pick up one & simultaneously remove it from the array.
So that when you draw next time, it doesn't repeat.
You need recursion.
public static void main(String[] args)
{
System.out.println(generateRandomNumbers(10, new ArrayList<Integer>()));
}
private static List<Integer> generateRandomNumbers(Integer maxLimit, List<Integer> randomNumberList)
{
for (int i = 0; i < maxLimit; i++)
{
Integer tempRandom = new Random().nextInt(10);
if (randomNumberList.contains(tempRandom))
generateRandomNumbers(1, randomNumberList);
else
randomNumberList.add(tempRandom);
}
return randomNumberList;
}
What happens if RAND_MAX (commonly 32767) isn't evenly divisible by 10? You're likely to get values between 1 and 7 more often than 8 and 0. That's a bias.
I would suggest discarding any values greater than or equal to 32760 (or rather, RAND_MAX - RAND_MAX % 10), and using the division operator to construct your random number:
int x;
do {
x = rand();
} while (x >= RAND_MAX - RAND_MAX % 10);
x /= RAND_MAX / 10;
You'll see a fairly drastic improvements from this; In fact, that seems like your most significant bias. However, the distribution of the values you get still isn't required to be uniform. Use a lookup table to discard any values you've previously selected:
int selected[10] = { 0 };
for (int i = 0; i < 10; i++) {
int x;
do {
x = rand();
} while (selected[x / RAND_MAX / 10] || x >= RAND_MAX - RAND_MAX % 10);
x /= RAND_MAX / 10;
selected[x] = 1;
student[i].id = x;
}
for (int i = 0; i < 10; i++) {
printf("student[%d].id: %d\n", i, students[i].id);
}

Resources