Boundless Arrays? - c++

I'm having some trouble. I wrote a code to find prime numbers up to a number, but for some reason, it gives me the error that I didn't define the number of elements in the array that I will be using. Is it possible to have an array where the number of elements isn't limited? Thanks :)
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
int primer(int max);
int main()
{
system("pause");
return 0;
primer(1000);
}
int primer(int max){
int a[]=2;
for (int i=2;i<=max;i++){
prime=true;
for (int ii=1;ii<=#a;ii++) {
if i/a[ii]==math.floor(i/a[ii]) {
prime=false;
}
}
if prime==true {
a[#a+1]=i;
}
}
for (i=1;i<=#a;i++) {
print(a[i]);
}
}
}

Yes. Use a std::vector or std::deque.

What is this # symbol that you're using everywhere?
Your line int a[]=2 is incorrect.
You need to specify how big your array is going to be. For example, int a[100], or
int a[] = {Values here}.
That being said, you probably want a flexibly sized array like the vector class.

The compiler needs to know how much space to allocate, and therefore requires you to state how many elements you have.
You could try using the STL vector instead of an array. This allows you to add as many elements as you want, without declaring the number right at the beginning.
http://www.cplusplus.com/reference/stl/vector/vector/

You are always 'limited', if only by the amount of memory which can allocate for the array.
Having said that, you will probably be fine using a std::vector.

Related

How to dynamically allocate a 2D std::array in C++ or why I should not use it?

I want to malloc an array in my code, and its size should be defined at runtime.
I tried like this:
#include <iostream>
#include <array>
int main(){
int M=4,N=3,P=5;
M=N+P;
std::array<std::array<double,M>,N> arr;
}
But MSVC told me:
a variable with non-static storage duration cannot be used as a non-type argument
I don't find the answer to this in stackoverflow.(The existing question seem not to solve my problem...)
How to dynamically allocate a 2D std::array in C++?
I know I could use std::vector to solve this. But the vector memory size needs to be organized by myself and this would be used many times in my project. And I want to use C++ type code rather than C type...Maybe there is a method to turn a 2D array in C type to std::array, but I can't find it by Google...
So I ask this question...
I mean the M and N should be got dynamically(not changed,but I can only know it in runtime...),like:
#include <iostream>
int main(){
int a=3;
int b=4;
int rowCount=a+b;
int colCout=b-a;
int** a = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
{
a[i] = new int[colCount];
}
}
I know where is my mistake. I fell into a logical question... If I don't use push_back,the vector works well. If I use it, the array doesn't work, too.
I think the capcity of vector is bigger than its size, I want to avoid this. But another question: How to limit the capacity of std::vector to the number of element show I should use my allocator or std::vector::shrink_to_fit() to avoid it...(There is no guarantee in C++17 if you use reserve(n))
The dynamically allocated array container in C++ is std::vector. std::array is for specifically compile-time fixed-length arrays.
https://cppreference.com is your friend!
But the vector memory size needs to be organized by myself
Not quite sure what you mean with that, but you specify the size of your std::vector using the constructor.
std::vector<std::vector<int>> arr(N);
If you need some special allocator (not just new/malloc), then you can also specify a custom allocator.
Your whole program that you propose is not good C++. A C++ solution would look like:
#include <vector>
int main() {
int a = 3;
int b = 4;
unsigned int rowCount = a + b;
unsigned int colCount = b - a;
std::vector<std::vector<int>> matrix(rowCount);
for (auto& row : matrix) {
row.resize(colCount);
}
}
std::array, like an actual array in C++, requires a constant size. It's what gives it any advantage at all over std::vector.
For a technical explanation as to how that requirement is implemented, remember that template parameters are required to be compile-time constants (since it changes how the code is generated, again at compile-time).
Anyway, you want to use std::vector here. If you know the size you want, give it as a constructor parameter.

How do I solve the "array initializer must be an initializer list" error?

I've written a problem for an algorithm problem. I'm new to C++, and I'm getting the following error message when I try to run my code: "array initializer must be an initializer list". Here's the code itself:
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
int main(){
int n;
cin>>n;
int a[n][2];
int b[n];
for(int i=0;i<n;i++){
cin>>a[i][0];a[i][1]=i;
b[i]=a[i][0];
}
sort(a,a+n);
for(int i=1;i<n;i++)
{
if(a[i][0]<=a[i-1][0]){
a[i][0]=a[i-1][i]+1;
b[i]=a[i-1][i]+1;
}
}
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
}
I don't know why I'm getting this error message. I've Googled it and couldn't find anything useful. If someone could explain to me why I'm getting this message and how to solve it, I'd really appreciate it. Thanks in advance.
cin>>n;
int a[n][2];
The size of an array variable must be compile time constant. n is not compile time constant. The program is ill-formed.
What alternative do I have?
If you want a runtime size array, then you must create the array in dynamic storage. Simplest approach is to use std::vector. Elements of vector cannot be arrays, but you can use std::array as the element type instead. Example:
std::vector<std::array<int, 2>> a;
sort(a,a+n);
a is an array of arrays. Elements of tha array are themselves arrays. The arguments decay to be pointers to arrays. Pointer to array does not satisfy the requirement of being "value swappable" that is required by std::sort. Iterators to std::array are "value swappable", so this issue is solved by using that as the inner array as suggested above.
If you want to create an array, the size must be constant number or you should use vector instead. In addition, compiler says you have to assign your array to 0 with curly brackets like that:
int a[5][2]={{0}};
int b[5]={0};

Trying to read file which contains ints and store in int vector, but I keep getting "Segmentation fault (core dumped)" error

So I made this practice file for my project to try and read a file containing integer numbers, and store them in an int vector. My problem is whenever I run the program, it will give me "Segmentation fault (core dumped)" during the call to the readFile() function.
Do not mind the extra imports, I just copy and paste the same imports to all my practice files. Also the cout << "hi" << endl; is just to see when the program has the error.
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include <vector>
using namespace std;
vector<int> readFile(string fileName);
int main()
{
vector <int> intvec = readFile("ints.txt");
cout << "hi" << endl;
for (int i = 0; i < intvec.size(); i++)
{
cout << intvec[i] << endl;
}
return 0;
}
vector<int> readFile(string fileName)
{
ifstream inFile(fileName);
vector<int> inputVec;
int i = 0;
while (!inFile.eof())
{
inFile >> inputVec[i];
i++;
}
inFile.close();
return inputVec;
}
You would want to do this
while (inFile >> i)
{
inputVec.push_back(i);
}
In your code, you define inputVec without giving it an initial capacity, so you can assume that its size is 0. So when you write inputVec[i], you're actually trying to access an index out of the vector's boundaries. It's like accessing the 5th element in an array of size 4.
By using push_back, you can add elements to the vector and it'll adjust the size dynamically.
c++ std::vectors need to be resized before adding to them. Try the "push_back" function instead, that adds them on the end and resizes the vector to fit.
BTW unlike e.g. JavaScript you can't use "vector[i] == value" to automatically resize a c/c++ array/vector.
BTW, square brackets [] operator in c arrays and std::vector is completely different to that in JavaScript. JavaScript arrays are associative maps and using array[value] causes it to create a key "value" automatically. But this is extremely slow. The square brackets operators in c/c++ don't work like that. They are a much faster direct memory access system.
If you have an array called "myArray" and you asked for myArray[10], the computer just looks at whatever is inside the computers RAM 10 addresses beyond the start of myArray (multiplied by the size of the elements, so myArray[10] would be 40 bytes past the start of an array with 4-byte values such as int or float.
It's designed for pure speed, so there's no bounds checking added to this. it's entirely up to the c/c++ programmer to ensure that you don't read or write outside the bounds with square brackets operatork, but the payoff is much faster programs. You can add your own bounds checking to your own code, or you can just be careful not to read past the range you've allocated.
Using the std::copy algorithm and iterators, a vector can be filled with the following:
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> intvec;
std::ifstream ifs("ints.txt");
std::copy(std::istream_iterator<int>(ifs),
std::istream_iterator<int>(),
std::back_inserter(intvec));
}
Note: The std::back_inserter automatically calls push_back for each item in the stream.

C++ Storing Array in Separate File

I am working on a c++ program in which I have to pass an array to multiple sorting functions and compare the running times of the functions. For example, I have an array with 100 elements containing random numbers from 1 to 10. I have a bubble sort, merge sort, and quick sort function, and I have to pass the array to each function. However when I pass the array, the first sorting function changes the original array so that when it is passed to the next function it is already sorted. This is expected, but I was wondering how I would store this array in a separate file, perhaps a header file, to keep the original array unsorted for each function call.
Here is a layout of my code:
#include <iostream>
using namespace std;
//void bubblesort, mergesort, quicksort function prototypes
int main()
{
int a[100];
for (int i = 0; i < 100; i++)
a[i] = rand() % 10 + 1;
bubblesort(a);
mergesort(a);
quicksort(a);
return 0;
}
//void bubblesort, mergesort, quicksort function definitions
This code is obviously just a layout and the sorting functions aren't relevant for this question other than the fact that a call to a sorting function changes the original array. Thanks for your help.
You do not need files for this. Even though usual file systems on operating systems these days are rather nicely mapped to memory (in many cases this will be their cache) with delayed swapping to disk, interacting with the file system may make your code much more inefficient since you are writing to the disk.
Since you tagged this question with C++ I will answer this question the C++ way (or at least the C++ standard library way). What you want is to make a copy of the array when you pass it to the functions. At the moment you are passing in the raw address of the array so you are not making any copies (only possibly a copy of the pointer). This process is made easy if you use vectors. So the program could be
#include <iostream>
#include <vector>
using namespace std;
// The declarations would just need to change to this, I am assuming
// they print to stdout
void bubblesort(vector<int> vec);
void mergesort(vector<int> vec);
void quicksort(vector<int> vec);
int main()
{
vector<int> a;
for (int i = 0; i < 100; i++)
a.push_back(rand() % 10 + 1);
bubblesort(a);
mergesort(a);
quicksort(a);
return 0;
}
Here the vectors will be passed in by value so the vector that the function accesses is a copy of the original. The thing with vectors is that they are much more flexible and should usually be preferred over arrays in most high level programming scenarios.
If however your application demanded a use of low level arrays you could use memcpy to achieve this copying effect.
#include <iostream>
#include <vector>
using namespace std;
//void bubblesort, mergesort, quicksort function prototypes
int main()
{
int a[100];
for (int i = 0; i < 100; i++)
a[i] = rand() % 10 + 1);
int for_bubble_sort[100];
memcpy(for_bubble_sort, a, 100);
bubblesort(for_bubble_sort);
int for_merge_sort[100];
memcpy(for_merge_sort, a, 100);
mergesort(for_merge_sort);
int for_quick_sort[100];
memcpy(for_quick_sort, a, 100);
quicksort(for_quick_sort);
return 0;
}
Well, you should make an another array. Copy the contents of original array to this one using memcpy() or use a loop.
You can declare your array as const so it preserves it initial value and the functions don't change it.

Create an array/vector of objects

I'm trying to create an array or vector of objects. Most other stuff that I've found online has been about dynamically creating this, but I know the size that I need. I say vector or arrays because I believe either one will work for my situation (I'm comparing start and stop points) but any input onto which one will work better would be gladly welcomed as I'm pretty new to C++. My code below is my header followed by my main where I attempt to declare an array of objects, then I get this error: Variable length array of non-POD element type "Window." posWsize is a const int declared
#ifndef __Compare_Data_C____comp_fns__
#define __Compare_Data_C____comp_fns__
#include <iostream>
using namespace std;
class Window {
int start, stop, length;
double average;
string strandID;
public:
void setValues(int a, int b, string strand, int length, double avg);
};
#endif
int main()
{
int posWsize = getSize(fwdWindowInput, "+", windowHeader);
Window posWindow[posWsize];
return 0;
}
getSize gets the size of my array. Any help would be greatly appreciated! If I can't do it this specific way that's ok, I just want to know how to properly create this array (or vector if appropriate) of objects. Thanks
Use an std::vector:
std::vector<Window> posWindow(posWsize);
As a general rule in C++, unless you have a compelling reason not to, use a vector.