Answer to Question #229115 in C++ for Karthi

Question #229115
Write a C++ program to find the sum of integer and float array
elements using function overloading. Dynamic memory allocation
should be used for arrays.
1
Expert's answer
2021-08-26T04:00:28-0400


#include <iostream>


using namespace std;
int getSum(int arr[],int n){
    int sum=0;
    for(int i=0;i<n;i++){
        sum=sum+arr[i];
    }
    return sum;
}
float getSum(float arr[],int n){
    float sum=0;
    for(int i=0;i<n;i++){
        sum=sum+arr[i];
    }
    return sum;
}
int main()
{
    int n1,n2;
    cout<<"Enter number of elements in integer array: ";
    cin>>n1;
    cout<<"Enter number of elements in float array: ";
    cin>>n2;
    int arr1[n1];
    float arr2[n2];
    
    cout<<"\nEnter "<<n1<<" integer elements:\n";
    for(int i=0;i<n1;i++){
        cin>>arr1[i];
    }
    cout<<"\nEnter "<<n2<<" float elements:\n";
    for(int i=0;i<n2;i++){
        cin>>arr2[i];
    }
    
    cout<<"\nThe sum of the integer elements is: "<<getSum(arr1,n1);
    cout<<"\nThe sum of the float elements is: "<<getSum(arr2,n2);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS