I wanted to pass an array pointer to a function and show content of that array from that function.I did this in below way but when i run the code the whole program is crashing.Please help me or give me idea how to solve this problem...
int main()
{
int size,i;
cout<<"Please enter the size of the array";
cin>>size;
int *array_=new int [size];
cout<<"Please enter all elements of the array";
for(i=0;i<size;i++){
cin>>array_[i];
}
insertion(&array_,size);
return 0;
}
void insertion(int *array_[],int size){
int i;
for(i=0;i<size;i++){
cout<<*array_[i];
}
}
void insertion(int *array_,int size){
int i;
for(i=0;i<size;i++){
cout<<array_[i]<<" ";
}
}
Related
I am trying to pass a dynamic memory allocated array and its size to a function 'sum' but it is giving error of permissive what should I do?
#include<conio.h>
#include<iostream>
using namespace std;
int sum(int n[], int *m)
{
for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}
}
int main()
{
int *n,*m,a; //declaration is done here**strong text**
cout<<"enter the size of array = ";
m=new int;
cin>>*m;
n=new int[*m];
for(int i=0;i<*m;i++)
{
cout<<"\n enter the "<<i+1<<" array = ";
cin>>n[i];
cout<<"\n";
}
/* for(int z=0;z<*m;z++)
{
cout<<"\n the output is = "<<n[z]<<"\n";
}*/
int sum(n,&m);//here "m" is an pointer and I am trying to pass int in a function with an array
return 0;
}
Your code should, probably, look like the following (Linux Ubuntu + gcc):
#include <iostream>
using namespace std;
int sum(int n[], int m)
{
int s=0;
for(int z=0; z<m; z++)
{
cout<<"\n array["<<z<<"]= "<<n[z]<<"\n";
s+=n[z];
}
return s;
}
int main()
{
int *n,m;
cout<<"enter the size of array = ";
cin>>m;
n=new int[m];
for(int i=0; i<m; i++)
{
cout<<"\n enter array["<<i+1<<"] value = ";
cin>>n[i];
cout<<"\n";
}
int s = sum(n, m);
cout<<"s="<<s<<endl;
return 0;
}
There is no use allocating the size of the array m dynamically. It is an ordinary int variable and can be initialized as
cin>>m;
You may also write the sum prototype in the form
int sum(int * n, int m)
It is another way of passing a 1-dimensional array as a function parameter.
Speaking frankly, these questions are the very basics of the language.
You should, probably, read something like
Dynamic memory allocation/dynamic arrays
about dynamic memory allocation and dynamic arrays and
Simple cases of std::cin usage
about the simplest cases of std::cin usage in C++.
I keep getting 3 compiling errors that say "Function definition is not allowed". I am not sure on how to fix it. I keep getting it after the int main, and after the void functions. Please help!
This is the code:
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
int populateIntegerArray(int *arrayPtr, int arraySize);
void displayIntegerArray(int *arrayPtr, int arraySize);
int findMaximumInteger(int *arrayPtr, int arraySize);
int populateIntegerArray(int *arrayPtr,int arraySize)
{
for(int i=0;i<arraySize;i++)
{
cout<<"Enter value for array element "<<i<<":";
cin>>arrayPtr[i];//reading values
}
void displayIntegerArray(int *arrayPtr,int arraySize)
{
for(int i=0;i<arraySize;i++)
cout<<&arrayPtr[i]<<": arrayPtr["<<i<<"] = "<<setw(15)<<arrayPtr[i]<<endl;
}
void findMaximumInteger(int *arrayPtr,int arraySize)
{
int maximum = arrayPtr[0];
for(int i=0;i<arraySize;i++)
{
if(maximum<arrayPtr[i])max=arrayPtr[i];
}
cout<<"Maximum integer in array is: "<<max<<endl;
}
int main()
{
int n;
//reading array size
cout<<"Enter desired array size:";
cin>>n;
int *a = new int[n];
cout<<"arrayPtr = "<<a<<endl;
populateIntegerArray(a,n);
displayIntegerArray(a,n);
findMaximumInteger(a,n);
cout<<"DELETING array at arrayPtr = "<<a<<endl;
delete a;
return 0;
}
First, you are missing the last } after the definition of populateIntegerArray and before the displayIntegerArray:
int populateIntegerArray(int *arrayPtr,int arraySize)
{
for(int i=0;i<arraySize;i++)
{
cout<<"Enter value for array element "<<i<<":";
cin>>arrayPtr[i];//reading values
}
void displayIntegerArray(int *arrayPtr,int arraySize)
Then, you have the following prototype:
int findMaximumInteger(int *arrayPtr, int arraySize);
But you define the actual function as returning void (it should be int instead):
void findMaximumInteger(int *arrayPtr,int arraySize)
{
Then, in your findMaximumInteger function, you are not defining the max variable anywhere.
Functions populateIntegerArray and findMaximumInteger are declared as returning int, but actually do not return a value. You should either return an integer, or (if you intend to just output the result to console without returning a value from a function) change the functions' prototypes to returning void (not int).
I wrote this simple code for bubble sort but it gives some random garbage values as output. Can someone please tell me my mistake. I tried to print the output of A[i] and A[j] in the function bubbleSort and looks like it is working fine. But why is the printSortedArray not giving the correct output? Thanks!
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void printSortedArray(int A[],int size)
{
cout<<"the sorted array is"<<endl;
int i;
for(i=0;i<size;i++);
{
cout<<A[i]<<" ";
}
}
void bubbleSort(int A[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if(A[j]>A[j+1])
{
swap(A[j],A[j+1]);
}
}
}
}
int main()
{
int A[50]; int size,i;
cout<<"enter the size of the array: ";
cin>>size;
cout<<"Enter the "<<size<<" numbers to be sorted"<<endl;
for(i=0;i<size;i++)
{
cin>>A[i];
}
bubbleSort(A,size);
printSortedArray(A,size);
return 0;
}
for(i=0;i<size;i++);
The trailing semicolon does not belong there. This results in undefined behavior.
The end result is that this function printed one garbage value after the end of the array.
I resized my array with a function, however it seems like it does not store any array value I entered.
basically I put -1, the loop stops and then supposed to show the elements in array. but it does not show anything.
The output does not show anything after cout.
#include <iostream>
using namespace std;
void resize(int *&arr, int &size){
int tempsize=size;
size=size+10;
int *temp= new int [size];
for(int i=0; i<tempsize;i++){
temp[i]=arr[i];
}
delete [] arr;
arr=temp;
}
int main()
{
int size=0;
int capacity =10;
int *p=new int[capacity];
int check=0;
int input;
cout<<"Please enter the number in array and input -1 to end it.";
while(check!=-1)
{
cin>>input;
if(input==-1)
{check=-1;}
else{
if(size==capacity){
resize(p,capacity);
p[size]=input;
size++;
}
}
}
cout<<"Show me the numbers in array: ";
for(int i=0; i<size;i++){
cout<<p[i]<<" ";
}
cout<<endl;
delete [] p;
return 0;
}
Your if statement whether you need to resize the array is too encompassing. The {} should contain just the resize, then outside the {} is when you should store into the array.
Instead, size is 0, which != capacity, so you don't do anything.
So I'm totally new to pointers, I apologize for this, I'm supposed to pass an array of pointers and get the mode of that array. After the array passes as a set of pointers, I can't manipulate the array to find the mode, everything I try results in a syntax error.
EDIT: I changed list to an array of pointers and I get a runtime error.
int main()
{
int size=0;
int *list[size];
cout<<"Please enter the size of your array: ";
cin>>size;
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>*list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
int getMode (int* list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int *number=list[0];
int count1=0;
int count2=0;
int mode=0;
for(int j=1;j<arraySize;j++)
{
for(int i=1;i<arraySize;i++)
{
if(list[i]==number)
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= *list[j];
count2=count1;
}
count1=0;
}
return mode;
}
When you pass an array to a function, it automatically decays to a pointer, so you don't need to use &list. And in the function, you shouldn't declare it int *list[], it should just be int list[] or int *list.
Also, in the getMode() function, you need to count the matches of list[j]. You're just counting the repetitions of number, which is list[0].
#include <iostream>
#include <iomanip>
using namespace std;
int getMode (int list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int count1=0;
int count2=0;
int mode=0;
for(int j=0;j<arraySize;j++)
{
for(int i=0;i<arraySize;i++)
{
if(list[i]==list[j])
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= list[j];
count2=count1;
}
count1=0;
}
return mode;
}
int main()
{
int size;
int *list;
cout<<"Please enter the size of your array: ";
cin>>size;
list=new int[size];
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
DEMO