Loop to Populate an Array and Then a Loop to Display the Array

Nov 19, 2008 at 1:57am
I've used this loop before. It is supposed to count up to whatever number I enter. All of a sudden, the output stops at 6.I enter 1. The program returns 1. Two? Two. All the way until 7. I enter 7, and it stops at 6.

I am using Bloodshed Dev C++, running on Vista Home Premium.

My code:

#include <iostream>
using namespace std;

int main()

{
int i;
int upperRange;
int testArray[0];

cout << "Please enter a number: ";
cin >> upperRange;

//Create array
for (i = 1; i <= upperRange; i++)
{
testArray[i] = i;
}

//Test array
for (i = 1; i <= upperRange; i++)
{
cout << testArray[i] << endl;
}

return 0;

}
Nov 19, 2008 at 2:54am
The problem is that you declared your array as size 0. You need to provide the maximum size of the array. Try using int testArray[100];

Or, if you need to be able to put in ANY number of integers, then you should use vector http://www.cplusplus.com/reference/stl/vector/
Nov 19, 2008 at 5:23am
If I use 'testArray[100] it only gives me results up to 102, no matter how high a number is entered.

I need the array size to be determined by the number entered. Is vector the only way to do this?
Nov 19, 2008 at 6:27am
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
#include <iostream>

using namespace std;

int main()
{
    int size;  //variable to store the size of array
	int *testArray; // an integer pointer variable to store 
	                // address of first cell of array

	
	cout << "Enter size of array, that is the amount of numbers you have" << endl;
	cin >> size;  
	
	testArray = new int [size]; //dynamically assign memory to an 
	                            //array of integer elements containing
	                            //'size' elements

	int i=0, count=1; //i is initialized to zero because in c++
	                  //the index of first element of an array is zero.

	while (i < size) //you can use the for loop if you like
	{
		testArray[i] = count; 
		cout << "testArray[" << i << "]\t" << testArray[i] << endl;
		++count;
		++i;
	}

	return (0);
}


What you need to know is that the first element of an array can be accessed using an index of 0.
For example, if i say my_array[0] = 34 , i'm referring to the
first element of the array and storing 34 there.
If i say my_array[1] = 27 i'm referring to the second element of the array and storing the number 27 in there.
That's why, i think, when you are using the for loop with i initialised to 1 instead of 0,
you tend to get 1 count in less!

To understand the code above (if you are not familiar with new[] operator read
http://www.cplusplus.com/doc/tutorial/dynamic.html


Nov 19, 2008 at 5:06pm
Thank you, everybody, for the help. I think arun1390's approach is going to do it. I'll try it while I'm at work.
Nov 20, 2008 at 4:26pm
Arun1390,
Thank you. That did the trick. I know that C++ starts counting at 0. I'm just using 1 as a starting place out of simplicity. It's playing loose with memory allocation, I know, but hey, beginners begin somewhere.
Nov 20, 2008 at 4:27pm
Arun1390,
Thank you. That did the trick. I know that C++ starts counting at 0. I'm just using 1 as a starting place out of simplicity. It's playing loose with memory allocation, I know, but hey, beginners begin somewhere.
Nov 21, 2008 at 3:03pm
Glad for you killingthemonkey!
wish you good luck with your c++...
Topic archived. No new replies allowed.