0

Good Afternoon,

I'm having some difficulties on an assignment where I need to write a program which will generate a list of signed random numbers, find the average of all the numbers, the average of the positive and negative numbers and the largest e number. So far I have this code, but it doesn't runs.

Here is the main cpp file:

```
#include <iostream>
#include <fstream>
#include <time.h>
#include "hw5_head.h"
using namespace std;
int main()
{
ofstream outFile;
int list[1000]; //integer array for a list
int n=0; //indicates array size
srand((unsigned)time(0));
int random_number;
for(int i=0; i<1000; i++)
{
random_number = (rand()%0)+1;
cout << random_number<<endl;
}
outFile.open("output.txt"); //output the data to file
int avg=0,avgP=0,avgN=0,maximum;
avgs(list,n,avg,avgP,avgN);
maximum=max(list,n);
printList(outFile,list,n,avg,avgP,avgN,maximum);
cout<<"\nsearch using linear search"<<endl;
cout<<"List contains 20: "<<linearSearch(list,n,20)<<endl;
cout<<"List contains 999: "<<linearSearch(list,n,999)<<endl;
int comparisions = 0;
bubbleSort(list,n,comparisions);
cout<<"\n\nNumber of comparisions in bubble sort: "<<comparisions;
cout<<"\nAfter bubble sort list elements are: "<<endl;
for(int i=0;i<n;i++)
cout<<list[i]<<" ";
cout<<"\n\nsearch using binary search"<<endl;
cout<<"List contains 14: "<<(bool)binarySearch(list,n,14)<<endl;
cout<<"List contains 99: "<<binarySearch(list,n,99)<<endl;
//closing the opened files
outFile.close();
return 0;
}
```

Header file:

```
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
#ifndef HW5_HEAD_H
#define HW5_HEAD_H
//generates random numbers and fills the array with those random numbers
void randomList(int list[ ], const int n)
{
srand(time(0));
for(int i=0;i<n;i++)
list[i]= (rand() % 1000)+1;
}
//finds average of positive, negative and all numbers seperately.
void avgs (const int list[ ], const int n, int &avg, int &avgP, int &avgN)
{
int posNums=0,negNums=0;
int posSum=0,negSum=0;
for(int i=0;i<n;i++)
{
if(list[i]>0)
{
posSum += list[i];
posNums++;
}
else if(list[i]<0)
{
negSum += list[i];
negNums++;
}
}
avgP = posSum/posNums;
avgN = negSum/negNums;
avg=(avgP+avgN)/2;
}
//finds the maximum value in the list
int max (const int list[ ], const int n)
{
int maximum =list[0];
for(int i=1;i<n;i++)
{
if(list[i] >maximum)
maximum=list[i];
}
return maximum;
}
//prints the list values,averages and maximum into the file
void printList(ofstream & outfile, const int list[ ], const int n, const int avg, const int avgP, const int avgN, const int max)
{
outfile<<"List eleemnts are: "<<endl;
cout<<"List eleemnts are: "<<endl;
for(int i=0;i<n;i++)
{
outfile<<list[i]<<" ";
cout<<list[i]<<" ";
}
outfile<<"\n\nAverage of positive numbers: "<<avgP<<endl;
cout<<"\n\nAverage of positive numbers: "<<avgP<<endl;
outfile<<"Average of negitive numbers: "<<avgN<<endl;
cout<<"Average of negitive numbers: "<<avgN<<endl;
outfile<<"Average of all numbers: "<<avg<<endl;
cout<<"Average of all numbers: "<<avg<<endl;
outfile<<"Maximum is: "<<max<<endl;
cout<<"Maximum is: "<<max<<endl<<endl;
}
//search for an element using linear search
bool linearSearch(const int list[ ], const int n, const int value)
{
for(int i=0;i<n;i++)
{
if(list[i] == value)
return true;
}
return false;
}
//sorting the list using bubble sort
void bubbleSort(int list[ ], const int n, int & counts)
{
bool swap;
int temp;
counts=0;
do
{
swap = false;
for (int count = 0; count < (n - 1); count++)
{
counts++;
if (list[count] > list[count + 1])
{
temp = list[count];
list[count] = list[count + 1];
list[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
//searching the array using binary serach
bool binarySearch(const int list[ ], const int n, const int value)
{
int first = 0, // First array element
last = n - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (list[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (list[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return found;
}
#endif
```

I appreciate any help that I can get.

Thank you