Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote