Printing 2d dynamic array c++ function - c++

I've been struggling with one issue, I have to build funtions that have to create and fill and print 2d dynamic array.
#include <string>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>
using namespace std;
void create_and_fill(int **T, int m, int n)
{
T = new int *[m];
for (int i = 0; i < m; i++)
{
T[i] = new int[n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
T[i][j] = -100 + rand() % 201;
}
}
}
void print(int **T, int m, int n )
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << T[i][j] << "\t";
}
cout << endl;
}
}
int main()
{
const int m = 5;
const int n = 6;
int **A = NULL;
create_and_fill(A, m, n);
print(A, m, n);
int **B = NULL;
create_and_fill(B, m, n);
return 0;
}
creating and filling works well and if I put some cout inside create_and_fill functions it also prints array. But if I try to print it using print function, there are some exceptions about forbidden actions.
I simply don't understand why some functions can do this and other don't and how to fix it. Thanks!

The problem is you are passing the pointer by value. You allocate and fill the array then you leak, because the changes are not stored in the original pointer you passed to the function. If you want to modify the pointer itself you need to pass it by reference:
void create_and_fill(int **&T, int m, int n)
You don’t delete the array anywhere in your code, so you have memory leak. Please note that every new should come with a delete.

Related

swap alternate in an array

You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
You don't need to print or return anything, just change in the input array itself.
#include <iostream>;
using namespace std;
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i]<<i;
}
void UpdateArr(int arr[], int n)
{
int i = 0, j = n - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
cout<<' printArr(arr[], n)';
}
int main()
{
int t;
cin>> t;
int n;
cin>> n;
int input[100];
for(int i=0; i<n; i++) {
cin >>input[i];
}
int arr[100] ;
n = sizeof(arr) / sizeof(arr[0]);
UpdateArr(arr, n);
return 0;
}
I'm not sure what are you exactly expecting the output to be (pls edit it and show the expected output) but I think this is what you need to do
#include <iostream>
#include <iomanip>
using namespace std;
void UpdateArray(int Arr[], size_t n) {
for (size_t i = 0; i < n / 2; i++) {
int Holder = Arr[i];
Arr[i] = Arr[~i + n];
Arr[~i + n] = Holder; } }
int main() {
int Arr[7] = { 1,2,3,4,5,6,7 };
UpdateArray(Arr, 7);
for (int i = 0; i < 7; i++) {
std::cout << Arr[i] << "\n"; }
return 0; }
size_t is like an int but it can't go into negative, but it can take bigger positive numbers, you can replace it with int, it shouldn't make a difference.
so we loop through half the array, replacing first items with last, the [~i + n] flips the value to the other side, so like index 4 in a array size of 20 will become 15

I am unable to get my code to get the stride of 7 to work properly C++

The question I am trying to solve is the following:
Write a function that traverses (and prints) the element of an array with stride =7. To do this the update part in the loop will be i= (i+7) % n, where n is the array size.
Would this function visit all elements of the array? Try different array sizes to check when it is impossible to traverse all elements.
The code that I wrote below doesn't print the correct values in the arry even if the value of i is correct.
Can anyone help, I would really appreciate it.
#include <fstream>
#include <stdlib.h>
using namespace std;
int* CreateArray(int n);
void StrideArray(int arr[], int n);
int main()
{
int* arr = new int[3];
arr = CreateArray(3);
cout << "The Elements In The Array Are: " << endl;
for (int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
cout << endl;
StrideArray(arr, 3);
cout << "The Elements In The Array Stride 7 Are: " << endl;
for (int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
delete[] arr;
return 0;
}
int* CreateArray(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = (rand() % 100);
}
return arr;
}
void StrideArray(int arr[], int n)
{
int i = 0;
for (int j = 0; j < n; j++)
{
i = (i + 7) % n;
arr[j] = arr[i];
}
}
The problem is in StrideArray you read back the modified values of arr.
void StrideArray(int arr[], int n)
{
int i = 0;
int puffer=new int[n];
for (int j = 0; j < n; j++)
{
i = (i + 7) % n;
puffer[j] = arr[i];
}
for (int j = 0; j < n; j++){
puffer[j] = arr[j];
}
debete[] puffer;
}
Is a good way to write the function.
Also it visits all element only if n isn't dividable by 7. So if n is not 7,14,21,...
Also to use cout you have to #include <iostream>
Your StrideArray function needs fixing; you are iterating over j but using i to index, which remains constant; and you are reassigning value at one index to another where as you are supposed to print it:
void StrideArray(int arr[], int n)
{
int i = 0;
for (int j = 0; j < n; j=j+7)
{
cout << arr[j] << endl;
}
}
I modified the rest of your code for demo:
#include <iostream>
#include <stdlib.h>
using namespace std;
int* CreateArray(int n);
void StrideArray(int arr[], int n);
int main()
{
int* arr = new int[3];
arr = CreateArray(21);
cout << "The Elements In The Array Are: " << endl;
for (int i = 0; i < 21; i++)
{
cout << arr[i] << " ";
}
cout << endl;
StrideArray(arr, 21);
delete[] arr;
return 0;
}
int* CreateArray(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = (rand() % 100);
}
return arr;
}

I'm trying to print my array but i'm not getting my desired result

I'm fairly new to programming and was trying to create a program which creates a one dimensional array with random numbers from a certain range and then prints it out. I managed to make a function to create the array but I'm having trouble actually printing out the array I made. I have a general idea of what the problem might be but no clue as to how to fix the code.
Here is the code in question:
#include <iostream>
using namespace std;
int *create(int n)
{
int *arr = new int [n];
for (int i = 0; i > n; i++)
{
arr[i] = rand() % 100;
}
}
int main ()
{
int n = 12;
int *arr = create(n);
cout << "this is the array: ";
for (int i = 0; i > n; i++)
{
cout << arr[i] << " ";
};
delete[] arr;
return 0;
}
There are two errors in your code:
You are not returning your array from create()
Your loop condition is incorrect.
Fixed code:
#include <iostream>
using namespace std;
int *create(int n)
{
int *arr = new int [n];
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 100;
}
return arr;
}
int main ()
{
int n = 12;
int *arr = create(n);
cout << "this is the array: ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
};
delete[] arr;
return 0;
}
I dont see how you code compiles.
You are not returning anything from function.
both your loop conditions should be <n
This way is works but your design is very poor, unless you are just learning handling pointers.
#include <iostream>
using namespace std;
int* create(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 100;
}
return arr;
}
int main()
{
int n = 12;
int* arr = create(n);
cout << "this is the array: ";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
};
delete[] arr;
return 0;
}
The only missing part in your code - you are not returning your arr variable from your create function.
int *create(int n)
{
int *arr = new int [n];
for (int i = 0; i > n; i++)
{
arr[i] = rand() % 100;
}
return arr;
}
This way it will work:)
You made a mistake in the loop. you used '>' instead of '<' line 22 should be.
for (int i = 0; i < n; i++)
The loop stopped before the first iterations because i=0 was smaller than n=12.

Storing a number of integers into array and printing them out

How do I make a function with for loop to store a given range of numbers into an array, then call that function in main program and print out the stored elements inside of the array?
int main ()
{
testing(array, 20);
}
int testing(int array[], int k)
{
for (int i = 0; k < 20; i++)
{
array[k] = i;
k++;
}
for (int j = 0; j < 20; j++)
{
cout << array[j] << endl;
}
}
I get the error of, "testing has to return value", which I understand that I should of have return var; for example. However, I don't know how to return an array of given elements to print out all the elements with a for loop.
Your function and loop constructions are not carefully designed. You can use pointers instead of having to return an array.
I think this is what you are trying to do:
#include <iostream>
void testing(int* a, int k)
{
for (int i = 0; i < k; i++)
a[i] = i;
}
int main()
{
int a[20];
testing(a, 20);
// you can see that a's elements have changed outside the main (in testing)
for (int j = 0; j < 20; j++)
std::cout << a[j] << "\n";
return 0;
}
You don’t seem to ever execute the first loop. Your stop condition for your first loop is k < 20, but k already is 20.
Then on the second loop, the array just prints out the garbage that was previously saved at those memory locations.
I think what you meant to do was:
for (int i= 0; i < k; i++) {
array[i] = i;
}
for (int j = 0; j < k; ++j) {
cout << array[j] << endl;
}
If you wanted to print the array in main, you need to change the function a bit, by passing in a pointer to the array rather than the array itself.
Also, your main function never initialised an array to pass into your function.
int main (void) {
int a[20];
testing (a, 20);
return 0;
}
Finally, set the return type of the testing function to void.
void testing (int array[], int k);
All in all, it should instead look like this:
#include <iostream>
using namespace std; // I don't recommend doing this
void testing (int * array, const int k);
int main (void) {
const int size = 20;
int a[size] = {0};
testing (a, size);
for (int i = 0; i < size; ++i) {
cout << a[i] << endl;
}
return 0;
}
void testing (int * array, const int k) {
for (int i = 0; i < k; ++i) {
array[i] = i;
}
}

i cant pass my array into my function

i am doing a shell sort program but there's some problem passing the array into my function, i have searched for some post about passing array into the function but i still don't understand.
#include <iostream>
#include <cmath>
using namespace std;
int shellsort(int arr[]){
int gap = floor(sizeof(arr)/2);
for(int gap = floor(sizeof(arr)/2); gap < 0; gap=gap/2){
for(int i = gap; i < sizeof(arr); i++){
if(arr[i] < arr[i-gap]){
int temp = arr[i];
arr[i] = arr[i-gap];
arr[i-gap] = temp;
if(gap == 1){
if(arr[i-1] < arr[i-2]){
int temp = arr[i-1];
arr[i-1] = arr[i-2];
arr[i-2] = temp;
}
}
}
}
}
return arr;
}
int main(){
int numcount;
cin>>numcount;
int numbers[numcount];
for(int i; i<numcount; i++){
cin>>numbers[i];
}
int numbers = shellsort(numbers);
cout<<numbers;
}
Beware of what you have promised to return and what you have returned.
int shellsort(int arr[]){
//....
return arr;//<--- ****not an int****
}
Now, either change to use a vector:
std::vector<int> shellsort(std::vector<int> arr){
int gap = floor(arr.size()/2);
for(int gap = floor(arr.size()/2); gap < 0; gap=gap/2){
//as above
}
return arr;
}
or send in the size
int * shellsort(int * arr, size_t size){
int gap = floor(size/2);
for(int gap = floor(size/2); gap < 0; gap=gap/2){
//as above
}
return arr;
}
The problem is that inside your function, the actual size of int arr[] is not known, and arr decays to a pointer, hence sizeof(arr) == sizeof(int*).
I suggest you rewrite your code to use std::vector:
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
std::vector<int> shellsort(std::vector<int> arr){
int gap = floor(arr.size()/2);
for(int gap = floor(arr.size()/2); gap < 0; gap=gap/2){
for(int i = gap; i < arr.size(); i++){
// ...as before...
}
}
return arr;
}
int main(){
int numcount;
cin>>numcount;
std::vector<int> numbers(numcount);
for(int i; i<numcount; i++){
cin>>numbers[i];
}
numbers = shellsort(numbers);
for (std::size_t i = 0u; i < numbers.size(); ++i)
cout<<numbers[i];
}