I use stackoverflow for the first time.
Error Question 1. I changed n or 4, 16 etc.. result = compile error
Error Question 2. It is not possible to call '2 Dimensional Array' with 'User Defined Function'.
Sorry for the incorrect grammar.
void outputarr(int n, int (*arr)[**n**]){ //n error
...
}
void inputarr(int n, int (*arr)[**n**]){ //n error
...
}
int main(void){
int input;
int *input2;
cin >> input;
cout << "Length of Array : " << input << '\n';
input2 = &input;
int **arr = new int*[input];
for(int i=0; i<input; i++)
arr[i]= new int[input];
**inputarr(input, arr); // pass to input&output error
outputarr(input, arr);** // pass to input&output error
for(int i=0; i<input; i++)
delete[] arr[i];
delete[] arr;
return 0;
}
Related
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));
#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<>.
My goal in this program is to take two user entered arrays of a given size and merge them into one.
I'm able to successfully enter both arrays' size and their elements, but it fails to output the merged array- giving me this error:
"Error in `./a.out': free(): invalid pointer: 0x0..."
I've tried to debug but to no avail, I can't seem to figure out if I have incorrect syntax or I'm making an incorrect call.
Any help appreciated
#include<iostream>
using namespace std;
int* mergeArrays(int[], int[], int, int);
int arr1[0], arr2[0];
int main()
{
int size1, size2, i;
cout<<"Enter the first array's size : ";
cin>>size1;
int *arr1 = new int[size1];
cout<<"Enter the first array's elements : ";
for(i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter the second array's size : ";
cin>>size2;
cout<<"Enter the second array's elements : ";
for(i=0; i<size2; i++)
{
cin>>arr2[i];
}
delete[] arr1;
delete[] arr2;
cout << mergeArrays;
}
int* mergeArrays(int arr1[], int arr2[], int size1, int size2){
int i, k, size;
int size3 = size1 + size2;
int *mergeArr = new int[size3];
for(i=0; i<size1; i++)
{
mergeArr[i]=arr1[i];
}
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++, k++)
{
mergeArr[i]=arr2[i];
}
cout<<"The merged array is: \n";
for(i=0; i<size3; i++)
{
cout<<mergeArr[i]<<" ";
}
return mergeArr;
}
Your program exhibits undefined behavior, by way of a buffer overrun. arr2 is an array of zero size, arr2[i] is accessing out of bounds for any value of i.
Also, you call delete[] arr2 but arr2 was not allocated with new
What I am trying to do is print out the elements that is dynamically allocated.
Here is my issue, I'm new in learning, and not understanding why this below cannot work? If you could please give a brief description why this is producing the error, I'm just trying to learn from my mistakes. I know I could just dynamically allocate the user input in the main function, but I wanted to see by trial and error if I could I just create an input function where I call it in my print function to produce the user elements after it is dynamically allocated.
Any suggestion on what I could do to make this work would be awesome, thanks.
void print(int* input, int size)
{
uinput(input,size);
for(int i=0; i<size;i++)
{
std::cout << " " << input[i];// Error -> Thread 1:EXC_BAD_ACCESS (code=1,address=0x0)
}
}
The entire code without the print function above
#include <iostream>
void print(int*, int);
void uinput(int*, int);
int* copy(const int*, int);
int main()
{
int size;
int* input;
std::cout << "Enter the size of the array";
std::cin >> size;
std::cout << "Original array:" << std::endl;
print(input,size);
int* expander = copy(input,size);
std::cout << "New array:" << std::endl;
print(expander,size);
delete [] input;
delete [] expander;
input= nullptr;
expander = nullptr;
return 0;
}
void uinput(int* input, int size)
{
int* uInput = new int[size];
for(int k=0; k <size;k++)
{
std::cin >> uInput[k];
}
}
int* copy(const int* input, int size)
{
int* newArray = new int[size*2];
int j =0;
for(int i = 0; i <size*2;i++)
{
if(j >i)
{
newArray[j]=newArray[i];
j++;
}
else
newArray[j]=0;
}
return newArray;
}
Update: I tried to modified where I just let it point to the size of uInput function, but that didn't work.
void uinput(int size)
{
int* uInput = new int[size];
for(int k=0; k <size;k++)
{
std::cin >> uInput[k];
}
}
Your uinput function allocates memory, but does not make use of the input parameter that you send it. As a result, trying to iterate over items supposedly pointed to by that pointer, which has not been initialized to point at anything, is undefined behavior, causing your program to crash, in this case.
Hi i am trying to create a bubble sort using dynamic arrays, the code seems to work but throws an run time error : HEAP Corruption Detected(since i am deleting dynamic arrays in the ...i donot understand why i am getting such an error). Also, the last two elements in the given array get sorted but i get the address displayed for the last element . As i am trying to learn dynamic arrays on my own.Kindly help me understand the error. Thanks in advance !!!
Array = {125,12,2,36,19}
#include "stdafx.h"
#include <iostream>
using namespace std;
void bubblesort(int* a, int length); // for bubble sort//
int _tmain(int argc, _TCHAR* argv[])
{
int size;
cout << " enter the size of array: " << endl;
cin >> size;
int* a = new int[size];
cout << "enter the elements in an array: " << endl;
for (int i = 0; i < size; i++)
cin >> *(a+i);
bubblesort(a, size);
delete[] a;
a = NULL;
return 0;
}
void bubblesort(int* a, int length)
{
int temp = 0;
for (int i = 0; i < length; i++)
{
if (a[i] > a[i+1])
{
temp = a[i+1];
a[i+1] = a[i];
a[i]= temp;
}
}
for (int i = 0; i < length; i++)
{
cout << " The elements are : " << endl;
cout << a[i] << endl;
}
}
As (it was) mentioned in the comments, you're reading outside the array.
a[i + 1] = a[i]; //When i == length - 1, this is UB
In the last iteration of the for loop, you'll overwrite whatever is after the end of the array. An array a[length] is only valid from 0 through length - 1.
Also, your bubble sort only runs once, while it is supposed to constantly run until all items are sorted.
On a subjective note, *(a+i) is identical to, but less readable than, a[i].