C++ User defined Function in Array Programming.

1. C++ Function Program to Sum all the digits from an Integer Array.
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 >>

C Program for Sum of all the Multiples of 3 or 5 below 1000 : From ProjectEuler.net

/* If we list all the natural numbers below 10 that
are multiples of 3 or 5, we get 3, 5, 6 and 9.
The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
*/

#include<stdio.h>
#include<conio.h>
int main()
{
    long sum=0,range=1;
    clrscr();
    do{
    if(range%3==0 || range%5==0)
        sum+=range;
        range++;
    }while(range<1000);
    printf("\nThe sum is: %ld", sum);
    getch();
    return 0;
}


Answer: 233168
233168
233168
233168


Hints/Background Knowledge:

                 Type Bytes             Range
---------------------------------------------------------------
         short int  2   -32,768 -> +32,767 (32kb)
unsigned short int  2   0 -> +65,535 (64Kb)
      unsigned int  4   0 -> +4,294,967,295 ( 4Gb)
               int  4   -2,147,483,648 -> +2,147,483,647( 2Gb)
          long int  4   -2,147,483,648 -> +2,147,483,647( 2Gb)
       signed char  1   -128 -> +127
     unsigned char  1   0 -> +255
             float  4 
            double  8  
       long double 12 


C++ Function: Overloading Example

#include <iostream>
 
using namespace std;
 
/* Number of arguments are different */
 
void display(char []);  // print the string passed as argument
void display(char [], char []);
 
int main()
{
   char first[] = "C programming";
   char second[] = "C++ programming";
 
   display(first);
   display(first, second);
 
   return 0;
}
 
void display(char s[])
{
   cout << s << endl;
}
 
void display(char s[], char t[])
{
   cout << s << endl << t << endl;
}

C++ Function: Print 45 Asterisks for Design the output

   using namespace std;
   void starline( );
   into main ()
   {
    starline ( );
    cout << "Data type Range" << endl;
    starline ( );
    cout << "char -128 to 127 " << endl
    << "short -32 ,768 to 32,767"<< endl
    << "int system independent: << endl
    << " long q-2,147,483,648 to 2,147,483,647" << endl;
    starline ( );
  return 0;
 }
//""""""""""""""""""""""""..
//starline ( )
// function defintion
void starline ( )
{
 for(into j=0;j<45; j++)
 cout << "*" ;
 cout << endl;
} 
 
 
The output from the program looks like this
*************************************
Data type Range
*************************************
Char -128 to 127
Short -32,768 to 32,767
Into system dependent
Double -2,147,483,648 to 2,147,483,647
*************************************

C++ Function: Adding Two Numbers with default value parameter

#include <iostream>
using namespace std;
 
int sum(int a, int b=20)
{
  int result;

  result = a + b;
  
  return (result);
}

int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;
   int result;
 
   // calling a function to add the values.
   result = sum(a, b);
   cout << "Total value is :" << result << endl;

   // calling a function again as follows.
   result = sum(a);
   cout << "Total value is :" << result << endl;
 
   return 0;
}

C++ Function : Find Maximum value between two natural numbers

#include <iostream>
using namespace std;
 
// function declaration
int max(int num1, int num2);
 
int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;
   int ret;
 
   // calling a function to get max value.
   ret = max(a, b);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
// function returning the max between two numbers
int max(int num1, int num2) 
{
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}