Stop working when running [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i wrote this code for my university project but it stop working when i try to run it.
can any one please help me where the problem is?
i guess its the pointers but i dont know where its wrong
for t and z if u want to try it use 20 and 5
and when i enter them it stop working instead of giving me outputs
it should give 2 .txt file including some numbers
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int t;
int z;
double deltaz;
double deltat;
double beta;
double mv=0;
ofstream out1("Time-setllment.txt");
ofstream out2("isochrones.txt");
cout<<"Enter time step(>9&integer only):";
cin>>t;
cout<<endl<<"Enter height step(integer only):";
cin>>z;
cout<<endl;
double **u = new double * [z+1];
for (int i=0;i<z;i++)
u[i]=new double [t+1];
double *s = new double [t+1];
mv = (0.00000003*365*24*3600)/(2*10);
deltaz = 20/z;
deltat = 50/t;
beta = (2*deltat)/(deltaz*deltaz);
if (beta>0.5)
cout<<"Beta is more than 0.5 :|"<<endl;
else
{
for (int i=0;i<z+1;i++)
{
if (i==0 || i==z)
{
for (int j=0;j<t+1;j++)
u[i][j] = 0;
}
else
{
u[i][0]=70-3.5*(i*deltaz);
}
}
for (int j=1;j<t;j++)
{
for (int i=1;i<z;i++)
{
u[i][j] = u[i][j-1]+beta*(u[i-1][j-1]+u[i+1][j-1]-2*u[i][j-1]);
}
}
for (int j=1;j<t+1;j++)
{
double sigmau=0;
for (int i=1;i<z;i++)
{
sigmau +=u[i][j];
}
s[j]=mv*(35*20-deltaz*((u[0][j]+u[z][j])/2+sigmau));
}
out1<<"Time \t Settlment \n";
for (int j=1;j<t+1;j++)
{
out1<<j*deltat<<"\t"<<s[j]<<"\n";
}
out2<<"Depth \t";
for (int j=0;j<t+1;j++)
{
out2<<"t(year)="<<j*deltat<<"\t";
}
out2<<"\n";
for (int i=0;i<z+1;i++)
{
out2<<i*deltaz<<"\t";
for (int j=0;j<t+1;j++)
{
out2<<u[i][j]<<"\t";
}
}
for (int i=0;i<z+1;i++)
delete []u[i];
delete []u;
delete []s;
}
return 0;
}

You have an error in data allocation:
double **u = new double * [z+1];
for (int i=0;i<z;i++)
You actually want to change this to:
double **u = new double * [z+1];
for (int i=0;i<z+1;i++)

Related

0/1 Knapsack problem using Dynamic Programming, Top-Down Approach [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Greeting everyone, I'm trying to solve 0/1 Knapsack problem using the Dynamic Programming Top-Down Approach. I'm pretty sure that most of my logic is correct, my code is compiling successfully. But, it's not giving the proper/correct output that is needed.
For Instance, suppose weight[] has inputs as 10,20,30 and it's corresponding value[] has 60,100,120. The max weight that the Knapsack can hold onto is 50. The max profit should be 220, but my code is giving me the answer 280 instead. Please help me, here's my piece of code:-
#include<bits/stdc++.h>
using namespace std;
void knapsack(vector<int>& weight, vector<int>& value, int w, int n){
vector<vector<int>> t;
for(int i=0;i<n+1;++i){
vector<int> temp;
for(int j=0;j<w+1;++j){
int x =0;
temp.push_back(x);
}
t.push_back(temp);
temp.clear();
}
for(int i=1;i<n+1;++i){
for(int j=1;j<w+1;++j){
if(weight[i-1]<=w){
t[i][j] = max(value[i-1]+t[i-1][w-weight[i-1]], t[i-1][j]);
}
else{
t[i][j] = t[i-1][j];
}
}
}
cout<<"Max Profit: "<<t[n][w];
// return final;
// vector<int> oneDimVector;
// for(int i = 0; i < n+1; i++){
// for(int j = 0; j < w+1; j++){
// oneDimVector.push_back(t[i][j]);
// }
// }
// vector<int>::iterator maxElement;
// maxElement = max_element(oneDimVector.begin(), oneDimVector.end());
// cout<<"Max Profit: "<<*maxElement;
}
int main(){
int n;
int w;//Total weight of knapsack
cin>>n;
cin>>w;
vector<int> weight;
vector<int> value;
for(int i=0;i<n;++i){
int x;
cin>>x;
weight.push_back(x);
}
for(int i=0;i<n;++i){
int x;
cin>>x;
value.push_back(x);
}
knapsack(weight,value,w,n);
}
I again debugged my code, I had to change one variable which I had written wrong in the following line of code:-
t[i][j] = max(value[i-1]+t[i-1][w-weight[i-1]], t[i-1][j]);
here, it should be:-
t[i][j] = max(value[i-1] + t[i-1][ j - weight[i-1]], t[i-1][j]);

newbie ask about decimal to binary in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
this program suppose to convert decimal to binary but somehow i screw it up
can some one point out the error for me?
thanks a lot
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0)) {
b[q]=a%2;
a=a/2;
q++;
}while(a>0);
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[q]);
}
}
Corrected code is:
#include<conio.h>
#include<stdio.h>
int main(){
int a;
int b[20];
int q = 0;
printf("decimal : ");scanf("%d",&a);
while(a>0) {
b[q]=a%2;
a=a/2;
q++;
}
printf("binary : ");
for (int i = q-1; i>=0;i--){
printf("%d",b[i]);
}
}
You were printing b[q] instead of b[i]
There are some problems with your code:
you added an extra ")" of the first while;
the second 'while' is useless (the code is being repeated due to the first one)
you are not printing the elements you want (you should use var 'i'), what you are really printing is the value after the last 0/1 (because you are using 'q')
code should look like this:
#include <conio.h>
#include <stdio.h>
int main() {
int a;
int b[20];
int q = 0;
printf("decimal: ");
scanf("%d", &a);
while (a > 0) {
b[q] = a % 2;
a = a / 2;
q++;
}
printf("binary: ");
for (int i = q - 1; i >= 0; i--) {
printf("%d", b[i]);
}
}

C++ unclear output [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find all the divisors of a number (n) and to add to the array those divisors that are at the 1st power (that appear only once), but I get in output just zeros, what's wrong with my code?
#include<iostream>
using namespace std;
int k,A[100000],n,p,d=2,pozitia=0;
int main()
{
cin>>n;
while(n>1)
{
p=0;
while(n%d==0)
{
p=p+1;
n=n/d;
}
if (p==1) { A[pozitia]=d; pozitia++; }
d=d+1;
}
for (int i=0;i<=pozitia;i++) cout<<A[pozitia]<<" ";
return 0;
}
You print always the same value:
for (int i=0;i<=pozitia;i++)
cout<<A[pozitia]<<" ";
It should be
for (int i=0;i<pozitia;i++)
cout<<A[i]<<" ";
Also pay attention that it should be i<pozitia and not i<=pozitia because you increment pozitia each time you insert a new value so at the end pozitia will point to a not initialized value in A.
I couldn't follow your logic for computing the divisors. It seems to be much simpler than you make it out to be.
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
Here's a program that uses that logic.
#include<iostream>
using namespace std;
void printDivisors(int A[], int pozitia)
{
for (int i=0;i<pozitia;i++) cout<<A[i]<<" ";
}
void fun(int n)
{
int A[100000];
int d = 2;
int pozitia=0;
int stop = n/2 + 1;
for ( ; d < stop; ++d )
{
if ( n % d == 0 )
{
A[pozitia]=d;
pozitia++;
}
}
printDivisors(A, pozitia);
}
int main()
{
int n;
cin>>n;
fun(n);
return 0;
}
The output for input of 100:
2 4 5 10 20 25 50

division of array on some criterion [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
in this code i try to divide the a[5] into two arrays on the criterion of stored data in the array .......
and store the index of the array a[5] to other arrays to show these index contain different data elements
but i doesn't work for me
#include <iostream>
using namespace std;
void printarray(int b[], int count)
{
int i;
for(i=0;i<count;i++)
cout<<b[i]<<endl;
}
void main()
{
int a[5]={1,0,0,1,1};
int array[5][5]=
{
{0,5,0,4,0},
{0,0,6,0,7},
{0,0,0,0,8},
{0,0,0,0,10},
{0,0,0,0,0}
};
int count=0;
int counti=0;
int C1=0;
int i;
for(i=0;i<5;i++)
{
if(C1==a[i])
{
count++;
}
else
{
counti++;
}
}
int *b= new int[count];
int *c= new int[counti];
for(i=0;i<5;i++)
{
if(C1==a[i])
{
b[i]=i;
}
else
{
c[i]=i;
}
}
printarray(b,count);
}
the code display the grabage values... plz help me
its show the following result
-842151450
1
The first i was 1 , so b will contain {1, 2}. where ( b[1] = 1, b[2] = 2 )
when you loop through b to print all elements you start from index 0 although b started from index 1.
you can fix index using (j, k instead of i).
int *b = new int[count];
int *c = new int[counti];
int j, k;
j = k = 0;
for (i = 0; i<5; i++)
{
if (C1 == a[i])
{
b[j++] = i;
}
else
{
c[k++] = i;
}
}
printarray(b, count);

issues with Storing to array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was testing one of my classes, but for some reason I can't seem to cast an intiger from a 2d array to double. Here is my (very simplified) code:
In main.cpp
#include<iostream>
#include<conio.h>
#include<string>
#include "trajectories.h"
int main()
{
std::string response;
int numOfCoords;
int speed;
int ** coords;
std::cout<<"enter the number of coordinates: ";
std::cin>>numOfCoords;
std::cout<<"enter speed: ";
std::cin>>speed;
coords=new int *[numOfCoords];
for (int i=0; i<numOfCoords; i++)
coords[i] = new int[2];
for(int i=0; i<numOfCoords*2; i++)
{
if(i%2==0)
std::cout<<"enter point "<<i/2<<".x : ";
else
std::cout<<"enter point "<<i/2<<".y : ";
std::cin>>coords[i/2][i%2];
}
NPCTrajectory traj(numOfCoords, speed);
traj.AddCoordinates(coords);
std::cout<<coords[0][0]<<", "<<coords[0][1]<<std::endl;
getch();
double currentCoords[2];
currentCoords[0]=double(coords[0][0]);
currentCoords[1]=double(coords[0][1]);
for(;;)
{
traj.HandleEvents(currentCoords);
std::cout<<"current coordinates : ("<<currentCoords[0]<<", "<<currentCoords[1]<<")"<<std::endl;
std::cout<<"do you wish to continue? ";
getch();
}
}
Trajectories.h contains class declaration only, so I believe it is irrelevant. Here is my trajectories.cpp
#include "trajectories.h"
int FPSCap=5;
NPCTrajectory::NPCTrajectory(int npoints, int newSpeed)
{
numOfPoints=npoints;
this->speed=newSpeed;
points = new int * [npoints];
for (int i=0; i<npoints; i++)
points[npoints] = new int[2];
state = 0;
maxOffset=speed/FPSCap;
}
void NPCTrajectory::AddCoordinates(int ** coordinates)
{
for(int i=0;i<this->numOfPoints; i++)
{
points[i][0]=coordinates[i][0];
points[i][1]=coordinates[i][1];
}
}
void NPCTrajectory::HandleEvents(double (&currentCoordinates)[2])
{
if(state+1==numOfPoints) return;
if(Distance(currentCoordinates[0], currentCoordinates[1], (double)points[state+1][0], (double)points[state+1][1])<maxOffset) state++;
double ratio = maxOffset/Distance(currentCoordinates[0], currentCoordinates[1], (double)points[state+1][0], (double)points[state+1][1]);
currentCoordinates[0]+=(points[state+1][0]-currentCoordinates[0])*ratio;
currentCoordinates[1]+=(points[state+1][1]-currentCoordinates[1])*ratio;
}
Please note that removing command traj.AddCoordinates(coords) will make the problem disappear. Am I passing the array correctly to the function?
The problem is in your constructor NPCTrajectory. Replace npoints with loop variable i. The following code:
for (int i=0; i<npoints; i++)
points[npoints] = new int[2];
should be like:
for (int i=0; i<npoints; i++)
points[i] = new int[2];
Because of this incorrect allocation, you are getting error (segmentation fault) in AddCoordinates function when you try to access points[i][0] with i=0 (assuming you are giving npoints>0 in NPCTrajectory).