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]);
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).
I have this function
int dctTransform(int matrix[][ndct])
{
int i, j, k, l;
float dct[mdct][ndct];
float ci, cj, dct1, sum;
for (i = 0; i < mdct; i++) {
for (j = 0; j < ndct; j++) {
if (i == 0)
ci = 1 / sqrt(mdct);
else
ci = sqrt(2) / sqrt(mdct);
if (j == 0)
cj = 1 / sqrt(ndct);
else
cj = sqrt(2) / sqrt(ndct);
sum = 0;
for (k = 0; k < mdct; k++) {
for (l = 0; l < ndct; l++) {
dct1 = matrix[k][l] *
cos((2 * k + 1) * i * pipi / (2 * mdct)) *
cos((2 * l + 1) * j * pipi / (2 * ndct));
sum = sum + dct1;
}
}
dct[i][j] = ci * cj * sum;
}
}
return dct;
}
And I want in main do something like that
matrixZ = dctTransform(matrixZ);
I have "return value does not match" error at "return dct" and I can't see what's wrong here, how can I improve it.
MatrixZ[8][8] is int, mdct & ndct = 8.
I'm attempting to initialize, populate and parse through an array in order to determine its "stability." To avoid a stack overflow, I decided to create dynamic arrays. The problem is that when it comes to populating the array, I get an exception regarding an access violation to a random location. I don't know if its something in the initialization or in the nested for loop when populating the array. I just can't seem to find anything wrong, nor my classmates/TAs. Thanks in advance for your help! I have tried compiling in VS, XCode, and g++ I have tried commenting out the dynamic array loops as well as the delete loops and gone for "regular arrays" such as float array[x][y] and I still get the same error.
#include <iostream>
#include <array>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
int check = 0;
int iteration = 0;
int newIteration = 0;
int newNewIteration = 0;
int const DIMENSION = 1024;
//Initializing the dynamic arrays in
//heap to avoid a stack overflow
float** firstGrid = new float*[DIMENSION];
for (int a = 0; a < DIMENSION; ++a) {
firstGrid[a] = new float[DIMENSION];
}
float** secondGrid = new float*[DIMENSION];
for (int b = 0; b < DIMENSION; ++b) {
secondGrid[b] = new float[DIMENSION];
}
float** thirdGrid = new float*[DIMENSION];
for (int c = 0; c < DIMENSION; ++c) {
thirdGrid[c] = new float[DIMENSION];
}
//Populating the arrays
//All points inside first array
for (int i = 0; i < DIMENSION; ++i) {
for (int j = 0; i < DIMENSION; ++j) {
firstGrid[i][j] = 0.0; //exception occurs here
}
}
for (int i = 1; i < DIMENSION - 1; ++i) {
for (int j = 1; i < DIMENSION - 1; ++j) {
firstGrid[i][j] = 50.0;
}
}
//Pre-setting second array
for (int i = 0; i < DIMENSION; ++i) {
for (int j = 0; i < DIMENSION; ++j) {
secondGrid[i][j] = 0.0;
}
}
for (int i = 1; i < DIMENSION - 1; ++i) {
for (int j = 1; i < DIMENSION - 1; ++j) {
secondGrid[i][j] = 50.0;
}
}
//Pre-setting third array
for (int i = 0; i < DIMENSION; ++i) {
for (int j = 0; i < DIMENSION; ++j) {
thirdGrid[i][j] = 0.0;
}
}
for (int i = 1; i < DIMENSION - 1; ++i) {
for (int j = 1; i < DIMENSION - 1; ++j) {
thirdGrid[i][j] = 50.0;
}
}
//Checking and Populating new arrays
for (int p = 1; p < DIMENSION - 1; ++p) {
for (int q = 1; q < DIMENSION - 1; ++p) {
check = abs((firstGrid[p - 1][q] + firstGrid[p][q - 1] + firstGrid[p + 1][q] + firstGrid[p][q + 1]) / 4
- firstGrid[p][q]);
if (check > 0.1) {
secondGrid[p][q] = (firstGrid[p - 1][q] + firstGrid[p][q - 1] + firstGrid[p + 1][q] + firstGrid[p][q + 1]) / 4;
iteration = iteration + 1;
}
}
}
for (int p = 1; p < DIMENSION - 1; ++p) {
for (int q = 1; q < DIMENSION - 1; ++p) {
check = abs((secondGrid[p - 1][q] + secondGrid[p][q - 1] + secondGrid[p + 1][q] + secondGrid[p][q + 1]) / 4
- secondGrid[p][q]);
if (check > 0.1) {
thirdGrid[p][q] = (secondGrid[p - 1][q] + secondGrid[p][q - 1] + secondGrid[p + 1][q] + secondGrid[p][q + 1]) / 4;
newIteration = newIteration + 1;
}
}
}
for (int p = 1; p < DIMENSION - 1; ++p) {
for (int q = 1; q < DIMENSION - 1; ++p) {
check = abs((thirdGrid[p - 1][q] + thirdGrid[p][q - 1] + thirdGrid[p + 1][q] + thirdGrid[p][q + 1]) / 4
- thirdGrid[p][q]);
if (check > 0.1) {
newNewIteration = newNewIteration + 1;
}
}
}
//Deleting arrays and freeing memory
for (int x = 0; x < DIMENSION; ++x) {
delete [] firstGrid[x];
}
delete [] firstGrid;
for (int x = 0; x < DIMENSION; ++x) {
delete [] secondGrid[x];
}
delete [] secondGrid;
for (int x = 0; x < DIMENSION; ++x) {
delete [] thirdGrid[x];
}
delete [] thirdGrid;
//iteration checking
cout << iteration << endl << newIteration << endl << newNewIteration;
if (iteration == 179 || newIteration == 179 || newNewIteration == 179) {
return 0;
}
else {
return 1;
}
}
You should use j consistently in your second for-loop (where the error occurs):
for(j=0; j < DIMENSION; j++)
I am trying to implement an algorithm to solve the Knapsack problem:
cst = 1;
for (j = 0; j < 200; j++) {
if (kk - cst < 0) {
continue;
cst++;
}
for (i = kk - cst; i >= 0; --i) {
C[i + cst] = max(C[i + cst], C[i] + index[cst]);
}
cst++;
}
The index array has the values of respective items represented by index of the array. I want to know where I'm going wrong.
In your code
if(kk-cst < 0)
{
continue;
cst++;
}
is wrong. the cst++ will never be executed. Please check and change your logic accordingly.
The increment to cst is unreachable code here. Swap the two lines
if (kk - cst < 0) {
cst++;
continue;
}
A simple dynamic programming knapsack implementation would be
int KnapSack(int W, int wt[], int val[], int n) {
int i, w;
int K[n + 1][W + 1];
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0) K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
else K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
as listed in http://www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-problem/
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.