Last value of the array 'disapears' - c++

So, i am currently doing a project. And while debuging i noticed that sometimes, a variable from the array changes value without me changing it.
The array is created to hold some values from tree structure.
int* tablica_synow1;
tablica_synow1 = (int*)malloc(sizeof(int) * number_of_sons(wskaznik_wierzcholki[zmienna1][i]));
//cout << "Initializing values to tablica_synow1" << endl;
for (int k = 0; k < ilu_synow(wskaznik_wierzcholki[zmienna2][j]); k++) {
if (k == 0)
tmp = wskaznik_wierzcholki[zmienna2][j]->son;
tablica_synow1[k] = tmp->key;
//cout << "tablica_synow1 [" << k << "]" << tablica_synow1[k] << endl;
tmp = tmp->brother;
}
//cout <<"end of initialization"<< endl;
after this I do some other things in my code that don't involde tablica_synow1
and then when i want to use it
for (int m = 0; m < ilu_synow(wskaznik_wierzcholki[zmienna2][j]); m++) {
int x = tablica_synow1[m];
cout << "tablica synow1 [" << m << "]" << tablica_synow1[m] << endl;
int y = i;
int a;
if (x > 0)
a = tablica2[y][zwroc_indeks(wartosci_tab[zmienna2], x)];
else
a = tablica4[y][x * (-1)];
if (a > najwieksza)
najwieksza = a;
}
the last element goes from 2 to -123781237 and it breaks my code
image

Related

how I can print an array as a vector form

I want to cout an array as a row vector but when I write:
int main() {
int B[3]={0};
for (int w = 0; w <2; w++) {
cout <<"B="<<" "<< B[w] << " ";
}
cout << endl;
return 0;
}
The output is B=0 B=0
But I want output to be like:
B=(0 0)
For a fixed size array of only I would probably even prefer a oneliner like this, because I can read it at first glance:
cout << "B=(" << B[0] << " " << B[1] << " " << B[2] << ")\n";
For a container B with a dynamic or very high number of elements n, you should probably do something like this:
cout << "B=(";
if(n > 0)
{
cout << B[0];
// note the iteration should start at 1, because we've already printed B[0]!
for(int i=1; i < n; i++)
cout << ", " << B[i]; //I've added a comma here, so you get output like B=(0, 1, 2)
}
cout << ")\n";
This has the advantage, that no matter what number of elements, you don't end up with trailing commas or unwanted whitespace.
I'd reccommend making a generic (template) function for the purpose of printing array/std::vector content anyways - it's really useful for debugging purposes!
int main() {
int B[3] = { 0 };
cout << "B=(";
for (int w = 0; w < 3; w++) {
cout << B[w];
if (w < 2) cout << " ";
}
cout << ")" << endl;
return 0;
}
Output should be now:
B=(0 0 0)
The simplest way to do this is:-
#include<iostream>
using namespace std;
int main()
{
int B[3]={0};
cout << "B=(";
for (int w = 0; w < 3; w++)
{
cout << B[w] << " ";
}
cout << ")" << endl;
return 0;
}
the output will be B= (0 0 0 )
You can try this one if you want:
#include <iostream>
using namespace std;
int main() {
int B[3]={0};
cout << "B=(";
for (int w = 0; w <2; w++) {
cout << B[w];
if(w != 1) cout << " ";
}
cout << ")" << endl;
cout << endl;
return 0;
}
The output is:
B=(0 0)
The line if(w != 1) checks whether you 've reached the last element of the array. In this case the last index is 1, but in general the if statement should be: if(w != n-1) where n is the size of the array.

global variable and class versus local

I am writing a program, it is an optimization problem. As part of the problem I have defined a class in which I save the optimization parameters. It is composed of some vectors that I resize them and give initial values to them in a function. The class is as follows:
typedef class Chrom
{
public:
vector<vector <short int>> bit;
vector<vector <float>> WaitingTime;
vector <short int> WaitingJob;
vector<vector <float>> StartTime;
vector<vector <float>> finish;
//short int FinishTime;
float fit;
void variablesresize(){
WaitingTime.resize(Jobs);
WaitingJob.resize(Jobs);
StartTime.resize(Jobs);
finish.resize(Jobs);
bit.resize(Jobs);
//cout << "machine size is:" << Machines;
for (int i = 0; i < Machines - 1; ++i)
{
WaitingTime[i].resize(Jobs);
}
for (int i = 0; i < Machines; ++i)
{
StartTime[i].resize(Jobs);
finish[i].resize(Jobs);
bit[i].resize(Jobs);
}
}
} chrom;
Then I define two classes of this type (popcurrent and popnext) and also functions y and x before main function:
chrom popcurrent[populationsize];
chrom popnext[populationsize];
void *initialize(chrom popcurrent[populationsize]); //defining the
functions that we will use
chrom x(chrom popcurrent,int pindex);
float y(chrom chromp);
void *selection(chrom popcurrent[populationsize]);
void *crossover(chrom popnext[populationsize]);
void *mutation(chrom popnext[populationsize]);
Problem ProblemConstraint;
void SetProblemSize(short int &Machines, short int &Jobs);
void vectordimension(chrom ch[populationsize]);
void ytest(chrom popcurrent);
void main()
{
My Issue is that whenever I call x and y within other functions and I assign some values in other functions, it gives back only zero for the members of those classes. For example below is a part of assigning value in function x:
chrom x(chrom popnext,int pindex)
{
int z = 0, i, j, k, tempindex, previousjobindex, l;
float temp;
//sorting chromosoms based on the population
//if
cout << '\n';
cout << "popcurrent.bit[0][0] =" << popnext.bit[0][0] << '\n';
cout << "popcurrent.bit[0][1] =" << popnext.bit[0][1] << '\n';
//for the first machine
//for (int i = 0; i < populationsize;i++)
for (i = 0; i < Machines; i++)//**just for test
for (j = 0; j < Jobs; j++)//**just for test
{
cout << "popcurrent.bit[" << i << "][" << j << "] =" << popnext.bit[i]
[j] << '\n';
}
for (int j = 0; j < Machines; j++)//after first machine so j=1 and not 0
{
for (int k = 0; k < Jobs; k++)
{
for (l = 0; l < Jobs; l++)
if (popnext.bit[j][l] == 1)
{
cout << "j= " << j << "k = " << k;
popnext.WaitingTime[j][k] = ProblemConstraint.t1[j][k];
popnext.StartTime[j][k] = popnext.finish[j - 1][k] +
popnext.WaitingTime[j - 1][k];
popnext.finish[j][k] = popnext.WaitingTime[j][k] + ProblemConstraint.Processing[j][k] + popnext.StartTime[j][k];
cout << "
cout << "popcurrent.StartTime[" << j << "][" << k << "]= " << popnext.StartTime[j][k] << " popcurrent.finish[j - 1][k]" << popnext.finish[j - 1][k] << '\n';
cout << " popcurrent.WaitingTime[" << j - 1 << "][" << k << "]= " << popnext.WaitingTime[j - 1][k] << '\n';//start time of machine j is equal to finish time of machine j-1
}
}
for (k = 1; k < Jobs; k++)
{
if (popnext.bit[j][k] == k + 1)
{
for (int l = 0; l < Jobs; l++)
if (popnext.bit[j][l] == 1 + k)
{
temp = popnext.finish[j][l];//finish time of previous job
tempindex = l;
}
popnext.StartTime[j][k] = temp;
if (popnext.StartTime[j][k] < popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k]);
popnext.StartTime[j][k] = popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k];//we need to update the start time and also we need to update w as well.
else if (popnext.StartTime[j][k] > popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k])
popnext.WaitingTime[j - 1][k] = popnext.StartTime[j][k] - popnext.finish[j - 1][k];//we need to update the waiting time which is greater than t1 here.
popnext.finish[j][k] = popnext.StartTime[j][k] + ProblemConstraint.Processing[j][k] + popnext.StartTime[j][k];
popnext.WaitingTime[j][k] = ProblemConstraint.t1[j][k];
cout << "popcurrent.finish[" << j << "][" << k << "]= " << popnext.finish[j][k] << '\n';//debugging code
}//start of '{' is: else if (popcurrent.bit[j][k] >0)
}
}
cout << "In x functionn, popcurrent.finish[Machines - 1][k]= " <<
popnext.finish[1][2] << '\n';//debugging cout
return(popnext);
}
The function returns popnext but I call it with x(popcurrent,pindex) and x(popnext,pindex). The problem is that after calling I expect that values of members such as finish change but they do not change and therefore when I have
x(popcurrent) ;
Because of last command in x function it prints out popcurrent.finish[Machines - 1][k]=32 but when I have a cout in another function such as main it gives me popcurrent.finish[Machines - 1][k]=0;
It only gives me zero for all values (I assigned zero to vector values in another function while defining them which is initial values of the vector). I wonder why this happens while I have defined popcurrent and popnext global classes?

Printin a X with *

i wanna print a X with * , i have done the left side of the X but i don't know how to print the other side (flip/mirror) .
if you run this codes it will print just the left side of (X) and now i wanna print the right side of (X) ? so what should i do to complete the (X) using stars(*)? thank you guys.
i was wondering is it possible to do this?(i'm a newbie to programming)
#include <iostream>
// Expected output pattern:
//
// * *
// * *
// * *
// *
// * *
// * *
// * *
using namespace std;
int main() {
cout << "Printing X with star(*)" << endl;
cout << endl;
int i;
int p;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "*";
}
cout << endl;
}
return 0;
}
You're on the right track, to do the right hand side you have to print more **** on each line in addition to what you already have done. It might help to think of printing each line of the X as printing some **** then some spaces then more **** and reduce the number of spaces each time you get closer to the cross-over point. Does that make sense? This might help get you further (. = space):
*......*
.*....*
..*..*
...**
and so on
This is one of many ways you could get there:
int main()
{
int size = 8;
int spacesBefore;
int spacesBetween = size;
int numStars = 1;
// Top half:
int i, j;
for ( i = 0; i < size/2; i++ ) {
spacesBetween = size - ( 2 * ( i + 1 ) );
spacesBefore = i;
for ( j = 0; j < spacesBefore; j++ ) // before
cout << " ";
for ( j = 0; j < numStars; j++ ) // * left
cout << "*";
for ( j = 0; j < spacesBetween; j++ ) // between
cout << " ";
for ( j = 0; j < numStars; j++ ) // * right
cout << "*";
cout << endl;
}
// bottom half, do the same kind of thing but changing the spacings
// ...
}
ok thank you every one that helped me , i found the answer i wanted after almost 6 hours and here is the answer:
#include <iostream>
using namespace std;
int main() {
cout << "Printing X with stars" << endl;
cout << endl;
int i;
int p;
int k;
int s;
int count = 72;
for (i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++){
cout << " ";
}
count-=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
}
count = 0;
for (i = 10; i >= 1; i--) {
for (int j = 1; j <= 10; j++) {
if (j > i) break;
cout << " ";
cout << "\t";
}
cout << "\t\t\t\t";
for (p = 1; p <= 10; p++) {
cout << "* ";
}
for (k=1; k<=count; k++) {
cout << " ";
}
count +=8;
for (s=1; s<=10; s++){
cout << "* ";
}
cout << endl;
if (count == 80) break;
}
return 0;
}

How do I exit a loop in C++ without using break?

I'm writing a code to swap integers in an array and I want to know how I can exit a loop without using a break statement and keeping my logic consistent. Here is my code below:
int swapped = 0;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
break;
}
}
if (swapped) {
break;
}
Use goto [I'll be bashed because of this].
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
for (int i = 1; i < arraycount; ++i)
{
for (int j = 1; j < arrays[i][0] + 1; ++j)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value "
<< arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
goto done;
}
}
done:
something;
for (int i = 1; i < arraycount && !swapped; ++i)
{
for (int j = 1; j < arrays[i][0] + 1 && !swapped; ++j)
{
if(arrays[i][j] % 2 != 0)
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with " << "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = 1;
}
}
}
this will do the same thing you have in inner loop.
Using a Break statement does not necessarily make your codes logic inconsistent and breaks are often useful to improve the readability of your code. But in answer to your question this can be achieved by utilizing while loops and logical boolean operators. A modified version of your code is below, I have tried to modify it as little as possible so you can still see your code within the example. There are a few logical errors in your code that I have left in the example below that you might want to look into. In particular the line below will print "is odd" when in fact the number would be even. If you where wanting to check if the number arrays[0][first] is odd then the following if statement would be needed if (arrays[0][first] % 2 != 0) instead of if (arrays[0][first] % 2 == 0).
Logical Error
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
This is the code without using breaks.
bool swapped = true;
if (arrays[0][first] % 2 == 0)
{
cout << arrays[0][first] << " is odd " << endl;
int i = 1;
while ( (i < arraycount) && swapped)
{
int j = 1;
bool if_odd = true;
while ((j < arrays[i][0] + 1) && if_odd)
{
if (arrays[i][j] % 2 != 0)
{
int temp = arrays[i][j];
cout << "Array #" << 1 << " value " << arrays[0][first] << " swapped with "
<< "Array #" << i << " value " << temp;
arrays[i][j] = arrays[0][first];
arrays[0][first] = temp;
swapped = false;
if_odd = false;
}
j++;
}
i++;
}
}

bad access in C++ where the variables in the for loops are not staying within given limits

So this is a method to iterate through a matrix of chars to find specific words in a dictionary file. The current problem is a bad access indicated below.... what is causing this is that d2 = like a million and l also equals a value in the hundreds of thousands.... however, my limits are clearly not even close to that.....
void Puzzle:: algorithm2(){
int numberofwords = 0;
for (int r = 0; r<Rows; r++) {
for (int c = 0; c<Columns; c++){
for (int d1 = -1; d1<=1; d1++){
for (int d2 = -1; d2<=1; d2++){
//42 ~ 30(2)^0.5
for (int l = dictionary.getminlength(); l<30; l++) {
if (d1==d2==0){
continue;
}
else if ((r+(d1*l))>30||(c+(d2*l))>30||(c+(d2*l))<0||(r+(d1*l))<0){
break;
}
else{
string tempword = "";
for (int p = 0; p<l+1; p++) {
tempword[p] = TheBoard[r+(d1*p)][c+(d2*p)];//badacces
if(dictionary.BinarySearch(tempword)){
cout << "Found " << tempword << " at (" << r << "," << c << ") to (" << r+d1*(int)tempword.length() << "," << c+d2*(int)tempword.length() << ")" << endl;
numberofwords++;
}
}
}
}
}
}
}
}
cout << numberofwords << "words" << endl;
}