converting an array of integers to string - c++

If I have an array that looks like
int digits[size] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4}
I want to remove the leading zeros and to do so I'm attempting to convert the array of integers into a string (which is an array of chars).
My attempt looks like
string toString(int digits[], int size){
string number = " ";
for(int i = 0; i < size - 1; i++){
number[i] = digits[i];
}
return number;
}
which came out horribly broken.
I also can't simply remove all zeros, just the leading ones.
Also if I may dogpile another question here:
how can I identify if a string is numeric?
e.g
string number = "12a4"
cout << "not a number"

you can use C++11 function std::to_string() here is an example
#include <string>
#include <iostream>
int main()
{
int size = 15;
int digits[size] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
std::string result = "";
for (int i = 0; i < size; i++)
{
if (!(digits[i] == 0 && result.size() == 0))
result += std::to_string(digits[i]);
}
std::cout << result << std::endl;
}
you can check if a string is numeric using this function
bool isNb(std::string str)
{
if (str.size() == 0)
return false;
for (int i = 0; i < str.size(); i++)
{
if (std::isdigit(str.at(i)) == false)
return false;
}
return true;
}

Instead of changing digits with your for loop, add them with
number += to_string(digits[i]);
Also, you can remove the toString line you have, and just use it as I put here.
As to your other question, just use a for loop to check each digit in the string and its ASCII value, if there is any whose ASCII value is less than 48 or greater than 57 then it's not a number.

Try the following way:
int i = 0;
while(digits[i] == 0) i++;
for (; i < size; i++)
result += to_string(digits[i]);

To answer your actual question (How to remove the leading zeros?) here a solution without strings:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
std::vector<int> x = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
// ...or if you insist on the array...
// int x[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
// std::vector<int> x = {x,x+15};
auto it = std::find_if_not(x.begin(),x.end(),[](int i){return i==0;});
std::vector<int> y{it,x.end()};
for (auto i : y) std::cout << i << " ";
}
prints:
1 2 3 0 4

You can use a string stream to convert from any type to string:
#include <sstream> //<-- ALLOWS USE OF std::stringstream
const int size = 15;
int digits[size] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
std::stringstream ss; //<-- DECLARE STREAM
int k;
for (k = 0; k < size; ++k)
if (digits[k] != 0)
break; //FIND FIRST NON-0 INDEX
for (int i = k; i < size; ++i)
ss << digits[i]; //ADD TO THE STREAM s
std::cout<< ss.str() << std::endl; //PRINT STREAM
12304

Related

Create multiple objects for a simulation

Basically I wrote code to simulate one encoded data vector over a AWGN channel. The simulation but only works once. So I would like to create multiple objects or find a way to run the code multiple times depending on int N (for example int N = 1000000; in my case), so that I can calculate the BER (bit error rate).
I haven't found an elegant way to do that yet though...
I hope you understand my question.
Do you need more information?
Thank you!!
#include <iostream>
#include "encode.h"
#include "awgn.h"
#include "decode.h"
using namespace Eigen;
int main()
{
std::string code = "Hamming";
int dim_u, dim_mat_col, dim_mat_row, dim_mat_col_H, dim_mat_row_H;
MatrixXi P;
if (code == "Hamming")
{
dim_u = 4; // can also call it "k"
dim_mat_col = 7; // also serves as dim of x and y, or simply n
dim_mat_row = 4;
dim_mat_col_H = dim_mat_col;
dim_mat_row_H = dim_mat_col - dim_mat_row;
P = MatrixXi::Zero(dim_u, dim_mat_col - dim_u);
P << 1, 1, 0,
0, 1, 1,
1, 1, 1,
1, 0, 1;
}
if (code == "BCH")
{
dim_u = 7;
dim_mat_col = 15; // also serves as dim of x and y, or simply n
dim_mat_row = 7;
dim_mat_col_H = dim_mat_col;
dim_mat_row_H = dim_mat_col - dim_mat_row;
P = MatrixXi::Zero(dim_u, dim_mat_col - dim_u);
P << 1, 1, 1, 0, 1, 0, 0, 0,
0, 1, 1, 1, 0, 1, 0, 0,
0, 0, 1, 1, 1, 0, 1, 0,
0, 0, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 0, 1, 1, 0,
0, 1, 1, 1, 0, 0, 1, 1,
1, 1, 0, 1, 0, 0, 0, 1;
}
if (code == "Golay")
{
dim_u = 12;
dim_mat_col = 24; // also serves as dim of x and y, or simply n
dim_mat_row = 12;
dim_mat_col_H = dim_mat_col;
dim_mat_row_H = dim_mat_col - dim_mat_row;
P = MatrixXi::Zero(dim_u, dim_mat_col - dim_u);
P << 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0,
0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1,
1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0,
1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0,
1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1,
1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0,
0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1,
0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1,
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1;
}
int N = 1000000; // number of simulations
bool c_hat_minus_c = 0;
int val = 0;
Encode vec(dim_u, dim_mat_row, dim_mat_col);
awgn channel(dim_mat_col);
Decode dec(dim_mat_col, dim_mat_row_H, dim_mat_col_H, P);
vec.encodeDataVector(dim_u, dim_mat_col, P);
// std::cout << "modulated x: " << vec.x << std::endl;
channel.addGausian(vec.x);
// std::cout << channel.y << std::endl;
c_hat_minus_c = dec.decodingalg(6000, channel.y, P, vec.x); // check if codeword is received correctly
// std::cout << channel.y << std::endl;
// std::cout << "val: " << val << std::endl;
}
If I wrap the stack allocated objects in a for loop like this:
for (int i = 0; i < N; i++)
{
Encode vec(dim_u, dim_mat_row, dim_mat_col);
awgn channel(dim_mat_col);
Decode dec(dim_mat_col, dim_mat_row_H, dim_mat_col_H, P);
vec.encodeDataVector(dim_u, dim_mat_col, P);
// std::cout << "modulated x: " << vec.x << std::endl;
channel.addGausian(vec.x);
// std::cout << channel.y << std::endl;
c_hat_minus_c = dec.decodingalg(6000, channel.y, P, vec.x); // check if codeword is received correctly
// std::cout << channel.y << std::endl;
// std::cout << "val: " << val << std::endl;
}
The program breaks and says:
/usr/include/eigen3/Eigen/src/Core/CommaInitializer.h:97: Eigen::CommaInitializer& Eigen::CommaInitializer::operator,(const Eigen::DenseBase&) [with OtherDerived = Eigen::Matrix<int, -1, -1>; XprType = Eigen::Matrix<int, -1, -1>]: Assertion `(m_col + other.cols() <= m_xpr.cols()) && "Too many coefficients passed to comma initializer (operator<<)"' failed.
Edit:
so I basically found out that it braks in encode.cpp
the second time it tries to initialize the Matrix G_
#include <iostream>
#include "encode.h"
#include "awgn.h"
#include <cstdlib> // rand and srand
#include <ctime> // For the time function
using namespace std;
using namespace Eigen;
Encode::Encode(int dim_u, int dim_mat_row, int dim_mat_col) //(7,4) Hamming code only up to now
{
// if (code == "Hamming")
// dim_u = 4;
// dim_mat_col = 7;
// dim_mat_row = 4;
u_ = RowVectorXi::Zero(dim_u);
G_ = MatrixXi::Zero(dim_mat_row, dim_mat_col);
}
void Encode::encodeDataVector(int dim_u, int dim_mat_col, MatrixXi &P)
{
// Get the system time.
unsigned seed = time(0);
// Seed the random number generator.
srand(seed);
for (int i = 0; i < dim_u; i++)
{
u_(i) = rand() % 2; // only zeros and ones
}
// cout << u_ << endl << endl;
MatrixXi I;
// I = MatrixXi::Zero(7, 7);
I = MatrixXi::Identity(dim_u, dim_u);
G_ << I, P; **<----- here**
// cout << G_ << endl << endl;
x = u_ * G_;
for (int i = 0; i < dim_mat_col; i++)
{
x(i) = x(i) % 2;
}
// std::cout << "correct codeword: " << x << std::endl;
// mapping for BPSK
for (int i = 0; i < dim_mat_col; i++)
{
if (x(i) == 0)
x(i) = 1;
else
x(i) = -1;
}
// awgn::awgn channel(dim_mat_col);
// channel.addGausian(this->x);
}
P is being passed by non-const reference, which means it may be modified by the functions you call. Passing a copy of P in each iteration makes sure that the modifications to P stay local to that iteration:
for (int i = 0; i < N; i++) {
MatrixXi P_copy = P;
...
}

My sudoku backtrack algorithm isn't working after the 7th line. What am I doing wrong?

My code runs but when I try to print the board using a function and when I check if the algorithm had aleast solved the board, it only solves up to the 7th line then stops.
In my process to see what I was doing wrong I printed my board right after the if statement that includes checkforzero in the last function that is initialized. That is how I found out that my code only solves until the seventh line of the puzzle. It also confuses me on how even through I reference the sodoku_board vector it still doesn't output a value of the changed 7 lines after solving it through the algorithm in the main function.
#include<iostream>
#include<vector>
#include<algorithm>
void print_board(std::vector<std::vector<int>> sudoku_board);
std::vector<std::vector<int>> missing_nums(std::vector<std::vector<int>> sudoku_board);
bool check_viable(std::vector<std::vector<int>> sudoku_board, int row, int column, int number);
bool checkforzero(std::vector<std::vector<int>> sudoku_board, int &row, int &column);
bool solve_board(std::vector<std::vector<int>> &sudoku_board, std::vector<std::vector<int>> missing);
int main(){
std::vector<std::vector<int>> missing;
//created board
std::vector<std::vector<int>> sudoku_board = {
{1, 0, 8, 0, 0, 5, 0, 0, 6},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{5, 0, 3, 8, 2, 0, 7, 0, 0},
{2, 0, 0, 1, 5, 0, 9, 0, 8},
{0, 0, 0, 0, 8, 0, 0, 0, 0},
{8, 0, 9, 0, 4, 2, 0, 0, 5},
{0, 0, 5, 0, 9, 8, 2, 0, 4},
{0, 0, 0, 0, 0, 0, 0, 0, 0},
{9, 0, 0, 2, 0, 0, 6, 0, 1}
};
print_board(sudoku_board);
missing = missing_nums(sudoku_board);
bool end = solve_board(sudoku_board, missing);
print_board(sudoku_board);
}
//simply printing the board inputed
void print_board(std::vector<std::vector<int>> sudoku_board){
std::cout<<"\n";
for(auto c : sudoku_board){
for(auto d : c){
std::cout<<d<<" ";
}
std::cout<<"\n";
}
std::cout<<"\n";
}
//find the missing nums in each row
std::vector<std::vector<int>> missing_nums(std::vector<std::vector<int>> sudoku_board){
std::vector<std::vector<int>> r(9);
for(int c = 0; c < 9; c++)
for(int i = 1; i <= 9; i++)
if(find(sudoku_board[c].begin(), sudoku_board[c].end(), i) == sudoku_board[c].end())
r[c].push_back(i);
return r;
}
//checks if the number can be inputed on board
bool check_viable(std::vector<std::vector<int>> sudoku_board, int row, int column, int number){
int row_change;
//checks vertical
for(int rowed : sudoku_board[row])if(rowed == number)return false;
//checks horizontal
for(int i = 0; i < sudoku_board.size(); i++)if(sudoku_board[i][column] == number)return false;
//checks box
while(row != 0 && row != 3 && row != 6)row--;
while(column != 0 && column != 3 && column != 6)column--;
for(int v = column; v <= column + 2; v++)if(sudoku_board[row+1][v] == number || sudoku_board[row+2][v] == number || sudoku_board[row][v] == number)return false;
return true;
}
bool checkforzero(std::vector<std::vector<int>> sudoku_board, int &row, int &column){
for(int i = 0; i < 9; i++){
for(int v = 0; v < 9; v++){
if(sudoku_board[i][v] == 0){
row = i;
column = v;
return true;
}
}
}
return false;
}
bool solve_board(std::vector<std::vector<int>> &sudoku_board, std::vector<std::vector<int>> missing){
int row, column;
if(!checkforzero(sudoku_board, row, column))
return true;
print_board(sudoku_board);
for(int d = 0; d < missing[row].size(); d++){
if(check_viable(sudoku_board, row, column, missing[row][d])){
sudoku_board[row][column] = missing[row][d];
if(solve_board(sudoku_board, missing))
return true;
sudoku_board[row][column] = 0;
}
}
return false;
}

GSL complex matrix - eigenvalues/eigenvectors

I've written a program which is computing the eigenvalues and eigenvectors of a hermitian matrix.
Does anyone know how this is done in GSL properly? Here is what I already have.
//hermitian matrix
0 1 0 -i
1 0 -i 0
0 i 0 1
i 0 1 0
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_complex_math.h>
#include <gsl/gsl_eigen.h>
using namespace std;
const int N = 4;
int main(){
gsl_eigen_hermv_workspace *workN = gsl_eigen_hermv_alloc(N);
gsl_matrix_complex *A = gsl_matrix_complex_alloc(N, N);
gsl_complex i = gsl_complex_rect(0.0,1.0);
gsl_complex ii = gsl_complex_rect(0.0,-1.0);
gsl_vector *eval = gsl_vector_alloc(N);
gsl_matrix_complex *evec = gsl_matrix_complex_alloc(N, N);
double mTab[] = {
0, 1, 0, 5,
1, 0, 5, 0,
0, 5, 0, 1,
5, 0, 1, 0
};
gsl_matrix_complex_view tmpM = gsl_matrix_complex_view_array(mTab, N, N);
gsl_matrix_complex_memcpy(A, &tmpM.matrix);
gsl_matrix_complex_set(A, 0, 3, ii);
gsl_matrix_complex_set(A, 1, 2, ii);
gsl_matrix_complex_set(A, 2, 1, i);
gsl_matrix_complex_set(A, 3, 0, i);
gsl_eigen_hermv(A, eval, evec, workN);
for(int i=0; i < N; i++){
for(int j=0; j < N; j++){
gsl_complex z = gsl_matrix_complex_get(A, i, j);
cout << GSL_REAL(z) << "+ i" << GSL_IMAG(z) << " ";
}
cout << "\n";
}
cout << "\n";
for(int i=0; i < N; i++){
cout << gsl_vector_get(eval, i) << " ";
}
return 0;
}
This is how I output the eigenvectors
for(int i=0; i < N; i++){
for(int j=0; j < N; j++){
gsl_complex z = gsl_matrix_complex_get(A, i, j);
cout << GSL_REAL(z) << "+ i" << GSL_IMAG(z) << " ";
}
cout << "\n";
}
Finally, here's the way I declared the matrix in question.
double mTab[] = {
0, 1, 0, 5,
1, 0, 5, 0,
0, 5, 0, 1,
5, 0, 1, 0
};
Later, I added the complex numbers.
I managed to print the eigenvectors but I don't know how to do that for the eigenvalues. Any help with that is appreciated?.
You have an error in using the double mTab for gsl_matrix_complex_view_array. This assumes an array of complex numbers represented as real parts followed by imaginary parts in one large array of double values. You can change your definition to:
double mTab[] = {
0, 0, 1, 0, 0, 0, 5, 0,
1, 0, 0, 0, 5, 0, 0, 0,
0, 0, 5, 0, 0, 0, 1, 0,
5, 0, 0, 0, 1, 0, 0, 0,
};
(Which also means you don't need to use a "dummy" variable of 5 just to rewrite it by ±i later.) Then the code for printing eigenvalues works well.
Also you have a typo in the eigenvector printing loop: it should be
gsl_complex z = gsl_matrix_complex_get(evec, i, j);
not
gsl_complex z = gsl_matrix_complex_get(A, i, j);

String to word break using C++

i have written sample code to string to word convert, but not able to understand the reason why output coming as My only.
below help me out the reason why this is happening like this.
//g++ 5.4.0
#include <iostream>
#include <string>
using namespace std;
int main()
{
char abc[20] = "My name is abc";
char result[5][10]= {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
int k = 0,j=0;
for (int i =0; abc[i]!='\0'; i++,j++)
{
result[k][j] = abc[i];
if (abc[i] == ' ')
{
result[k][j] = '\0';
cout<<result[k]<<endl;
k++;
j=0;
}
}
}
output of the below code is coming as My only not the next word after My.
result[k] (inside if condition) return only My not the next coming word.
there's j++ every loop, so j=0 is set to j=1 in next loop.
This leads to result[k][0] = 0 for every k>0.
And it is also need to deal abc[i] = '\0' case, otherwise last word don't appear.
for (int i =0; ; i++,j++) {
result[k][j] = abc[i];
if (abc[i] == ' ' || abc[i] == '\0') {
result[k][j] = '\0';·
cout<<result[k]<<endl;
k++;
j=-1;
}
if (abc[i] == '\0') break;
}
Make
j = -1;
inside the if statement. Rest you can figure it out.

C++ Program stuck in a loop

So this program is supposed to take a string and convert it to Reverse Polish Notation, and then generate the assembly code for it.
For example. If I was to input "x = y", the program would return
"RPN : xy="
"Code:
1 lod y
2 sto x"
However the program instead returns gibberish and continues to do so until it runs out of memory.
Here's the input function
void
getstring()
{
if(datafile) {
file.getline(str,241);
}else{
cout << "Enter a string, please \n\n";
cin.getline( str, 241);
}
nstring++;
}
And these are the functions that work with it.
void
internalize()
{
int i, j;
static char inter[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 6, 4, 0, 5, 0, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,
0,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
23,24,25,26,27,28,29,30,31,32,33, 0, 0, 0, 0, 0,
0,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
49,50,51,52,53,54,55,56,57,58,59, 0, 0, 0, 0, 0};
char ch, code, k, *p, *q;
cout << "internal form: \n\n";
k=0;
q=inform;
for(p=str;*p;p++){
*q++ = code = inter[(int)*p];
if(k+code == 0){
k = p-str+1;
ch = *p;
}
}
*q = 0;
for(i=j=0, p = inform;p++,j++;j<len){
cout << setw(3) << (int)*p;
if(++i == 16){
cout << '\n';
i = 0;
}
}
if (i !=0){
cout <<'\n';
}
if((err = (0<k))!=0){
cout << '\n**' << (int)k << "-th nonblank char <" <<ch<<"> is illegal. **\n";
}
}
void
makerpn()
{
static char pr[]={0,0,1,2,3,3,4,4};
char n, *p, *r, s, *t;
cout << "\nRPN:\n\n";
t = stak - 1;
r = rpn;
for (p = inform;p++ ; *p){
if(7 < (s = *p)){
*r++ = s;
//a
}else if(s == 1){
*++t = s;
} else{
while(1){
if(t<stak){
*++t = s;
break;
} else if(pr[s] <= pr[n = *t--]){
*r++ = n;
} else if(3 < n+s){
*++t = n;
*++t = s;
}
break;
}
}
while(stak <= t){
*r++ = *t--;
}
*r = '\0';
for(r=rpn;*r;r++){
cout << ext[(int)*r];
}
cout << "\n\n";
}}
void
gencode()
{
void emit(char, char);
char a,j,lop,n1,n2,*p,*t;
cout << "Generated symbolic code \n\n";
j = len = lop = 0;
t = p = rpn;
while(3 <(a = *++p)){
if(7 <a){
*++t = a;
if((++lop == 2) && (0<len)){
n2=*t--;
n1=*t--;
*++t=j+60;
*++t=n1;
*++t=n2;
emit(2,(j++)+60);
}
}else {
if(lop == 1){
emit(a,*t--);
}else{
if( 1 < lop){
n2 = *t--;
n1 = *t--;
emit(1,n1);
emit(a,n2);
}else {
if((a==4) || (a==6)){
n1 = *t--;
emit(a,n1);
}else {
n1=*t--;
emit(2,j+60);
emit(1,n1);
emit(a,j+60);
if( 59 < n1){
--j;}}}}}}
lop = 0;
}
I'm guessing there is something wrong with the loops but I'm not sure what. I've been tweaking them for a while and all I managed to do was to get it to repeat Hp? instead of HW over and over.
for (p = inform;p++ ; *p) will continue until the next value of p is nullptr, and each round will dereference p and discard the result - you probably meant this the other way around.
Similar is this case: for(i=j=0, p = inform;p++,j++;j<len) which will continue running until j++ becomes 0 and discard the result of the comparision j<len each iteration.
In general, there is really only one problem here: Your code is really, really hard to read. Try to break it down into manageable chunks, and verify each one is doing what you want. That way, if there remains a single chunk not doing what you want, you can identify it, and ask for that one specifically instead of dumping a whole program.