i>=1??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio>
#define SIZE 4

int main()
{
	float score[SIZE];
   int i;
	cout<<"Enter "<<SIZE <<" floats: \n";
   for(i=1; i<=SIZE;i=i+1)
   	cin>>score[i];
   cout<<"The scores in reverse order are: \n";
   for(i=SIZE; i>=1; i=i-1)
   	{cout<<score[i];
      cout<<endl;}

   getch();
   return 0;

}


Hi, can I know why I must put ">" inside line 13 above? Why can't I do like the one below? I mean the "end" point is just at i=1, so why do we need to use i>=1 instead? Thanks...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio>
#define SIZE 4

int main()
{
	float score[SIZE];
   int i;
	cout<<"Enter "<<SIZE <<" floats: \n";
   for(i=1; i<=SIZE;i=i+1)
   	cin>>score[i];
   cout<<"The scores in reverse order are: \n";
   for(i=SIZE; i=1; i=i-1)
   	{cout<<score[i];
      cout<<endl;}

   getch();
   return 0;

}
I think you meant i==1, as i=1 is assignment, not comparison.

The second part of the for loop is not the end loop. It's the loop condition; the loop will only run so long as that condition is true, just like a while loop.
Your code invalid because indexing of array elements starts from zero. So your array score has no element with the index equal to 4.

Also if you have a modern compiler it shall issue an error because you did not specify that you are using names from the standard name space. The correct code will look

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
#include <iostream>
#include <conio.h>

using namespace std;

#define SIZE 4

int main()
{
   float score[SIZE];

   cout << "Enter " << SIZE << " floats: \n";

   for ( int i = 0; i < SIZE; i++ ) cin >> score[i];

   cout << "The scores in reverse order are: \n";

   for ( int i = SIZE; i >= 1; i-- ) 
   {
      cout << score[i-1];
      cout << endl;
   }

   getch();

   return 0;
}
Last edited on
What ZHuge said. The second part is the running condition, not the stopping condition. It might be easier to understand by turning it into a while loop:

for (int i = SIZE; i >= 1; i--) { ... }
is equivalent to:
1
2
3
4
5
int i = SIZE;
while (i >= 1) {
   ...
   i--;
}
Thanks Gaminic and Zhuge, I understand it...

Then I try to use while loop...is this a correct way to write while loop?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio>
#define SIZE 4

int main()
{
	float score[SIZE];
   int i;
	cout<<"Enter "<<SIZE <<" floats: \n";
   for(i=1; i<=SIZE;i=i+1)
   	cin>>score[i];
   cout<<"The scores in reverse order are: \n";

   i=SIZE;
   while(i>=1)
   {
		cout<<score[i];
      cout<<endl;
      i--;
      }

   getch();
   return 0;
}


And also is that what vlad said is correct? I mean is that array must use int i = 0; inside line 14(vlad code) instead of int i=1;? Thanks...
Last edited on
The above while loop should work. and Vlad is right. An array of size 4 is actually 5 indexes, 0 1 2 3 and 4. index 4 is used for the "null terminator" or /0. This tells the computer where your array ends. 0 1 2 and 3 store all four of your values.
Last edited on
Thanks...Means that both of the code below can works fine too right? Only the array concept wrong if using starting as int=1 right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio>
#define SIZE 4

int main()
{
	float score[SIZE];
   int i;
	cout<<"Enter "<<SIZE <<" floats: \n";
   for(i=1; i<=SIZE;i=i+1)
   	cin>>score[i];
   cout<<"The scores in reverse order are: \n";

   i=SIZE;
   while(i>=1)
   {
		cout<<score[i];
      cout<<endl;
      i--;
      }

   getch();
   return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio>
#define SIZE 4

int main()
{
	float score[SIZE];
   int i;
	cout<<"Enter "<<SIZE <<" floats: \n";
   for(i=0; i<=SIZE-1;i=i+1)
   	cin>>score[i];
   cout<<"The scores in reverse order are: \n";

   i=SIZE-1;
   while(i>=0)
   {
		cout<<score[i];
      cout<<endl;
      i--;
      }

   getch();
   return 0;
}
The above while loop should work. and Vlad is right. An array of size 4 is actually 5 indexes, 0 1 2 3 and 4. index 4 is used for the "null terminator" or /0. This tells the computer where your array ends. 0 1 2 and 3 store all four of your values.


An array of size 4 has 4 valid indexes. 0, 1, 2 and 3. There is no "null terminator" that tells the computer where your array ends.
Thanksn cire for ur explanation of "null terminator", actually before that I confuse why we need it, now I know that no "null terminator" actually....

how about the 2 codes i type above? Is that array concept wrong? Or still acceptable?
1
2
for(i=0; i<=SIZE-1;i=i+1)
   	cin>>score[i];


This is correct, if somewhat odd. i < SIZE or i != SIZE would be more usual.

1
2
for(i=1; i<=SIZE;i=i+1)
   	cin>>score[i];


This is not correct. It uses the indexes 1-4, and as we've already established, valid indexes are 0-3.


And I just noticed: you have the indexes wrong in the first block for the while loop and right in the second code block. Strangely, in the opposite way you got the for loops right/wrong.
Last edited on
Thanks cire,

u mean this 1st block wrong is it?
1
2
3
4
5
   for(i=1; i<=SIZE;i=i+1)


   i=SIZE;
   while(i>=1)


and this 2nd block correct?

1
2
3
4
   for(i=0; i<=SIZE-1;i=i+1)

   i=SIZE-1;
   while(i>=0)


May I know the reasons? Because I can get both same answer(but different code)...confusing...haha...
Last edited on
It uses the indexes 1-4, and as we've already established, valid indexes are 0-3.
atjm88 wrote:
Thanksn cire for ur explanation of "null terminator", actually before that I confuse why we need it, now I know that no "null terminator" actually....
NerdTastic actually confused that with a string. if you have something like this: const char *x = "1234"; then he's right. There's a '\0' terminator and 5 elements and you can use 'x' as an array.
Topic archived. No new replies allowed.