1. C++ Function Program to Sum all the digits from an Integer Array.
The below program will work as the points mention hereafter:
#include<iostream>
#include<conio.h>
using namespace std;
int arraysum(int[],int);
int main()
{
int n,j;
cout<<"Enter the length of the array"<<endl;
cin >>n;
//initializing the array length
int x[n];
//fill up the array using integer data
cout<<"Enter the elements :"<<endl;
for(j=0;j<n;j++)
{ cin>>x[j];
}
//call the function to sum
arraysum(x,n);
getch();
}
int arraysum(int a[],int n)
{
int sum=0;
for (int i=0;i<n;i++)
{ sum+=a[i];
}
cout<<"The sum is: " <<sum;
}
2. Write a C++ function to insert an elements in an Array in Recursive Method:
This program is working as below:
1. Taking the value of n as input for array length
2.Call the user defined function ArrayFunction with two parameters(int[],int).
3. The ArrayFunction is collecting the array elements from user.
4. After fill up the ArrayFunction return the array elements to main( ) using return arr[a-1].
5. main( ) prints the inserted elements to display.
#include <iostream>
#define ARRAY_SIZE 5
using namespace std;
int ArrayFunction(int[],int);
int main()
{
int n;
cout<<"Enter Array Length: "<<endl;
cin>>n;
int x[n];
cout<<"Enter array elements: "<<endl;
ArrayFunction(x,n);
cout<<"The inserted elements are: "<<endl;
for(int i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
}
int ArrayFunction(int arr[],int a)
{
if(a>0)
{
ArrayFunction(arr,a-1);
{
cin>>arr[a-1];
}
}
return arr[a-1];
}
<< Go to Index Page >>
The below program will work as the points mention hereafter:
Explanation: the main( ) function will call the user defined function named arraysum(). the prototype of arraysum() is interger and it will pass two parameter(int[],int). The first part is for array and the second part is for length of the given array. The arraysum() function receives all the data and doing sum using looping technique and print inside the function. Here function does not returns anything. It only receives value from main( ) program.
#include<iostream>
#include<conio.h>
using namespace std;
int arraysum(int[],int);
int main()
{
int n,j;
cout<<"Enter the length of the array"<<endl;
cin >>n;
//initializing the array length
int x[n];
//fill up the array using integer data
cout<<"Enter the elements :"<<endl;
for(j=0;j<n;j++)
{ cin>>x[j];
}
//call the function to sum
arraysum(x,n);
getch();
}
int arraysum(int a[],int n)
{
int sum=0;
for (int i=0;i<n;i++)
{ sum+=a[i];
}
cout<<"The sum is: " <<sum;
}
2. Write a C++ function to insert an elements in an Array in Recursive Method:
This program is working as below:
1. Taking the value of n as input for array length
2.Call the user defined function ArrayFunction with two parameters(int[],int).
3. The ArrayFunction is collecting the array elements from user.
4. After fill up the ArrayFunction return the array elements to main( ) using return arr[a-1].
5. main( ) prints the inserted elements to display.
#include <iostream>
#define ARRAY_SIZE 5
using namespace std;
int ArrayFunction(int[],int);
int main()
{
int n;
cout<<"Enter Array Length: "<<endl;
cin>>n;
int x[n];
cout<<"Enter array elements: "<<endl;
ArrayFunction(x,n);
cout<<"The inserted elements are: "<<endl;
for(int i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
}
int ArrayFunction(int arr[],int a)
{
if(a>0)
{
ArrayFunction(arr,a-1);
{
cin>>arr[a-1];
}
}
return arr[a-1];
}
<< Go to Index Page >>