In my main() function I have declared an array of type int with the numbers 1 to 10. I then have two other functions of type int* that take this array and its size as parameters, perform some operations, and each returns a pointer to the new array. Where I'm having issues is with a third function that prints the contents of the array.
#include <iostream>
using namespace std;
const int SIZE_OF_ARRAY = 10;
int main() {
int array[SIZE_OF_ARRAY] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr1 = 0;
ptr1 = function1(array, SIZE_OF_ARRAY);
print(array, SIZE_OF_ARRAY);
cout << endl;
int *ptr2 = 0;
ptr2 = function2(array, SIZE_OF_ARRAY);
print(array, SIZE_OF_ARRAY);
return 0;
}
void print(int array[], const int SIZE_OF_ARRAY)
{
for (int i = 0; i < (SIZE_OF_ARRAY * 2); i++)
{
cout << array[i] << " ";
}
}
int* function1(int array[], const int SIZE_OF_ARRAY)
{
int *ptr = new int[SIZE_OF_ARRAY];
// Do stuff.
return ptr;
}
int* function2(int array[], const int SIZE_OF_ARRAY)
{
int *ptr2 = new int[SIZE_OF_ARRAY * 2];
// Create new array double in size, and set contents of ptr2
// to the contents of array. Then initialize the rest to 0.
return ptr2;
}
As expected here, the result of calling the print() function twice is something like:
1 2 3 4 5 6 7 8 9 10 465738691 -989855001 1483324368 32767 -1944382035 32767 0 0 1 0
1 2 3 4 5 6 7 8 9 10 465738691 -989855001 1483324368 32767 -1944382035 32767 0 0 1 0
But I want the result to be like this instead:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0
How can I accomplish this? (Note that for this assignment I'm using C++98). Thanks in advance.
new int[SIZE_OF_ARRAY] allocates memory, but doesn't assign values to the array elements. What you are seeing is what was in that memory when it got allocated for the array. You can change your function2 to assign zeroes to array elements, if that's what you want.
First of all, you want to print different number of elements on the two calls to print, so you should not delegate deciding whether to multiply the size by two to the print, but rather do it on the calling side. Change the print function to only iterate up to SIZE_OF_ARRAY, and change the two places where you call it to:
print(ptr1, SIZE_OF_ARRAY);
and
print(ptr2, SIZE_OF_ARRAY * 2);
correspondingly.
Now, I assume that your second function does assign values to all 20 elements, but if it does not, the ones to which it did not assign values would continue containing garbage. To get around it, just initialize them at the beginning of the second function:
int *ptr2 = new int[SIZE_OF_ARRAY * 2];
for (size_t i = 0; i < SIZE_OF_ARRAY * 2; ++ i) ptr2[i] = 0;
With these two changes you should get the desired behavior.
Also, if you allocate something with new[], you need to delete it with delete[], otherwise you get a memory leak. Add these two lines at the end of main:
delete[] ptr1;
delete[] ptr2;
Note, that using delete instead of delete[] would be wrong in this case. If something is allocated as an array, it must be deleted as an array.
Related
This code is for a CS 235 class, but I'm new to C++ and I have no clue how they want me to do this. The grow function works by taking in an array and changing it to become twice as large. Coming from python, I guess this is the equivalent of mutating the array?
Anyway, I can't get it do the same for the insert function. I need it to check if the position that we're trying to insert an item at is outside the array bounds and make the array big enough that it can insert the value. Everything works....inside the function. The array does not maintain the changes outside the function. I've tried passing it in as a pointer, as a (reference to a pointer?) like *&array, and basically any combination I can think of.
void grow(int *&original_array, unsigned int & capacity){
int *temp = new int[capacity * 2];
for (int i=0; i<capacity*2; i++){
temp[i] = 0;
}
std::cout << "line 18: ";
print_array(temp, capacity*2);
for(int i=0; i<capacity; i++){
temp[i] = original_array[i];
}
std::cout << "line 23: ";
print_array(temp, capacity*2);
// delete[] original_array;
original_array = temp;
std::cout << "line 27: ";
print_array(original_array, capacity * 2);
capacity = capacity * 2;
}
bool insert (int array[], unsigned int & maxSize, unsigned int & nFilled, unsigned int pos, int value){
while (maxSize < pos){
grow(array, maxSize);
print_array(array, maxSize);
}
for(unsigned int i = nFilled - 1; i >= pos; i = i-1){
array[i+1] = array[i];
}
array[pos] = value;
print_array(array, maxSize);
return true;
}
Here's some sample input and what my program outputs right now:
int main() {
unsigned int my_size = 4;
int new_array[4] = {1,2,3,4};
unsigned int nFilled = 4;
insert(new_array, my_size, nFilled, 5, 15);
print_array(new_array, my_size);
return 0;
}
Output:
line 18: {0, 0, 0, 0, 0, 0, 0, 0}
line 23: {1, 2, 3, 4, 0, 0, 0, 0}
line 27: {1, 2, 3, 4, 0, 0, 0, 0}
{1, 2, 3, 4, 0, 0, 0, 0}
{1, 2, 3, 4, 0, 15, 0, 0}
{1, 2, 3, 4, -152629248, 32758, 0, 8}
the second to last line is inside the function and the last one is outside the function. I need these to be the same
Any help is appreciated - the TA's and professors are being unhelpful
Thanks
There's two problems with your code working as written, the first one is that you can't reassign the value of new_array in main as written. As written it is not dynamically allocated, and so it cannot be changed. You will need to change that to be dynamically allocated:
int* new_array = new int[4];
for(int x = 0; x < 4; ++x)
{
new_array[x] = x + 1;
}
That will address one issue, however the code still won't work, and that has to do with how variables are passed to insert and grow.
Your grow() function takes int*& a reference to a pointer to integer. This means that the underlying value passed in can be changed. However, your insert() function takes int[] an integer array that decays to to pointer to integer, and changes here will not be reflected at the call site of insert().
You can verify that by adding some more debugging statements:
bool insert (int array[],
unsigned int & maxSize,
unsigned int & nFilled,
unsigned int pos,
int value)
{
// unchanged above...
std::cout << "Inside insert: " << array << '\n';
return true;
}
int main()
{
// unchanged above...
std::cout << "After insert: " << new_array << "\n";
print_array(new_array, my_size);
return 0;
}
You will get output similar to this:
line 18: {0 0 0 0 0 0 0 0 }
line 23: {1 2 3 4 0 0 0 0 }
line 27: {1 2 3 4 0 0 0 0 }
{1 2 3 4 0 0 0 0 }
{1 2 3 4 0 15 0 0 }
Inside insert: 0x548ed0
After insert: 0x548eb0
{1 2 3 4 0 0 49 0 }
Note that the array "Inside insert" and "After insert" are different.
To fix this, you need to make sure you pass something to insert() that allows for changing the value, similar to what was done for grow().
That change is as simple as changing the signature of insert() as follows:
bool insert (int *&array,
unsigned int & maxSize,
unsigned int & nFilled,
unsigned int pos,
int value)
Note the first parameter to insert() was changed to match the type in grow() a reference to pointer to integer. Now when we pass the pointer new_array to insert() a reference to that pointer is passed (which allows the value to be changed), and that is also passed to grow() (if necessary), allowing this to propagate all the way to the call site.
Be aware though there are stylistic problems with this approach -- unclear ownership semantics, potential for memory leaks (present in the posted code) etc.
I will also caveat that "reinventing" this type of data structure instead of using std::vector is not a good practice, however it does have its uses in a teaching context.
This question already has answers here:
Initialization of all elements of an array to one default value in C++?
(12 answers)
Closed 3 months ago.
I'm trying to initialize a single value to whole array in c++.
For ex:- I want to initialize 1 in whole array by writing it by only once
I have tried to initializing 1 in whole array but it throws error , I'm expecting 1 in whole array.
ex- int array[5]={0};
output- 0 0 0 0 0
int array[5]={1};
output- 1 0 0 0 0
expecting- 1 1 1 1 1
if you want 1 value at very index you can do this by
int arr[5] = {1,1,1,1,1}; or
int arr[5];
arr[0] = 1;
arr[1] = 1;
arr[2] = 1;
arr[3] = 1;
arr[4] = 1;
otherwise if you write arr[5] = {1}; only first index will have value 1 rest will be assigned zero automatically like this {1,0,0,0,0}
or #include <iostream>
using namespace std;
int main()
{
int arr[5];
memset(arr, 1, sizeof(arr));
cout << arr;
return 0;
}
so type this code this memset function will fill all the index with value 1 by writing it only once.
hope this answer helps you.
Edited, try this code snippet working perfectly fine
#include <iostream>
#include <array>
int main () {
std::array<int,5> myarray;
myarray.fill(1);
std::cout << "myarray contains:";
for ( int& x : myarray) { std::cout << ' ' << x; }
std::cout << '\n';
return 0;
}
int* snap = nullptr;
int last = -1;
void func(int* md){
if(snap!=nullptr) {
last = *snap;
}
snap = md;
cout<<last<< " "<<*snap<<endl;
}
int main() {
for(int i =0;i<10;i++) {
int arg = i;
func(&arg);
}
return 0;
}`
Output
-1 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
Shouldn't the 'last' variable have the previous iteration's value of 'snap'. But the values are equal. What am I doing wrong here?
On the second iteration of your for loop, snap points to a dangling pointer. The int arg that it points to has fallen out of scope.
While it is a dangling pointer, you dereference it, causing undefined behavior.
I've tried to make an algorithm, which counts how many numbers are divided without a remainder. Code works, but every time I run it, I'm getting "trash" numbers in my output. I'm using dynamic arrays to solve a problem.
#include <iostream>
using namespace std;
int main()
{
int N = 30;
int *data = new int [N];
for (int i = 0; i < 100; i++)
{
for (int c = 1; c < N; c++)
{
if (i % c == 0)
{
data[c] += 1;
}
}
}
for (int k = 1; k < N; k++)
{
cout << data[k] << endl;
}
delete [] data;
}
I've expected to have, at least, what C++ Shell says: http://cpp.sh/6xtc
100
50
34
25
20
17
15
13
12
10
10
9
8
8
7
7
6
6
6
5
5
5
5
5
4
4
4
4
4
, but got in different IDE the same result:
100
11932994
34
25
20
17
15
13
12
620757039
37045
11951945
8
11927896
7
7
7290
158
6
5
5
570425383
37040
11951941
4
11927892
4
1835102827
859059803
You do
int *data = new int [N];
And allocate an N-sized array. Then you immediately start trying to increment the values in it:
data[c] += 1;
But what was in there to begin with?
If you want to guarantee that all the values will be initialized to 0, per this answer you can allocate your array with:
int *data = new int [N]();
// ^^^^
<Obligatory "you should just be using std::vector!" comment here.\>
(But actually though, vectors would make this way easier and avoid this issue entirely)
data[c] += 1;
You add something to an uninitialized value. data[c] was not necessarily 0 before that. new does not initialize a dynamic array to zero.
Quit all new/delete and use std::vector<>. And, do learn to use the debugger - really helpful in this case.
I want to create an array and raise 2 to every element in that array and store it as new array arr2. Here is my code
#include <iostream>
using namespace std;
int main(){
int arr1[7] = {1, 2, 3, 4, 5, 6, 7};
auto arr2 = 2 ** arr1;
cout << arr2 << endl;
}
But, it prints only the first element, it does not print the whole array. Why? So, basicaly, what I did here is I created arr1 with elements {1,2,3,4,5,6,7} and then I want arr2 to be
[2, 4, 8, 16, 32, 64, 128]
but for some reason it prints only the first element of array, it prints 2, but I want it to print all elements. Notice that 2 ** arr1 is the line where I am raising 2 to power (using exponentiation operator, i think it is how you call it if I'm not wrong) and then it should store array at array2.
What is wrong and why does it print only the first element instead all the elements?
** is not an exponentation operator. C++ is not Fortran.
You have multiplied 2 by the first element of arr: your statement is equivalent to int arr2 = 2 * arr1[0];. What you have entered is perfectly legal C++ (consisting of multiplication and pointer dereference), and the use of auto is adding to the obfuscation.
This statement
auto arr2 = 2 ** arr1;
is equivalent to
auto arr2 = 2 * *arr1;
Array designators in expressions are converted (with rare exceptions) to pointers to their first elements.
So the above statement can be rewritten like
auto arr2 = 2 * *( &arr1[0] );
As the first element of the array arr1 is equal to 1 then you have
auto arr2 = 2 * 1;
The expression 2 * 1 has the type int.
So as result the statement can be just rewritten like
int arr2 = 2;
There is no such operator as ** in C++. Instead you have to use standard function pow.
You can do the task either by writing an appropriate loop manually or using for example the standard algorithm std::transform.
Here is a demonstrative program
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iterator>
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6, 7 };
int arr2[sizeof( arr1 ) / sizeof( *arr1 )];
std::transform(std::begin(arr1), std::end(arr1),
std::begin(arr2),
[](int x) { return pow( 2, x ); });
for (int x : arr1) std::cout << x << ' ';
std::cout << std::endl;
for (int x : arr2) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
Its output is
1 2 3 4 5 6 7
2 4 8 16 32 64 128
The simplest way to do what you want can be something like this.
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int arr1[7] = {1,2,3,4,5,6,7}, arr2[7];
for(int i = 0; i<7; i++){
arr2[i] = pow(2, arr1[i]); //arr2 will be created.
cout<<arr2[i]<<" "; //Show arr2.
}
}