Declaring sqrt(var) as a compile time constant in c++ - c++

I have a c++ program where I need to pass the square root of a number in a for loop.
#include<random>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <omp.h>
using namespace std;
int main()
{
vector<int>inputDataBits(49); // vector of randomly generated input data bits
#ifdef printDebug
std::cout << "the input data bits are" << endl;
std::cout << "-------------------------" << endl << endl;
int var =49;
const int r=(int)sqrt(var);
float input2d[r][r];
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
input2d[i][j] = inputDataBits[(j %r) + (i *r)];
std::cout << input2d[i][j] << "\t";
}
std::cout << endl << endl;
}
std::cout << endl << endl;
#endif
return 0;
}
I get an error 'expression must have a constant value'. Is there a way to do this in c++?

This is the purpose of the constexpr keyword (make the value known at compile time).
constexpr int var=49;
constexpr int r=(int)sqrt(var);
Unfortunately, in the documentation sqrt() is not declared as a constexpr function.
Only gcc seems to consider it as constexpr but it is not portable.

The size of an array needs to be known at compile-time.
Instead you can use a std::vector, which has a dynamic size.
std::vector<std::vector<float>> input2d(std::vector<float>(r), r);

Related

vector with reinterpret_cast

The following code inserts only one value to the vector col.
The code is extracted from DBMS code base (for importing files), specifically, it is from 1
The code uses void* to be able to read any field type (int, float, and so on).
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<void*> &col){
reinterpret_cast<std::vector<int>&>(col).push_back( 1);
reinterpret_cast<std::vector<int>&>(col).push_back( 2);
reinterpret_cast<std::vector<int>&>(col).push_back( 13);
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout <<reinterpret_cast<std::vector<int>&> (col)[i] <<endl;
return 0;
}
I am not sure how this code work?
Your code is exhibiting undefined behavior.
std::vector<void*> and std::vector<int> are two completely separate and unrelated types, you can't safely cast between them the way you are, especially since there is no guarantee that void* and int are the same byte size.
Cast the values you are pushing, don't cast the vector itself, eg:
#include <iostream>
#include <vector>
#include <cstdint>
using namespace std;
void add(std::vector<void*> &col) {
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(1)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(2)));
col.push_back(reinterpret_cast<void*>(static_cast<intptr_t>(13)));
}
int main() {
std::vector<void*> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << reinterpret_cast<intptr_t>(col[i]) << endl;
return 0;
}
Of course, you really should be using the proper container type to begin with:
#include <iostream>
#include <vector>
using namespace std;
void add(std::vector<int> &col) {
col.push_back(1);
col.push_back(2);
col.push_back(13);
}
int main() {
std::vector<int> col;
add(col);
cout << col.size() << endl;
for(int i=0;i<col.size();i++)
cout << col[i] << endl;
return 0;
}

the vector constructor doesn't change the end_file istream_iterator

i'm studying C++ for C programmers course (coursera) and in module 4 there is an example for how to use istream iterators to load data to STL vector ..but when i tried the code it only printed the first number from the file. i can't find the mistake in the code.
note :the instructor didn't run the code, he Taught is using PDF. so maybe there something missing in it.
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
using namespace std;
int main()
{
fstream data_file("data.txt");
istream_iterator<int> start_file(data_file), end_file;
vector<int> data(start_file, end_file);
int sum = 0;
for (auto i = start_file; i != end_file; i++)
{
sum += *i;
cout << *i << endl;
}
cout << data.size()<<endl;
cout << sum << endl;
cout << (sum* 1.0) / data.size() << endl;
return 0;
}

Novice C++, seeking help in array division

#include <iostream>
#include <valarray>
using namespace std;
// to get new card number
int main ()
{
int i;
int array[5]= {10,2,6,34,51};
valarray<int> v[5];
int v %= 13;
for (int i=0; i<5 ; i++) {
cout << v[i]%=13 << " ";
}
}
hello, my goal is to get the array to perform a modulus division by number 13.
I've search and try a few different way but I can't figure out a way to make it work.....
Thank you...
Some of the problems with your code:
valarray does not have the same notation as normal arrays: valarray<int> v[5]; declares 5 different valarray objects and puts them in a C-style array. The notation you are looking for is valarray<int> v(10);
Get rid of the int v %= 13; line: this redefines v (an array) as an integer.
Use v[i]=(array[i]%13); for the calculation, what you have doesn't make sense.
Then output cout << v[i] << " ";
Also, you aren't really using any of the features of valarray, so it may make more sense just to use one single array, like:
#include <iostream>
using namespace std;
// to get new card number
int main ()
{
int array[5]= {10,2,6,34,51};
for (int i=0; i<5 ; i++) {
array[i]%=13;
cout << array[i] << " ";
}
}
Edit: by the way, the cool thing about valarray here is that you can apply the same function to every value at once. Like this:
#include <iostream>
#include <valarray>
using namespace std;
int main() {
valarray<int> v(10);
for (int i=0;i<10;++i) {
v[i]=i*i; //Fill the array with 0,1,4,9,16,... as an example
}
v%=13; //This applies the modulo 13 on the whole array at once.
for (int i=0;i<10;++i) {
cout << v[i] << endl;
}
}
Seems you want something like...
int array[5]= {10,2,6,34,51};
int v[5];
for (int i = 0; i < 5; ++i)
v[i] = array[i] % 13;
for (int i = 0; i < 5; ++i)
std::cout << v[i] << " ";
std::cout << '\n';

c++ multiple smart pointers allocation cause crash

The maxPointers value may need to be different for your system, but allocating many unique_ptrs causes this application to crash and burn. Removing the definition of s and the cin operation gives some more room for pointer allocation.
Using MSVC 2015.
So, why does it crash and how to avoid it?
Thanks.
#include <iostream>
#include <vector>
#include <string>
#include <memory>
using namespace std;
int main(int argn, const char*argv[])
{
int maxPointers = 37900;
vector<unique_ptr<string>> pointerHolder;
for (int i = 0; i < maxPointers; i++)
{
pointerHolder.push_back(make_unique<string>("pointer " + i));
}
cout << "done creating "<< maxPointers << " pointers" << endl;
string s;
cin >> s;
for (int i = 0; i < maxPointers; i++)
{
pointerHolder.at(i).release();
}
pointerHolder.clear();
cout << "done releasing " << maxPointers << " pointers" << endl;
return EXIT_SUCCESS;
}
The crash you encounter is because you build strings from garbage that results from call "pointer " + i. If you intend to concatenate literal "pointer" with an integer, then you'd need to convert that integer to std::string with std::to_string first:
make_unique<string>("pointer " + to_string(i));
// ~~~~~~~~~~~^

My function does not modify its inputs

I'm trying to learn C++ and have this small beginner question:
why does the standardize function not modify its inputs?
To help with the answers, I have posted an executing code at Coliru
here
and the sources of my program below.
Referring to the code, the question would be: why isn't what's
printed after outside the same as what's printed after inside?
#include <cstdlib>
#include <ctime>
#include <algorithm> // std::copy
#include <iostream>
using namespace std;
void standardize(const int n,const float x[],float ave,float sct){
float acc=0.0f,sum=0.0f;
sum=std::accumulate(x,x+n,0.0f);
ave=sum/(float)n;
std::for_each(x,x+n,[&](const float d){acc+=(d-ave)*(d-ave);});
sct=std::sqrt(acc/(float)(n-1));
std::cout << "inside" << std::endl;
std::cout << ave << std::endl;
std::cout << sct << std::endl;
return;
}
int main(){
const int n=1024;
float a2[n];
float part0=0.0f,part1=0.0f;
std::srand(std::time(0));
for(int i=0;i<n;i++) a2[i]=std::rand()/(float)RAND_MAX;
standardize(n,a2,part0,part1);
std::cout << "outside" << std::endl;
std::cout << part0 << std::endl;
std::cout << part1 << std::endl;
}
You are passing ave and sct by values. Your standardize method modifies copies of those arguments, letting unchanged the original ones declared in main()
Consider passing them by reference:
void standardize(const int n,const float x[],float& ave,float& sct)