I want to create two functions, one that takes array values and lenngth
and the other displays array values and length

Here is my code, it gives me segmentation fault

#include <iostream>
#include <iomanip>
using namespace std;


void input_array(float a[], int &n)
	{
	   int j;
  	 for (j=0; j<n; j++)
		 {
               cin>>a[j];
		  }
	  }

void print_array(float a[], int n )
	{
	   int i;
             for (i=0; i<n; i++)
		 {
         	cout<<a[i]<<endl;
		 }
	  }

main()
{
int n;
float a[n];
//int n;
cout<<"Input the array size you want"<<endl;
cin>>n;
cout<<"enter the array elements"<<endl;
input_array(a, n);
print_array(a, n);
}

Recommended Answers

All 7 Replies

>int n;
>float a[n];
Not only is initializing an array size with an uninitialized variable hideously broken, array sizes in C++ have to be compile-time constants, so what you're trying to do won't work anyway. You need to either handle the memory manually with new[] and delete[], or use a dynamic container like a std::vector.

it seg faluts probably because it doesn't compile. You can't declare an array like you did in main(). You have to allocate it dynamically, like this:

int main()
{
    int n;
    int* array = 0;
 cout<<"Input the array size you want"<<endl;
cin>>n;
   // allocate array size
   array = new int[n];

}

THanks Ancient Dragon
This one works :D

#include <iostream>
#include <iomanip>
using namespace std;


void input_array(int a[], int &n)
	{
	   int j;
  	 for (j=0; j<n; j++)
		 {
               cin>>a[j];
		  }
	  }

void print_array(int a[], int n )
	{
	   int i;
             for (i=0; i<n; i++)
		 {
         	cout<<a[i]<<endl;
		 }
	  }

main()
{

int n;
    int* a = 0;
 cout<<"Input the array size you want"<<endl;
cin>>n;
   // allocate array size
   a = new int[n];
cout<<"enter the array elements"<<endl;
input_array(a, n);
print_array(a, n);
}
Member Avatar for iamthwee

don't forget to free it up at the end.

>void input_array(int a[], int &n)
Is there any reason for passing a reference? I'd say there is, but you're not doing it:

void input_array ( int a[], int &n )
{
  for ( int i = 0; i < n; i++ ) {
    if ( !( cin>>a[j] ) ) {
      n = i;
      break;
    }
  }
}

This guarantees that if less than n values are successfully read, you don't try to print more than that. I don't particularly like this kind of sneaky side effect, but it explains why you pass a reference.

>main()
While your compiler may allow this, C++ doesn't support implicit int. You have to explicitly state the return type of a function:

int main()

n is not changed so use const int & in lieu of int &

n is not changed so use const int & in lieu of int &

why? just don't pass it as a reference and it won't get changed. But Nurue's solution is the best (assuming the calling function doesn't mind having the integer changed on it).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.