Sudden variable reset - c++

Problem:
"maxPrint" resets to 0 out of nowhere.
In function "skaitymas" it complies to if, and changes itself to "p" finding the biggest one.
After the function is done, "maxPrint" suddenly becomes 0 again...
maxPrint is not even used anywhere after that..
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const char duomF[] = "1.txt";
const char rezF[] = "rez1.txt";
const int CMax = 81;
void reset(int SK[])
{
for (int i = 0; i < CMax; i++)
{
SK[i] = 0;
}
}
void skaitymas(int SK[], int &n, int &maxPrint)
{
ifstream df(duomF);
char temp;
int tempsk;
int p;
df >> n;
for (int i = 0; i < n; i++)
{
df >> p;
if (p > maxPrint)
{
maxPrint = p;
}
cout << p << " " << maxPrint << endl;
for (int j = CMax - p; j < CMax; j++)
{
df >> temp;
{ if (temp == '0') tempsk = 0;
else if (temp == '1') tempsk = 1;
else if (temp == '2') tempsk = 2;
else if (temp == '3') tempsk = 3;
else if (temp == '4') tempsk = 4;
else if (temp == '5') tempsk = 5;
else if (temp == '6') tempsk = 6;
else if (temp == '7') tempsk = 7;
else if (temp == '8') tempsk = 8;
else if (temp == '9') tempsk = 9;
}
SK[j] += tempsk;
}
}
df.close();
}
void skaiciavimas(int SK[])
{
int temp;
for (int i = CMax; i >= 0; i--)
{
if(SK[i] >= 10)
{
temp = SK[i] / 10;
SK[i-1] += temp;
SK[i] = SK[i] % 10;
}
}
}
int main()
{
int SK[CMax];
int n; int maxPrint = 0;
reset(SK);
skaitymas(SK, n, maxPrint);
skaiciavimas(SK);
for (int i = CMax - (maxPrint - 1); i < CMax; i++) cout << SK[i] << " ";
cout << maxPrint << endl;
ofstream rf(rezF);
for (int i = CMax - (maxPrint - 1); i < CMax; i++) rf << SK[i];
rf.close();
return 0;
}

In this loop you are accessing SK out of bounds:
void skaiciavimas(int SK[])
{
int temp;
for (int i = CMax; i >= 0; i--)
{
if(SK[i] >= 10) //<<< BUG (i = CMax)
{
temp = SK[i] / 10; //<<< BUG (i = CMax)
SK[i-1] += temp; //<<< BUG (i = 0)
SK[i] = SK[i] % 10; //<<< BUG (i = CMax)
}
}
}
Note that valid indices for SK are from 0 to CMax - 1, so accessing SK[CMax] results in undefined behaviour, as does accessing SK[-1].
Note that when you write to an array out of bounds you may well overwrite adjacent variables, which probably explains the unexpected modification of maxPrint, but as with any case of undefined behaviour, literally anything can happen.
Without knowing what your code is supposed be doing I can only guess that perhaps your for loop should be:
for (int i = CMax - 1; i > 0; i--)

Related

The roman character to integer convertion function is not working properly

I am getting segmentation fault to this code to convert roman numerals to numbers, i am getting segmentation error when i try the program in https://www.programiz.com/cpp-programming/online-compiler/ and i found that the inside of the loop A is never executed in my case
#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;
char str[50];
int a[50],m=50,val=0;
int intit(){
m=strlen(str);
for(int i=0;i<m;i++){//loop A
cout<<i;
if(str[i]=='M'){a[i]=1000;}
if(str[i]=='D'){a[i]=500;}
if(str[i]=='C'){a[i]=100;}
if(str[i]=='L'){a[i]=50;}
if(str[i]=='X'){a[i]=10;}
if(str[i]=='V'){a[i]=5;}
if(str[i]=='I'){a[i]=1;}
}
return 0;
}
int main(){
char str[50];
int a[50],m,val=0;
cin>>str;
cout<<"exit val" +char(intit());
for(int i=m-1;i>=0;i--){ //loop B
cout<<"inside evaluation loop"<<a[i];
if(a[i+1]>a[i]){val = val - a[i];}
else{val = val + a[i];}
}
cout<<"\n\n\nans:"<<val;
getch();
}
You use global and local variable with the same name. remove one of them and the program work correctly.
However it is better to send your parameter by reference than define global parameter:
int intit(char (& str)[50], int (&a)[50],int &m ,int &val ) {
m = strlen(str);
for (int i = 0;i < m;i++) {//loop A
cout << i;
if (str[i] == 'M') { a[i] = 1000; }
if (str[i] == 'D') { a[i] = 500; }
if (str[i] == 'C') { a[i] = 100; }
if (str[i] == 'L') { a[i] = 50; }
if (str[i] == 'X') { a[i] = 10; }
if (str[i] == 'V') { a[i] = 5; }
if (str[i] == 'I') { a[i] = 1; }
}
return 0;
}
int main() {
char str[50];
int a[50], m = 50, val =0;
cin >> str;
cout << "exit val" + char(intit(str,a,m,val));
for (int i = m - 1;i >= 0;i--) { //loop B
cout << "inside evaluation loop" << a[i];
if (a[i + 1] > a[i]) { val = val - a[i]; }
else { val = val + a[i]; }
}
cout << "\n\n\nans:" << val;
}

how to extract the coefficients and exponent of a polynomial as string

hello my problem is that i have to extract the coefficients and exponent of a polynomial given by the user.
when i tried my code it just worked for the coefficient, and for the exponent it gives me a zero. p.s a is just for testing
int main() {
char x[10];
char y[10];
char a[100] = "53x2+4x^3";
for (int i = 0; a[i] != '+'; i++)
{
if (a[i] != 'x')
{
x[i] = a[i];
}
}
for (int i = 0; a[i] != '+'; i++)
{
if ((a[i] == 'x') && (a[i + 1] == '^')) {
y[i] = a[i + 2];
}
}
double w;
int z;
w = atof(x);
z = atoi(y);
cout << w << endl;
cout << z << endl;
return 0;
}
You need to initialize your coefficient and exponent buffers with null terminators so that they are properly null terminated when the output console reads them.
#include <iostream>
using namespace std;
int main() {
char x[10] = {'\0'};
char y[10] = {'\0'};
char a[100] = "53x^2+4x^3";
for (int i = 0, j = 0; a[i] != '+'; i++)
{
if (a[i] != 'x')
{
x[j] = a[i];
j++;
}
}
for (int i = 0, j = 0; a[i] != '+'; i++)
{
if ((a[i] == 'x') && (a[i + 1] == '^')) {
y[j] = a[i + 2];
j++;
}
}
double w;
int z;
w = atof(x);
z = atoi(y);
cout << w << endl;
cout << z << endl;
return 0;
}

How to counting sort two dimensional array

Hi i have a problem with my code, i try sort array by counting sort (it must be stable sort), but my code doesn't work. I try implemented counting-sort from some wbesite, but it was in c# and I'm not sure s it done correctly. Can you tell me what is wrong with it?
#include <stdio.h>
#include <iostream>
using namespace std;
void sort_with_show(int **a, int rozmiar, int polecenie)
{
int y, d;
for (int i = 0; i< rozmiar - 1; i++)
{
for (int j = 0; j < rozmiar - 1 - i; j++)
{
if (a[j + 1][0] < a[j][0])
{
y = a[j][0];
a[j][0] = a[j + 1][0];
a[j + 1][0] = y;
}
}
}
for (int i = 0; i < rozmiar; i++)
{
//cout << tablica[i].x << " " << tablica[i].y << "\n";
if (polecenie == 0)
{
cout << a[i][0] << '\n';
}
else if (polecenie == 1)
{
cout << a[i][0] << "," << a[i][1] << "\n";
}
}
}
int main()
{
int rozmiar = 0;
int polecenie = 0;
char t[20] = { '\0' };
char *p, *q;
int liczba = 0;
int calaLiczba = 0;
bool isY = false;
cin >> rozmiar;
int ** a = new int *[rozmiar];
for (int i = 0; i < rozmiar; i++)
a[i] = new int[2];
cin.ignore();
int i = 0;
while(i < rozmiar)
{
fgets(t, sizeof t, stdin);
for (p = t, q = t + sizeof t; p < q; p++)
{
if (*p >= 48 && *p <= 57)
{
liczba = *p - 48;
calaLiczba = calaLiczba * 10 + liczba;
}
if (*p == ' ')
{
a[i][0] = calaLiczba;
isY = true;
calaLiczba = 0;
liczba = 0;
}
if (*p == '\n')
{
a[i][1] = calaLiczba;
isY = false;
}
}
for (int j = 0; j < 20; j++)
t[j] = '\0';
liczba = 0;
calaLiczba = 0;
isY = false;
i++;
}
cin >> polecenie;
cin.ignore();
sort_with_show(a, rozmiar, polecenie);
return 0;
}

c++ error: vector subscript out of range, line 1201

New to c++. I'm getting the "out of range" error message when try to debug the code. I tried to used resize(), but it is still not fixed. The code is to read instructions into a 2d vector and print out the graph.
What am I doing wrong?
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
typedef struct Pattern{
int rowNum;
int colNum;
char token;
bool isTriangular;
bool isOuter;
}Pattern;
void CommandProcessing(vector<string>&, Pattern&);
void Builder(Pattern&, vector<vector<char>>&);
void Printer(vector<vector<char>>&);
int main()
{
Pattern characters;
vector<vector<char>> key;
characters.colNum = 3;
characters.rowNum = 3;
characters.token = '#';
characters.isOuter = false;
characters.isTriangular = false;
Builder(characters, key);
Printer(key);
}
void Builder(Pattern& character, vector<vector<char>>& matrix)
{
int i = 0, j = 0;
char c;
if (character.token == 0)
c = 'a';
else
c = character.token;
matrix.resize(character.rowNum);
for (int i = 0; i < character.rowNum; i++){
if (character.isTriangular)
matrix[i].resize(i + 1);
else
matrix[i].resize(character.colNum);
if (character.isOuter)
{
if (character.isTriangular)
{
if (i = j)
matrix[i][j] = c;
else
matrix[character.rowNum - 1][i] = matrix[i][0] = c;
}
else
matrix[0][j] = matrix[i][0] = matrix[character.rowNum - 1][j] = matrix[i][character.colNum - 1] = c;
i++;
j++;
}
else
{
if (character.isOuter)
{
while (i <= j){
for (i = 0; i < character.rowNum; i++)
for (j = 0; i < character.colNum; j++)
matrix[i][j] = c;
}
}
else
for (i = 0; i < character.rowNum; i++)
for (j = 0; i < character.colNum; j++)
matrix[i][j] = c;
}
c++;
}
}
void Printer(vector<vector<char>>& print)
{
for (int i = 0; i < print.size(); i++){
for (int j = 0; j < print[i].size(); j++)
{
cout << print[i][j] << endl;
}
}
}
if (i = j) should be if (i == j).
if (i = j) do a assignment and test if i != 0.
Other problems:
while (i <= j){
for (i = 0; i < character.rowNum; i++)
for (j = 0; i < character.colNum; j++) // You test `i` instead of j
matrix[i][j] = c;
// Once test fixed
// Here `i == character.rowNum` and `j == character.colNum` (if `character.rowNum != 0`)
// So the while loop condition doesn't change and may so do infinite loop
}

4 bit Binary Addition in C++

We were given an assignment to make a program that takes two decimal numbers as input and performs binary addition in the background then outputs a decimal sum. I seem to be getting the wrong results despite how much I think my code logic is correct.
My current sum gives me 0. The Correct answer should be 24.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int add(int, int);
int decbin(int);
int bindec(long);
int binADD(string, string);
int main()
{
int a = 15;
int b = 9;
int z;
z = add(a,b);
cout << z;
return 0;
}
int add(int a, int b)
{
int z;
ostringstream x, y;
x << decbin(a);
y << decbin(b);
z = binADD(x.str(), y.str());
return z;
}
int decbin(int x)
{
int d[16];
int i = 0;
int j;
int ans;
while(x > 0)
{
d[i] = x % 2;
i++;
x = x / 2;
}
for(j = i - 1; j >= 0; j--)
{
ans = d[j];
}
return ans;
}
int bindec(int x)
{
int bin, dec = 0, rem, base = 1;
bin = x;
while (x > 0)
{
rem = x % 10;
dec = dec + rem * base;
base = base * 2;
x = x / 10;
}
return dec;
}
int binADD(string a, string b)
{
int carry = 0;
int result[5];
int res;
int ans;
for(int i = 0; i < 4; i++)
{
if(a[i] == '1' && b[i] == '1' && carry == 0)
{
result[i] = 0;
carry = 1;
}
else if(a[i] == '0' && b[i] == '1' && carry == 1)
{
result[i] = 0;
carry = 1;
}
else if(a[i] == '1' && b[i] == '1' && carry == 1)
{
result[i] = 1;
carry = 1;
}
else if(a[i] == '1' && b[i] == '0' && carry == 1)
{
result[i] = 0;
carry = 1;
}
else if(a[i] == '1' && b[i] == '0' && carry == 0)
{
result[i] = 1;
carry = 0;
}
else if(a[i] == '0' && b[i] == '0' && carry == 1)
{
result[i] = 1;
carry = 0;
}
else if(a[i] == '0' && b[i] == '1' && carry == 0)
{
result[i] = 1;
carry = 0;
}
else if(a[i] == '0' && b[i] == '0' && carry == 0)
{
result[i] = 0;
carry = 0;
}
}
result[4] = carry;
for(int j = 4; j >= 0; j--)
{
res = result[j];
}
ans = bindec(res);
return ans;
}