Generating signed numbers

Good Afternoon,

Can anyone help me to figure out why the program crashes when the show the list of the generated 1000 numbers and it doesn't output the results of the functions. Here is the code that I have.
Main cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>
#include "hw5_head.h"

using namespace std;

int main()

{
		ofstream outFile;
			
        const int n = 1000; //indicates array size
		int list[n]; //integer array for a list
        int avg,avgP,avgN,maximum;
		int value, counts;
		
		randomList(list, n);
        
		showList(list, n);    
		
	
		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: "<<linearSearch(list,n,value)<<endl;
		
		bubbleSort(list,n,counts);

        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: "<<(bool)binarySearch(list,n,value)<<endl;

           
            //closing the opened files

         
            outFile.close();

            return 0;

} 
Heade File:
[code]
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>

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)

{
	signed seed;
	cout << "Enter a random seed ";
	cin>>seed;
	srand(seed);

	for (int i = 0; i < n; i++)
	{
		list[i] = rand()%1000;
	}
	return;
}

//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 elements are: "<<endl;
	cout<<"List elements 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;
   int i,j;

   counts=0;

   for (i = 0; i<n; i++)
   {
	   swap = false;
	   for (j = 0; j < n - i - 1; j++)
	   {
		   if (list[j] > list [j+1])
		   {
			   swap = true;

			   temp = list[j];
			   list[j] = list[j+1];
			   list[j+1] = temp;

			   counts++;
		   }
	   }
		   if(!swap)
			   break;
	}
	   return;
}

   

//searching the array using binary serach

bool binarySearch(const int list[ ], const int n, const int value)
{
	int first = 0, last = n - 1, middle, position = -1;         
	bool found = false;       

    while (!found && first <= last)
	{
		middle = (first + last) / 2;     
		if (list[middle] == value)      
		{
			found = true;
			position = middle;
		}
		else if (list[middle] > value)  
			last = middle - 1;
		else
			first = middle + 1;          
	}
	return found;
}
void showList(const int list[], const int n)
{
	cout<<"The list is ..... " <<endl;
	for (int i=0; i<n; i++)
	{
		cout<<setw(8)<<right<<list[i];
		if ((i+1)%1000==0)
			cout<<endl;
	}
	cout<<endl;
}
#endif


[/code]
Every variable is NOT getting initialized. That is bad practice to my eyes. Initialize them all to 0, then check the results.
Functions randomList and showList are correct. However all array elements are positive. So the statement

avgN = negSum/negNums;

results in crash because negNums will be equal to 0.
EssGeEich,

I already initialized all the variables to 0 and still the program crashes
vlad from moscow,

How would implement to have the array elements as negative, so avgN = negSum/negNums won't crash
For example you could use the following expression

list[i] = rand() % n - n / 2;
vlad from moscow,

The program is now generating postive and negative numbers, but I having trouble with the function avg because it isn't outputting the correct results. Here is the print screen that I'm getting.

Average of positive numbers: 250
Average of negitive numbers: -252
Average of all numbers: -1
Maximum is: 499
I think it is becauss that you use incorrect formula. Try the following

avgP = posSum/posNums;
avgN = negSum/negNums;
avg = (avgP+avgN) / n;

Also it would be better if you define avg, avgP, avgN as float and the statenents above would be rewritten the following way


avgP = ( float )posSum/posNums;
avgN = ( float )negSum/negNums;
avg = ( float )(avgP+avgN) / n;
vlad from moscow wrote:
avgP = ( float )posSum/posNums;
avgN = ( float )negSum/negNums;
avg = ( float )(avgP+avgN) / n;

Or, even better:

avgP = ( float )posSum/(float)posNums;
avgN = ( float )negSum/(float)negNums;
avg = ( float )(avgP+avgN) / (float)n;

Otherwise it would be like a "float / int" which causes not-so-precise behaviour and a warning in most cases.
@EssGeEich
Do not make up stories .There are precise behaviour and none warnings. So the second casting is excessive. It is much better to write as I showed above.

Right you are. [offTopic]Just sayin', you look nervous these days, vlad... Not the first nor second time I see you getting nervous.[/offTopic]
thank you for your help
Topic archived. No new replies allowed.