Write a program to process integer and double arrays as follows. Take a list of
ID: 3547295 • Letter: W
Question
Write a program to process integer and double arrays as
follows. Take a list of ten numbers, and place them in an array. Find the smallest number in the list,
and also compute the sum of the first N values in the array using two methods: an iterative method,
and a recursive method. Print results using a function.
Write the following functions:
Function 1:
Explanation / Answer
#include <iostream>
#include <fstream.h>
using namespace std;
void get_array_data(int a[], int n=10) //Function 1
{
for(int i=0; i<n; i++)
{
int tmp;
cin>>tmp;
if(!cin)
{cout<<"Re-input: ";cin>>tmp;}
a[i]=tmp;
}
}
int get_smallest(int a[], int n=10) //Function 2
{
int min=a[0];
for(int i=1; i<n; i++)
{
if(a[i]<min)
min=a[i];
}
return min;
}
double get_smallest(int a[], int n=10) //Function 3
{
int min=a[0];
for(int i=1; i<n; i++)
{
if(a[i]<min)
min=a[i];
}
return min;
}
int iterate_sum(int a[], int n=10) //Function 4
{
int sum=0;
for(int i=0; i<n; i++)
sum=sum+a[i];
return sum;
}
int recur_sum(int *a, int n=10) //Function 5
{
return *a+recur_sum(a++,--n);
}
void print_data(int a[], int n=10) //Function 6
{
cout<<get_smallest(a,n);
cout<<iterate_sum(a,n);
cout<<iterate_sum(a,5);
cout<<recur_sum(a,n);
cout<<recur_sum(a,5);
}
int DynamicArrayMemory(int n=10) //Function 7
{
int a[n];
return *a;
}
int main() //Main
{
ifstream readfile;
readfile.open("input.txt");
int c=0;
readfile>>n;
int a[]=DynamicArrayMemory(n);
while(!readfile.EOF())
{
readfile>>(a[c]);
c++;
}
readfile.close();
print_data(a,n);
for (int i=0; i<n; i++)
delete a[i];
delete[] a;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.