C-+-+ Exercise #6: Write a function called nRandomNumbers() to performing the fo
ID: 664263 • Letter: C
Question
C-+-+ Exercise #6: Write a function called nRandomNumbers() to performing the following: Generates N random integers between -250 and 250 linclusive and save them in a file Prints all the generated integers. Finds the average of all the negative integers, and the maximum of all the positive integers Returns the the average and maximum values Write a main program that perform the following: Prompts the user to input the value of N Calls the function nRandomNumbers(). Prints the average of all the negative integers, and the maximum of all the positive integers.Explanation / Answer
#include<iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <fstream>
using namespace std;
int i,num, large=0;
float avg_negative=0;
void generate_Rand(int n)
{
int positive[n];
int tot_positive=0, tot_negative=0, count_negative=0, count_positive=0;
ofstream file1;
file1.open ("numbers.txt");
srand (time(NULL));
for (i=1;i<=n;i++)
{
num = rand()%501+(-250);
file1 << num;
file1<<endl;
if(num<=0)
{
count_negative++;
tot_negative=tot_negative+num;
}
else
{
count_positive++;
if(num>large)
{
large=num;
}
}
cout<<num;
cout << endl;
}
//cout<<tot_negative<<endl;
//cout<<count_negative<<endl;
avg_negative=(float)tot_negative/count_negative;
file1.close();
}
int main()
{
int n;
cout<<"Enter the value of n";
cin>>n;
generate_Rand(n);
cout<<"Largest positive number is"<<endl;
cout<<large<<endl;
cout<<"Average of negative numbers is"<<endl;
cout<<avg_negative<<endl;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.