90 307
Assignments Done
99.3%
Successfully Done
In September 2020

Question #49775
Write a function called nRandomNumbers( ) that  Generates N random integers between -250 and 250 (inclusive) and saves them in a file called number.txt, (Use the function srand()).  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. Then, write a main program that  Prompts the user to input the value of N.  Calls the function nRandomNumbers( ).  Prints the average of all the negative integers
1
2014-12-08T04:45:49-0500
////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream> // for outputting to file
#include <time.h> // for time()
#include <conio.h> // for getch()
#include <stdlib.h> // or <cstdlib>
using namespace std;
//prototypes
double RandomNumbers(int N);
int* GetRandomNumbers(int N);
void SaveNumbers(int* numbers, int N);
void PrintNumbers(int* numbers, int N);
double GetAverage(int* numbers, int N);
double GetNegAverage(int* numbers, int N);
int GetMaxPositive(int*numbers, int N);
void main()
{
cout << "Enter the amount ofrandom numbers: ";
int N;
cin >> N;
double average = RandomNumbers(N);
cout << "Your average: "<< average << endl;
cout << "Press any key tocontinue...";
getch();
}
double RandomNumbers(int N);
{
int* randNumbers = GetRandomNumers(N);
cout << "Saving tofile...\n\n";
SaveNumbers(randNumbers, N);
cout << "Yournumbers:\n";
PrintNumbers(randNumbers, N);
double negAvg = GetNegAverage(randNumbers,N);
cout << "Your negativeaverage: " << negAvg << endl;
int maxPositive =GetMaxPositive(randNumbers, N);
return GetAverage(randNumbers, N);
}
int* GetRandomNumbers(int N)
{
srand(time(NULL));
int* numbers = new int[N];
for (int i = 0; i < N; i++)
{
numbers[i] = rand() %251 - 250;
}
return numbers;
}
void SaveNumbers(int* numbers, int N)
{
ofstream file;
file.open("number.txt"); //it'll be written in directory with your program
for (int i = 0; i < N - 1; i++)
{
file <<numbers[i] << " ";
}
file << numbers[N - 1];
file.close();
}
void PrintNumbers(int* numbers, int N)
{
for (int i = 0; i < N; i++)
{
char space = N % 10 ==0 ? '\n' : ' '; // if in one line there is 10 items, it'll go down; esle it
print space
cout <<numbers[i] << space;
}
cout << endl;
}
double GetAverage(int* numbers, int N)
{
double sum = 0;
for (int i = 0; i < N; i++)
{
sum += numbers[i];
}
return sum / N;
}
double GetNegAverage(int* numbers, int N)
{
double sum = 0;
int negNumCount = 0;
for (int i = 0; i < N; i++)
{
if (numbers[i] < 0)
{
sum+= numbers[i];
negNumCount++;
}
}
return sum / negNumCount;
}
int GetMaxPositive(int* numbers, int N)
{
int max = numbers[0];
for (int i = 1; i < N; i++) // westart from 1, because element with index 0 we already used
{
if (numbers[i] >max)
{
max= numbers[i];
}
}
return max;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!