The Function won't initiate can someone help? When I run it in the debugger program skips over function and I don't know why?
#include <iostream>
using namespace std;
int size_array= 0;
int *data_array;
void sorting(int *[], int);
int main()
{
cout<<"enter in array size \n";
cin>>size_array;
int *data_array=new int(size_array);
for(int i=0;i<size_array;i++)
{
cout<<"enter number "<<i+1<<endl;
cin>>data_array[i];
}
**int sorting(int data_array, int size_array);**
for (int i=0; i<size_array;i++)
{
cout<<data_array[i]<<endl;
}
return 0;
}
The marked code is simply declaring the function, not calling it.
Also, your data_array is a pointer to a single int whose value is initialized as size_array. But you want an array of size_array number of ints instead.
Try this:
#include <iostream>
using namespace std;
void sorting(int[], int);
int main()
{
int size_array = 0;
cout << "enter in array size \n";
cin >> size_array;
int *data_array = new int[size_array];
for(int i = 0; i < size_array; i++)
{
cout << "enter number " << i+1 << endl;
cin >> data_array[i];
}
sorting(data_array, size_array);
for (int i = 0; i < size_array; i++)
{
cout << data_array[i] << endl;
}
delete[] data_array;
return 0;
}
void sorting(int data_array[], int size_array)
{
// sort data_array as needed...
}
Related
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 1 year ago.
When inputting a array in c++ the element in 0th position will become the length of the array.
have two functions to input and print the array when print function calls the output array has always the array length in 0th position.
#include<iostream>
using namespace std;
int getArray(int array[])
{
int len;
cout << "Enter the length of the array" << endl;
cin >> len;
cout << "Enter the elements in the array" << endl;
for (int i = 0; i < len; ++i)
{
cin >> array[i];
}
return len;
}
void printArray(int array[], int len)
{
for (int i = 0; i < len; i++)
{
cout << array[i];
}
}
int main(int argc, char const *argv[])
{
int array[] = {};
int len = getArray(array);
printArray(array, len);
return 0;
}
In C++, the size of an array must be a compile time constant. So you cannot write code like:
int n = 10;
int arr[n]; //incorrect
Correct way to write this would be:
const int n = 10;
int arr[n]; //correct
For the same reason the following code (last statement) is incorrect :
int k;
cin >> k;
int arr[k]; //incorrect because k must be a compile time constant
You can see that this results in a problem here.
You should use std::vector for this purpose.
Using std::vector, your implementation would look like:
#include<iostream>
#include <vector>
using namespace std;
//passing vec by reference
int getArray(std::vector<int> &vec)
{
int len;
cout << "Enter the length of the vector" << endl;
cin >> len;
cout << "Enter the elements in the vector" << endl;
int element;
for (int i = 0; i < len; ++i)
{
cin >> element;
vec.push_back(element);//use push_back to add element to vector
}
return len;
}
//passing vec by reference
void printArray(std::vector<int> &vec, int len)
{
for (int i = 0; i < len; i++)
{
cout << vec[i]<<std::endl;//use vec[i] to access ith element
}
}
int main()
{
std::vector<int> vec;
int len = getArray(vec);
printArray(vec, len);
return 0;
}
You can see the output here.
Note
You can simply take the input in the main() itself instead of calling another functions. Similarly for printing the vector. But i have given the code according to your implementation.
i made this program to make a function to check if integar array is palindrome or not.it is always giving output that it is not a palidrome on every input. can you plz help me out?
code:
#include<iostream>
using namespace std;
void palindrome(int[],int[],int);
int main()
{
int size=5;
int array1[size];
int array2[size];
palindrome(array1,array2,size);
}
void palindrome(int array1[],int array2[],int size)
{
size=5;
int l=0;
cout<<"enter your array=";
for(int i=0;i<size;i++)
{
cin>>array1[i];
}
for(int i=0;i<size;i++)
{
array2[i]=array1[size-i];
}
if(array1==array2)
{
cout<<"given array is palindrome.";
}
else{
cout<<"given array is not palindrome.";
}
}
You can't compare two arrays with ==, it will only compare if the array's address are equals, so different arrays never equals, we can switch it to std::equal
There is one memory issue with array2[i]=array1[size-i];, you will get a buffer overflow.
This is a slighly modified version of your code:
#include <iostream>
using namespace std;
void palindrome(int[], int[], int);
int main() {
int size = 5;
int array1[size];
int array2[size];
palindrome(array1, array2, size);
}
void palindrome(int array1[], int array2[], int size) {
size = 5;
int l = 0;
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> array1[i];
}
for (int i = 0; i < size; i++) {
array2[i] = array1[size - i - 1];
}
if (std::equal(array1, array1 + size, array2, array2 + size)) {
cout << "given array is palindrome.";
} else {
cout << "given array is not palindrome.";
}
}
It's recommended to use std::vector instead of the C style array:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
int size = 5;
palindrome(size);
}
void palindrome(int size) {
std::vector<int> vec(size);
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> vec[i];
}
std::vector<int> rev_rec{vec.rbegin(), vec.rend()};
if (vec == rev_rec) {
cout << "given array is palindrome." << std::endl;
} else {
cout << "given array is not palindrome." << std::endl;
}
}
And we can also avoid the copy of vector, with reverse iterator:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void palindrome(int);
int main() {
int size = 5;
palindrome(size);
}
void palindrome(int size) {
std::vector<int> vec(size);
cout << "enter your array=";
for (int i = 0; i < size; i++) {
cin >> vec[i];
}
if (std::equal(vec.begin(), vec.end(), vec.rbegin(), vec.rend())) {
cout << "given array is palindrome." << std::endl;
} else {
cout << "given array is not palindrome." << std::endl;
}
}
Hey guys please help me on this I have tried calling my bool function in my main func but it wont even show the first cout of program and the compiler terminates the program here is my code
#include <iostream>
using namespace std;
bool puzzle(int size, int array[], int start)
{
cout <<"how many blocks you want for the puzzle? \n";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "enter your numbers in order for the blocks:\n";
cin >> array[i];
if (array[0] > size) { return false; };
if (array[0] == size) { return true; };
}
}
int main()
{
puzzle;
return 0;
}
Your function has parameters so you need to call them to make it work. In this case (an example):
int size = 5;
int array[5];
int start = 0;
puzzle(size, array, start);
I want to create a function which generates an array(filled with random numbers) of the size I give as an input and the function returns the address of the first element of the generated array. I wrote the code as best as possible without any errors or warning. But at the runtime, the program crashes. I try to debug it but the debugger also froze and do nothing. I think the problem is in returning the pointer. Please help.
#include<iostream>
#include<cstdlib>
using namespace std;
int** the_gen(int num)
{
srand(1000);
int *ptr= new int(num);
int** const dptr=&ptr;
for(int i=0;i<num;i++)
{
*ptr= rand();
ptr++;
}
return dptr;
}
int main()
{
cout<<"Size of array:"<<endl;
int size_of_array;
cin>>size_of_array;
int **a;
a=the_gen(size_of_array);
for(int i=0;i<size_of_array;i++)
{
cout<<**a<<",";
a++;
}
}
you were using int** unnecessarily. only need to use that if you're creating an array of int pointers or a 2d array of int's:
the following code does what you're after i think:
#include<iostream>
#include<cstdlib>
using namespace std;
int* the_gen(int num)
{
srand(1000);
//edit
int *ptr = new int[num];
int* const dptr = ptr;
for (int i = 0; i < num; i++)
{
*ptr = rand();
ptr++;
}
return dptr;
}
int main()
{
cout << "Size of array:" << endl;
int size_of_array;
cin >> size_of_array;
int *a;
a = the_gen(size_of_array);
for (int i = 0; i < size_of_array; i++)
{
cout << *a << ",";
a++;
}
}
I think returning pointer is always bad idea, we should take memory pointers as a parameter as the follow
void the_gen(int num, int** arry)
{
srand(1000);
int *ptr = new int[num];
*arry = ptr;
for (int i = 0; i < num; i++)
{
ptr[i] = rand();
}
}
int main()
{
cout << "Size of array:" << endl;
int size_of_array;
cin >> size_of_array;
int *a;
the_gen(size_of_array, &a);
for (int i = 0; i < size_of_array; i++)
{
cout << a[i] << ",";
}
}
Hi I have this code written to read in a matrix with the dimension given. However I want to modify it to read in a matrix from a file from the command line using cin. I then want it to print row by row the position of a non zero element followed by that value. Can someone help me modify this. Thanks.
#include <fstream>
#include <iostream>
#include <stdlib.h>
//using std:#include <fstream>
//#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::ofstream;
void readArray(int, int, double **);
int nz=0;
main(int argc, char *argv[])
{
int rowCT = atoi(argv[1]);
int colCT = atoi(argv[2]);
// here you reserve the space for the array
double **A = new double*[rowCT];
for (int i = 0; i < rowCT; i++)
A[i] = new double[colCT];
readArray(rowCT, colCT, A);
}
//int count = new int[rowCT];
void readArray(int r, int c, double **arr)
{
//here r rows of c elements are read in
int count[r];
int total=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>> arr[i][j];
if(arr[i][j]>0)
{
nz++;
}
}
count[i]=nz;
nz = 0;
}
cout<< r << endl;
for(int i=0; i<r; i++)
{
cout<<count[i]<< " ";
for(int j=0; j<c; j++)
{
if(arr[i][j]>0)
{
cout<< j+1 << " " << arr[i][j] << " ";}
}
cout<< endl;
}
for(int i =0; i<r; i++)
{
total+= count[i];
}
cout << total<< endl;
}