Random Number Generation Being Stored in an Array Help

Hello everyone, I am very new here, but have read many forum posts. I've just finished a program, and when I go to generate 10 random numbers between 0 - 1000 and store it into an array, it just keeps spitting out the same exact super long negative number 10 times, which makes no sense whatsoever. Could someone please take a look and try to help see what I did wrong here.

-Thank you so much.

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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

const int SIZE = 10,	//The amount of numbers to be generated into the array
		max = 1000;	//The maximum value that the rand function can choose from

void createArray(int a[], int arraySize, int maximum);	//Prototype for createArray function
void bubbleSort(int a[], int arraySize);	//Prototype for bubbleSort function
void displayArray(int a[], int arraySize, int w);	//Prototype for displayArray function

int main()
{

	const int w = 6;	//The width between each column 
	int numbers[SIZE];	//The array storing the SIZE values
		


	cout << "This program will generate and then sort " << SIZE << " random numbers from 0 to " << max << "." << endl; 
	cout << endl;
	
	srand(time(0));	//Seeds the random number generator
	
	cout << "The original " << SIZE << " numbers generated." << endl;

	createArray(numbers, SIZE, max);	//Calling the createArray function

	displayArray(numbers, SIZE, w);	//Calling the displayArray function

	cout << endl;

	cout << "The " << SIZE << " numbers sorted in ascending order." << endl;
	cout << endl;

	bubbleSort(numbers, SIZE);	//Calling the bubbleSort function

	displayArray(numbers, SIZE, w);	//Calling the displayArray function


return 0;
}

void createArray(int a[], int arraySize, int maximum)	//This function will generate 10 random numbers and store it in an array

{
	int i;	//LCV or array subscript
	
	
	for (i = 0; i < arraySize; i++)	//Counting loop
	{
		
		a[i] = (rand() % maximum);	//Generates SIZE numbers to store into the array
	}
	

}

void bubbleSort(int a[], int arraySize)	//D.S Malik's bubble sort algorithm for sorting an array in ascending order
{
	int temp,	//A temporary storage variable used for swapping values
		iteration,	//Used as a counter variable
		index;	//LCV or array subscript

	for (iteration = 1; iteration < arraySize; iteration++)	//Loop control
	{
		for (index = 0; index < arraySize - iteration; index++)
			if (a[index] > a[index + 1])	//Rearranges the array values into ascending order
			{
				temp = a[index];
				a[index] = a[index + 1];
				a[index + 1] = temp;
			}
	}
}

void displayArray(int a[], int arraySize, int w)	//This function will display a given array
{
	int i;	//LCV or array subscript

	
	for (i = 0; i < SIZE; i++)	//Counting loop
	{

		cout << setw(w) << a[arraySize];	//Organizes the array into columns

	}
	cout << endl;
}
You are using the wrong variable as index while printing the array elements.
Wow, I just changed that little variable and boom, everything is running just how it's supposed to!

-Thanks so much for spotting it!

p.s. - I'm a fan of Brum Brum Rally!
Last edited on
Topic archived. No new replies allowed.