I need to write a neat and organized C++ program for the following: And if you c
ID: 674757 • Letter: I
Question
I need to write a neat and organized C++ program for the following: And if you can help can you make it nice and simple? Thank you!
C++
Arrays Write a C++ program to generate 100 integer numbers ranges 1 to 1000. Use a onedimensional array to store all numbers. Find the sum of all the numbers, the smallest, the largest, and the mean. Print the output accordingly. To enhance the understanding of C++ programming modules concepts, your program should integrate various functions.
Use the function prototypes given below:
int sum(int[], int); // return sum of numbers with given array and array size
int smallest(int[], int); // return smallest number
int largest(int[], int); // return largest number double mean(int[], int); // return mean
Explanation / Answer
#include<iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int sum(int[], int); // return sum of numbers with given array and array size
int smallest(int[], int); // return smallest number
int largest(int[], int); // return largest number
double mean(int[], int); // return mean
int main()
{
int arr[100];
for(int i=0;i<100;i++)
{
arr[i] = rand() % 1000 + 1;
}
cout << "Sum of all numbers is : " << sum(arr,100);
cout << " Largest of all numbers is : " << largest(arr,100);
cout << " Smallest of all numbers is : " << smallest(arr,100);
cout << " Mean of all numbers is : " << mean(arr,100);
return 0;
}
int sum(int arr[],int size)
{
int sum = 0;
for(int i=0;i<size;i++)
sum = sum+arr[i];
return sum;
}
int largest(int arr[],int size)
{
int max = arr[0];
for(int i=1;i<size;i++)
if(max<arr[i])
max = arr[i];
return max;
}
int smallest(int arr[],int size)
{
int min = arr[0];
for(int i=1;i<size;i++)
if(min>arr[i])
min = arr[i];
return min;
}
double mean(int arr[], int size)
{
int sum = 0;
for(int i=0;i<size;i++)
sum = sum+arr[i];
return sum*1.0/size;
}