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.
Related
I ve done a program that take an number n as input and then return a square matrix n*n with the property that all row, columns, and diagonals have the same sum.
The project works without problem, and I tried to optimize it as much as i can, from the algorithm to uses the specific data type for this(in my case unsigned short, cause i didn t need a bigger storage).
After all i tried to see the performance and i wanted to try it with a bigger number like 100,200, so on;
But when i tried to change the storage of matrix the program didn t work properly and returned a matrix with 0 and the sum was strange.
I don t understand from where is this bug.
#include <stdio.h>
#include <stdlib.h>
unsigned short a[100][100], i = 0, j = 0, n, suma[100];
void next_b(unsigned short *i, unsigned short *j); // find the properly i and j
void completare(unsigned short i, unsigned short j); // completes the matrix after i find the i and j
void tipar(); // print the matrix
int suma_linie(unsigned short x); //sum of a row
int suma_coloana(unsigned short y); //sum of a column
int suma_diagonala_principala(); //first diagonal
int suma_diagonala_secundara(); //second one
int main()
{
scanf("%hu", &n);
system("cls");
j = n / 2 - 1;
a[0][j] = 4;
a[0][j + 1] = 1;
a[1][j] = 2;
a[1][j + 1] = 3;
suma[0] = 5;
suma[1] = 5;
suma[n + j] = 6;
suma[n + j + 1] = 4;
for (int x = 2; x <= (n / 2) * (n / 2); x++)
{
next_b(&i, &j);
a[i][j] = x;
completare(i, j);
}
tipar();
//for(int x=0;x<n;x++){
//
// printf("suma de pe linia %d este: %d\n",x,suma_linie(x));
// printf("suma de pe coloana %d este: %d\n\n",x,suma_coloana(x));
//}
//printf("suma de pe daig principala este: %d\n\n",suma_diagonala_principala());
// printf("suma de pe daig secundara este: %d\n\n",suma_diagonala_secundara());
for (int x = 0; x < 2 * n + 2; x++)
{
if (x < n)
{
printf("suma de pe linia %d este %hu\n", x, suma[x]);
}
else if (x < 2 * n)
{
printf("suma de pe coloana %d este %hu\n", x % n, suma[x]);
}
else if (x == 2 * n)
{
printf("suma de pe diag principala este %hu\n", suma[x]);
}
else
{
printf("suma de pe diag secundara este %hu\n", suma[x]);
}
}
return 0;
}
void tipar()
{
for (int k = 0; k < n; k++)
{
for (int l = 0; l < n; l++)
{
if (a[k][l] < 10)
{
printf(" %d |", a[k][l]);
}
else if (a[k][l] <= 99)
{
printf(" %d |", a[k][l]);
}
else if (a[k][l] < 1000)
{
printf(" %d |", a[k][l]);
}
else if (a[k][l] < 10000)
{
printf("%d ", a[k][l]);
}
}
printf("\n");
for (int z = 0; z <= 6 * n - 1; z++)
{
printf("-");
}
printf("\n");
}
printf("\n");
}
void next_b(unsigned short *i, unsigned short *j)
{
if (*i - 2 < 0)
{
if (a[n - 2][*j + 2] == 0 && *j + 2 <= n - 2)
{
// printf("cazul 2\n");
*i = n - 2;
*j += 2;
return;
}
else if (a[*i - 2][*j] == 0)
{
// printf("cazul 7\n");
*i += 2;
return;
}
}
else
{
if (*j == n - 2)
{ //printf("cazul 3\n");
*i -= 2;
*j = 0;
return;
}
else if (a[*i - 2][*j + 2] != 0)
{
//printf("cazul 4\n");
*i += 2;
}
else if (a[*i - 2][*j + 2] == 0)
{
// printf("cazul 5\n");
*i -= 2;
*j += 2;
}
}
}
void completare(unsigned short i, unsigned short j)
{
if (i <= n / 2)
{ //////////// l
if (i == n / 2 - 1 && j == n / 2 - 1)
{
a[i][j + 1] = 4 * a[i][j];
a[i + 1][j] = 4 * a[i][j] - 2;
a[i + 1][j + 1] = 4 * a[i][j] - 1;
a[i][j] = 4 * a[i][j] - 3;
}
else
{
a[i][j] = 4 * a[i][j];
a[i][j + 1] = a[i][j] - 3;
a[i + 1][j] = a[i][j] - 2;
a[i + 1][j + 1] = a[i][j] - 1;
}
}
else if (i == n / 2 + 1)
{ ///////////// u
if (j == n / 2 - 1)
{
a[i][j] = 4 * a[i][j];
a[i][j + 1] = a[i][j] - 3;
a[i + 1][j] = a[i][j] - 2;
a[i + 1][j + 1] = a[i][j] - 1;
}
else
{
a[i][j + 1] = 4 * a[i][j];
a[i + 1][j] = 4 * a[i][j] - 2;
a[i + 1][j + 1] = 4 * a[i][j] - 1;
a[i][j] = 4 * a[i][j] - 3;
}
}
else
{ ///////x
a[i][j + 1] = 4 * a[i][j];
a[i + 1][j + 1] = 4 * a[i][j] - 2;
a[i + 1][j] = 4 * a[i][j] - 1;
a[i][j] = 4 * a[i][j] - 3;
}
suma[i] += a[i][j] + a[i][j + 1];
suma[i + 1] += a[i + 1][j] + a[i + 1][j + 1];
suma[n + j] += a[i][j] + a[i + 1][j];
suma[n + j + 1] += a[i][j + 1] + a[i + 1][j + 1];
if (i == j)
{
suma[2 * n] += a[i][j] + a[i + 1][j + 1];
}
if (i + j + 1 == n - 1)
{
suma[2 * n + 1] += a[i + 1][j] + a[i][j + 1];
}
}
int suma_linie(unsigned short x)
{
int s = 0;
for (int y = 0; y < n; y++)
{
s += a[x][y];
}
return s;
}
int suma_coloana(unsigned short y)
{
int s = 0;
for (int x = 0; x < n; x++)
{
s += a[x][y];
}
return s;
}
int suma_diagonala_principala()
{
int s = 0;
for (int x = 0; x < n; x++)
{
s += a[x][x];
}
return s;
}
int suma_diagonala_secundara()
{
int s = 0;
for (int x = 0; x < n; x++)
{
s += a[x][n - x - 1];
}
return s;
}
The code is solve with an algorithm that i found on Wikipedia-Magic Square.
In the above cod if i try to change suma size to 200 for example or any other value, the program works strange and returns stupid things.
It s valid even i set a size higher.
I used two ways to see the sum, one with a predefined functions and the other adding the matrix element to suma, the functions used int and the other method utilize unsigned short if its matters.
I checked your code and found some problems. I tried with n = 90.
for(int x=2; x<=(n/2)*(n/2); x++)
{
next_b(&i,&j);
a[i][j]=x;
completare(i,j);
}
If you check this code then you can see when the value of i, j become 0, 2 accordingly then it goes to next_b(i, j) method. And there are some lines which are trying to access negative indexes. So it throws exception.
Like -
else if(a[*i-2][*j]==0) // here
{
// printf("cazul 7\n");
*i+=2;
return;
}
.............................................
if(*j==n-2)
{
//printf("cazul 3\n");
*i-=2;
*j=0;
return;
}
else if (a[*i-2][*j+2]!=0) // here
{
//printf("cazul 4\n");
*i+=2;
}
else if(a[*i-2][*j+2]==0) // here
{
//printf("cazul 5\n");
*i-=2;
*j+=2;
}
Try to fix these negative indexing and then it should work (Assuming your approach is correct).
so I was writing this code for the game of life using C on linux but i got this warning! what does this warning mean and how can i fix it ?. The code i wrote is :
#include <stdio.h>
#include <string.h>
#include <omp.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_N 2000
int plate[2][(MAX_N + 2) * (MAX_N + 2)];
int which = 0;
int n;
int live(int index){
return (plate[which][index - n - 3]
+ plate[which][index - n - 2]
+ plate[which][index - n - 1]
+ plate[which][index - 1]
+ plate[which][index + 1]
+ plate[which][index + n + 1]
+ plate[which][index + n + 2]
+ plate[which][index + n + 3]);
}
void iteration(){
#pragma omp parallel for schedule(static)
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
int index = i * (n + 2) + j;
int num = live(index);
if(plate[which][index]){
plate[!which][index] = (num == 2 || num == 3) ?
1 : 0;
}else{
plate[!which][index] = (num == 3);
}
}
}
which = !which;
}
void print_plate(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
printf("%d", plate[which][i * (n + 2) + j]);
}
printf("\n");
}
printf("\0");
}
int main(){
int M;
char line[MAX_N];
memset(plate[0], 0, sizeof(int) * (n + 2) * (n + 2));
memset(plate[1], 0, sizeof(int) * (n + 2) * (n + 2));
if(scanf("%d %d", &n, &M) == 2){
for(int i = 1; i <= n; i++){
scanf("%s", &line);
for(int j = 0; j < n; j++){
plate[0][i * (n + 2) + j + 1] = line[j] - '0';
}
}
for(int i = 0; i < M; i++){
iteration();
}
print_plate();
}
return 0;
}
it would be so much appreciated if you could help me fix cause i think this should have work fine.
You have this:
scanf("%s", &line);
line is of type char[2000] (MAX_N). By taking the address-of operator of it, you are getting a type of char(*)[2000]. Get rid of the & and instead you will have type char[2000] which will decay to the char* you need.
There are a few errors in the code:
You're trying to scan a variable line by addressing it in the scanf() function. This could be solved if you remove the ampersand & sign from there.
Explained answer is though already provided in the first answer of this question.
Using the statement:
printf("\0"); // format contains a (null)
is totally meaningless. You're trying to print something which doesn't exists - a null.
The pragma:
#pragma omp parallel for schedule(static)
will be ignored as per of -Wunknown-pragmas flag.
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.)
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!
So i made a program to solve a linear equation of 3, but for some reason it does not give me the appropriate answer, i have done my research but cant seem to find whats wrong with my coding
I am using visual c++ 2010/ 2015
void linear()
{
int y[3][3], inv[3][3], co[3][3], d[3], sol[3], D = 0, i = 0, j = 0;
char z;
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++)
{
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");
return;
}
for (i = 0;i < 3;i++)
for (j = 0;j < 3;j++)
swap(&co[i][j], &co[j][i]);
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();
}
Integer division.
With matrix solutions, even with integer inputs, invariably the solution will need floating point math.
// int inv[3][3], sol[3];
double inv[3][3], sol[3];
...
for (i = 0;i < 3;i++)
for (j = 0;j < 3;j++)
// inv[i][j] = co[i][j] / D;
inv[i][j] = 1.0 * co[i][j] / D;
...
sol[i] += inv[i][j] * d[j];
...
// printf("The solutions are\nX=%i\nY=%i\nZ=%i\n", sol[0], sol[1], sol[2]);
printf("The solutions are\nX=%e\nY=%e\nZ=%e\n", sol[0], sol[1], sol[2]);