Is it possible to dynamically allocate a vector? - c++

I was told on here to use vectors instead of arrays for this particular solution ( My original solution using to dynamically allocate arrays) my problem is I don't really understand vectors, my book only covers a small section. So, maybe I am doing something wrong? I'm just trying to learn a different way in solving it with vectors. So, when I ask is it possible to dynamically allocate a vector. What I mean is it legal to do this std::vector<int*> vect= nullptr;, if not why? Also it would be really helpful to see an example how to modify my original solution with vectors. I'm a beginner just trying to learn from my mistakes.
#include <iostream>
#include <vector>
void sortAscendingOrder(int*, int );
void LowestTestScore(int*, int);
void calculatesAverage(int*,int);
int main()
{
std::vector<int*> vect= nullptr;
int input;
std::cout << "Enter the number of testscores you want to enter." <<std::endl;
std::cin >> input;
vect = new int[input];
for(int count =0; count < input; count++)
{
std::cout << "Enter the test score" << (count +1) <<":" <<std::endl;
std::cin >> vect[count] ;
while( vect[count] < 0)
{
std::cout <<"You enter a negative number. Please enter a postive number." <<std::endl;
std::cin >> vect[count];
}
}
sortAscendingOrder(vect,input);
for(int count =0; count < input;count++)
{
std::cout << "\n" <<vect[count];
std::cout << std::endl;
}
LowestTestScore(vect,input);
calculatesAverage(vect,input);
return 0;
}
void sortAscendingOrder(int* input,int size)
{
int startScan,minIndex,minValue;
for(startScan =0; startScan < (size-1);startScan++)
{
minIndex = startScan;
minValue = input[startScan];
for(int index = startScan+1;index<size;index++)
{
if(input[index] < minValue)
{
minValue = input[index];
minIndex = index;
}
}
input[minIndex]=input[startScan];
input[startScan]=minValue;
}
}
void LowestTestScore(int* input, int size)
{
int count =0;
int* lowest = nullptr;
lowest = input;
for(count =1; count <size;count++)
{
if(input[count] < lowest[0])
{
lowest[0] = input[count];
}
}
std::cout << "Lowest score" << *lowest;
}
void calculatesAverage(int* input, int size)
{
int total = 0;
int average =0;
for(int count = 0; count < size; count++)
{
total += input[count];
}
average =(total/size-1);
std::cout << "Your average is" << average;
}

Dynamic allocation of an array is required when you want to increase the size of an array at run-time. Just like arrays, vectors used contiguous storage locations for their elements. But, unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.
Vectors do not reallocate each time an element is added to the container. It pre-allocates some extra storage to accommodate future growth. Libraries can implement different strategies for growth to balance between memory usage and reallocation, but in any case, reallocation should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back)
To answer your question:
Yes, it is possible to dynamically allocate a vector and no, it requires no extra effort as std:vector already does that. To dynamically allocate vectors is therefore, a redundant effort.
You can write:
#include<vector>
typedef std::vector< std::vector<double> >vec_array;
vec_array name(size_x, std::vector<double>(size_y));
This means: size instances of a std::vector<double>, each containing size_y doubles (initialized to 0.0).
Or, you can simply use vector to increase the container size (knowing it handles storage requirements by itself) like:
#include <vector>
vector<int> vec_array;
int i;
for(i=0;i<10;i++)
vec_array.push_back(i);

Related

Run-Time Check Failure #2 - Stack around the variable 'sortObject' was corrupted. how to fix?

I was trying to store numbers in an array. The first half of the array are numbers that are ascending 1,2,3,4,5 etc and the second half of the array are random numbers. When i run the program it produces the output I wanted but gives me the error please help
#include <iostream>
#include <cstdlib>
using namespace std;
class sorting {
private:
int size, elements;
int arr[NULL];
public:
void sort(){
cout << "Enter number of desired elements" << ">"; cin >> elements;
arr[elements];
half();
}
void half() {
for (int i = 0; i < elements/2; i++) {
arr[i] = i + 1;
}
for (int i = elements / 2; i < elements; i++) {
arr[i] = rand();
}
cout << "This is the elements of the array";
for (int i = 0; i < elements; i++) {
cout << arr[i] << " ";
}
}
};
int main()
{
sorting sortObject;
sortObject.sort();
return 0;
}
As i could see you want the array size to change during run time depending on the input, we need to dynamically allocate a array.so take a integer pointer as a field instead of static array.
then inside the sort function after reading the input, dynamically allocate the memory to pointer.(actually its better if we do it in a constructor).
int *arr;
arr=(int *)malloc(elements*sizeof(int));

Some test cases aren't passing

#include <iostream>
#include <vector>
int i=0; //points at the current stack that we are working with
int box=0; //no. of boxes held by the crane
int64_t H; //max. height of the stacks given in the que.
int main()
{
int n, value; //storing no. of stacks and creating an additional variable value to store operations
std::cin>> n >> H;
int64_t arr[n]; //storing the no. of boxes each stack has in an array
std::vector<int> arr2; //storing the operations we have to perform in a vector
for(int j=0; j<n; j++){std::cin>> arr[j];} //getting arr
while(std::cin>>value) //getting arr2
{
arr2.push_back(value);
}
for(int xy=0; xy<n; xy++){if(arr[xy]>H){return 0;}} //ensuring that all stacks have no.of boxes less than max. height
if(arr2.size()<1 || arr2.size()>10e5 || n<1 || n>10e5 || H<1 || H>10e8){return 0;} //constraints given in the que.
int k=0; //creating a variable to keep count of how many programs we have already executed
while(k<arr2.size()){
if(arr2[k] == 1){MoveLeft();}
else if(arr2[k]==2){MoveRight(n);}
else if(arr2[k]==3){PickBox(arr, i);}
else if(arr2[k]==4){Dropbox(arr, i);}
else if(arr2[k]==0){k=arr2.size();}
k++;
}
for(int j=0; j<n; j++){std::cout<< arr[j] << " ";} //printing the arr after executing the code
return 0;
}
This is a question from a past year ZCO. And the above code is what I wrote to solve the prob.
The four functions Moveleft, MoveRight, Pickbox, Dropbox have been defined in the same file but aren't shown here because I think there's no issue with them.
When I submit the code, all test cases passed except 2. I don't know what is the problem with my code. Pls help me.
I have tried my best to make the code readable. Sorry if the code looks messy.
With the method you're trying to define an array with a user-input length is unfortunately invalid in C++.
But fortunately, there are basically two methods use to allocate arrays dynamically.
Method 1: Using Vectors
Vector is an important part of C++. It has a lot of features (e.g. its size don't need to be defined static unlike a normal array does, can redefine array size, etc.) An example's given:
#include <iostream>
#include <vector>
int main(void) {
std::vector<int> vArray; // vector<> declaration
int size = 0;
int getInput = 0;
std::cout << "Enter an array size: ";
std::cin >> size;
for (int i = 0; i < size; i++) {
std::cout << "Enter a value: ";
std::cin >> getInput;
vArray.push_back(getInput); // inserts one+ container and data in it
}
for (int i = 0; i < vArray.size(); i++) {
// retrieving contained data...
std::cout << vArray[i] << std::endl;
}
return 0;
}
Method 2: Using 'new' Keyword with Pointed Variable
The simple use of new will help you to achieve your requirement. It's less recommended since already there's concept of vectors which actually works efficiently than arrays. Let's take a look into a simple program:
#include <iostream>
int main(void) {
int *pArray;
int size;
std::cout << "Enter an array size: ";
std::cin >> size;
pArray = new int[size]; // initializing array with dynamic size
for (int i = 0; i < size; i++) {
std::cout << "Enter value: ";
std::cin >> pArray[i];
}
for (int i = 0; i < size; i++) {
std::cout << pArray[i] << std::endl;
}
delete[] pArray;
return 0;
}
Both are nice options to work with, but it's recommended by most using vector<>.

How can I print the numbers in main function?

I am new to c++ language. I am trying to solve a problem using function. I have to print the pentagon numbers untill the integer input, but when function returns the values, it only prints one value. I would love some help with it.
#include<iostream>
using namespace std;
int pent(int num){
int p;
for(int i=1;i<=num;i++){
p=(i*(3*i-1)/2);
}
return p;
}
int main(){
int num;
cin>>num;
int sender=pent(num);
cout<<sender<<endl;
return 0;
}
Your function returns int, that is a single integer. To return more, you can use std::vector. As you probably are not familiar with it, I will give you some pointers...
The most simple constructor creates a vector with no entries:
std::vector<int> x;
You can reserve space for elements via reserve:
x.reserve(num);
The vector still has no elements, but it already allocated enough space to hold num elements. This is important, because when we will add elements the vector will grow and that potentially requires to copy all elements to a different place in memory. We can avoid such frequent reallocations by reserving enough space upfront.
To add elements to the vector you can use push_back:
x.push_back(42);
Eventually to print all elements of the vector we can use a range-based for loop:
for (auto element : x) std::cout << element << " ";
So you can rewrite your code like this:
#include <iostream>
#include <vector>
std::vector<int> pent(int num){
std::vector<int> result;
result.reserve(num);
for(int i=1;i<=num;i++){
result.push_back(i*(3*i-1)/2);
}
return result;
}
int main(){
int num;
std::cin >> num;
auto sender = pent(num);
for (auto number : sender) std::cout << number << " ";
}
In your program, from your pent() function you are only returning last calculated value. In you ever time, you are overwriting you variable p.
So there is a way which #asmmo is suggesting, to print in pent() function.
Or you can pass a vector to your pent() function and store values in that and print it in main function.
For your ref:
void pent(int num, vector<int> &arr) {
int p;
for (int i = 1; i <= num; i++) {
arr[i-1] = (i*(3 * i - 1) / 2);
}
}
int main() {
int num;
cin >> num;
vector<int> arr(num);
pent(num, arr);
for (int i = 0; i < num; i++) {
cout << arr[i] << endl;
}
return 0;
}

dynamically allocating vector containing vector to be allocated dynamically

I'm learning C++ and when practicing on Hackerrank, I encountered vectors containing vectors for the first time. This is the problem to solve.
From the program provided below, I want to know:
Is it the correct way to declare the required vector as I did?
Does the statement "a.resize(i)" at line 27 work as I intend it to?
#include<iostream>
#include<vector>
#include<array>
bool inrange(int min, int max, int x)
{
//check if x is in range
return (x >= min && x <= max);
}
int main(void)
{
int q{}, n{}; //for no. of queries & no. of elements in array
std::cin >> q >> n;
if (inrange(1, 100000, q)) //ensure q to be in specified range
{
if (inrange(1, 100000, n))
{
//for declaring vector of vectors
using innertype = std::vector<int>;
std::vector <innertype> a;
//array to store no. of elements in each ith array
std::vector <int> ki;
for (int i{ 0 }; i < n; ++i)
{
//extend vector by one element
>line 27 a.resize(i);
//get the array for ith position
int j{ 0 }; char buffer;
do
{
//extend vector at ith index by one element
a[i].resize(j);
std::cin >> a[i][j];
std::cin >> buffer;
++j;
} while (buffer != '\n');
ki.resize(j);
ki[i] = j;
}
//take indexes for query1 and query2 to print requested elements
int i{}, j{};
std::cin >> i >> j;
std::array q1request{ i,j };
std::cin >> i >> j;
std::array q2request{ i,j };
//print elements "a[i][j]"
std::cout << a[q1request[i]][q1request[j]] << '\n';
std::cout << a[q1request[i]][q2request[j]];
}
}
return 0;
}
The program terminates after taking two inputs.
Debug Assertion Failed!
Expression: vector subscript out of range
There is nothing really wrong with your usage of vector apart from indexing (maybe a bit strange, but not wrong). The usage of the resize method of the vector is missused. For the pre-allocation of the vector, two methods are used in general. resize() and reserve().
One can be used in case you need indexing the vector.
std::vector<int> test(10, 0); // creating vector of 10 items set to 0
test.resize(100); // resizing to allocate space for 100 elements
// test.size() == 100
for(size_t i = 0; i < test.size(); ++i)
{
test[i] = i;
std::cout << "Filling " << i << "th element." << std::endl;
}
The other is used usually when you dont need the index.
std::vector<int> test;
test.reserve(100); // reserve space for at least 100 elements
// test.size() == 0
for(size_t i = 0; i < 100; ++i)
{
test.push_back(i); // will not allocate new memory
}
The issue you have is that you resize the vector to hold 0 elements in 1st iteration and then you access element at index 0. The same would go for any other subsequent index.

Incorrect Variable output with Vector Class C++

My output for the call to the temporary array size wont correctly output. It resizes as according, but I can't get the MAX to display the new value of the new array. My error is within the Resize function within the class.
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <ctime>
using namespace std;
class VectorClass {
private:
int * Vector;//This will be our resizeable array
int Size; //Keep track of vector current size
int MAX=10;
int growth = 5;
int num;
int Resize(int growth, int MAX);
public:
VectorClass(int growth, int Size);
~VectorClass();
int AddItem(int num);
void RemoveItem();
void Print(void);
};
VectorClass::VectorClass(int growth, int Size)
{
Size = 10;
growth = 5;
Vector = new int[Size];
}
VectorClass::~VectorClass()
{
cout << "Destructor was called." << endl;
}
//Will insert num into the vector at the current open position
int VectorClass::AddItem(int num)
{
Vector[Size] = num;
Size++; //Indicate that there isnt as much free space
if (Size == MAX)
{
Resize(Size, MAX);
}
Print();
return num;
}
//Get rid of the most recently added item
void VectorClass::RemoveItem()
{
Size--; //Tricks the vector into one fewer elements in it it currently does
Print();
}
int VectorClass::Resize(int growth, int MAX)
{
cout << "Array is full! Resizing the Array!" << endl;
//Step 1: make a copy
int * temp = new int[MAX]; //Make a new array, same size as exiting array
//loop that copies the original into the copy
for (int i = 0; i<MAX; i++)
{
temp[i] = Vector[i];
}
//Step 2: Delete the original
delete[] Vector; //Deletes all elements in the array Vector from the Heap
//Step 3: Make a bigger vector
Vector = new int[MAX + growth];
//Step 4: Reverse the copy and record the size change
for (int i = 0; i<MAX; i++)
{
Vector[i] = temp[i];
}
MAX = MAX + growth;
//Step 5: Delete the copy
delete[] temp;
cout << "Resize was called.\n" << endl;
return MAX;
}
void VectorClass::Print()
{
cout << "*******************************************************" << endl;
for (int i = 0; i< Size; i++)
{
cout << Vector[i] << endl;
}
cout << "Size = " << Size << "\tMAX = " << MAX << "\t Growth = " << growth << endl << endl;
cout << "*******************************************************" << endl;
}
int main(void)
{
VectorClass V(5,10);
for (int i = 0; i <= 4; i++)
{
int x = rand();
V.AddItem(x);
}
//Print the Vector #1
V.Print();
//Delete 2 Items
V.RemoveItem();
V.RemoveItem();
//Add 9 random Numbers
for (int i = 0; i <= 8; i++)
{
int x = rand();
V.AddItem(x);
}
//Print the Vector
V.Print();
system("pause");
return 0;
}
Several things are wrong with you code. The first one, probably not the one you care about most, is that you never free the memory. You should do it in your destructor, or even better use a std::unique_ptr to handle your memory.
Now, i believe you are yourself confused about your own variables. I see that you possess a variable member named num that you never use. Even worse, you have a parameter in AddItem with the same name. Are you sure it does what you want? The same is true for growth. I would advise you to name your member variable differently, so that you know what they are quickly. I prefixe them with "m_" for example, but you can do as you wish.
You do not need to declare your function parameters inside your class. Only in the function prototype.
Then, in your AddItem function, you use your variable Size to determine where to add the new element, but you initialize your array with it too, which means that not only you do not add your elements at the beginning of your array, you try to write them in memory you do not own!
I could continue for a long time. I am sorry but it only appears to me that you do not know C++ at all. You should go learn the basics again, and maybe start with an easier project to begin your C++ learning.
Good luck :-)