Printing My Array

Hello,

I'm trying to create an array of 10 numbers to be input by the user and print it. I want my program to be able to stop reading numbers and print the array if a negative number is entered as well.
I'm having trouble with that though. When I try to print my array, it gives me random numbers it seems. I will enter single-digit numbers but will get random numbers in the hundred-millions when I print. Also, it prints numbers for all ten spaces, even if I only enter 3 or 4 numbers.

So, what am I doing wrong here? Also, is there a way to make the program only print the numbers entered, if it's less than ten?

I really appreciate the help!

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

int main()
{
	int i;
	int array[10];
	int n;
	
	for (i=0; i<=9; i++)
	{
		cout << "\nEnter a number: ";
		cin >> n;
		
		if (n>=0)
		{
			n=array[i];
		}
		else
		{
			break;
		}
	
	}
	for (i=0; i<=9; i++)
	{
		cout << "\nNumbers are " << array[i];
	}
	
return 0;
	
}


************************************************************************

EDIT:

I have just tried to sort my array using a bubble sort. the problem with the "random" numbers has come up again, and I'm not sure why or how to fix it, again. I'm not even sure if the sort is working. What am I doing wrong?
Here's my code:

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

int main()
{
	int i;
	int array[10];
	int n;
	int SwapHolder = -1;
	
	for (i=0; i<=9; i++)
	{
		cout << "\nEnter a number: ";
		cin >> n;
		
		if (n>=0)
		{
			array[i]=n;
			
			if (array[i] > array[i+1])
			{	
				SwapHolder = array[i+1];
				array[i+1] = array[i];
				array[i] = SwapHolder;
			}
			else
			{
				continue;
			}
		}
		else
		{
			break;
		}
	
	}
	for (i=0; i<=9; i++)
	{
		cout << array[i];
	}
	
return 0;
	
}
Last edited on
line 17: n = array[i];
should be: array[i] = n;

You want to assign n to the array, not the other way round.

Also you will still get random numbers on any elements of the array you haven't given a value too, because you haven't initialised them.


It will print all ten values because you have asked it to, in you for loop.
You could declare a count, initialise it to zero, and for every value entered you increment it. Then in your for loop would go from 0 to count - 1;
Last edited on
Thank you so much! that worked, but once I tried to add a bubble sort (my first time, very unfamiliar with it), it happened again. What am I doing wrong? :(
@VisuAlly

Trying to do a sort on a unfilled array, doesn't quite work. Plus, when you try checking array[ i+1] in the loop, you're out of bounds in the array, which goes from 0 to 9. First have the array filled, and then do a bubble sort. Check with a search on this site, and you'll find out how to do one.
Oooh okay. Thanks!
Topic archived. No new replies allowed.