using c++ Write a main function that reads numbers into an array(of capacity 100
ID: 3827446 • Letter: U
Question
using c++
Write a main function that reads numbers into an array(of capacity 100) from the file input text, After all the numbers have been input, print their average as well any elements in the array whose value is below the average. Assume the numbers are terminated by the value - 9999 in the file. Make sure you include logic to ensure the file was opened successfully, that the array does not overflow, and that no division by 0 is performed (throw exception in each of these cases). Also have main catch any exception thrown, printing out the exception message. A. declare a variable, p, of type Patient, and assign "Smith" to the name, 27 to the age, 120 to systolic and 75 to the diastolic fields.Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
void main()
{
int x,n = 0;
int sum=0, count=0;
float avg;
int arr[100];
try
{
ifstream File;
File.open("integers.txt");
while(!File.eof())
{
File >> x;
if(x==9999 || n>=100) break;
arr[n]=x;
n++;
}
File.close();
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
if(n==0) throw "Div by Zero ";
avg=sum/n;
cout <<"Average of the array is "<< avg<<endl;
for(int i=0;i<n;i++)
{
if(arr[i] < avg)
cout << arr[i]<<" ";
}
}
catch(const char* msg)
{
cout <<"Exception Caught";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.