-4

I just started to learn programming 2 months ago and I am still newbie in it. I just learn how to write a code of arrays with loop. This was my code.

#include <iostream>

using namespace std;

int main()
{
    int array[5];

    for(int x=1; x<=5; x++)
       {
        fidan[x]=16;

        cout<<x<< " --- " << array[x]<<endl;
       }

    return 0;
}

I know that array is count from 0. But I want my program to start with 1. So, at for loop instead of x=0, I write x=1. Then at my last x it started to being weird.

Can someone help me with it. I would appreciate it. Thank you

Garf365
  • 3,619
  • 5
  • 29
  • 41
F.Zrad
  • 1
  • 1
  • "But i want my program to start with 1." Then please make it from 1 to 4, not 5. – songyuanyao Jul 29 '16 at 11:47
  • You need to revist the section on arrays in the [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) you are using to learn from. the last array index is one less than its size – NathanOliver Jul 29 '16 at 11:47
  • @NathanOliver From the question: *I know that array is count from 0.* – Borgleader Jul 29 '16 at 11:47
  • @Borgleader fixed. – NathanOliver Jul 29 '16 at 11:48
  • 2
    *"But i want my program to start with 1"* - You will do yourself a *tremendous* favor for your future with the C or C++ languages by abandoning that and embracing 0..(N-1) subscripting. Consider this. If you were moving *from* a 0-based subscript language to some language that did *not* allow 0-based subscripting, there would be no alternative; you would have to succumb to what the language provides. Consider this as no different. Just because you *can* doesn't mean you should. – WhozCraig Jul 29 '16 at 11:49
  • 2
    "But i want my program to start with 1". You _can_ make a class which works like that. However, nobody will hire a C++ programmer who does that. – MSalters Jul 29 '16 at 11:57
  • 1
    The above is not your actual program because `fidan` is not `array`. Don't retype programs. Learn how to copy and paste. – stark Jul 29 '16 at 11:58

4 Answers4

1

array[5] means an array with 5 elements. These elements are:

array[0],array[1],array[2],array[3],array[4]. 

Now you can declare it as array[6] and then you will have an array[5] element. Now your code should have produced a segmentation fault when accessing the array[5] element, but this is undefined behavior so who knows to whom that memory segment belongs to.

The weird characters that you get is because that memory does not belong to the array variable and probably cannot be interpreted as int. Hope that helps.

Garf365
  • 3,619
  • 5
  • 29
  • 41
k_kaz
  • 596
  • 1
  • 9
  • 20
1

you should use one of two ways:

for (int i = 0 ; i < 5 ; i++)
    cout << array[i] << " "; 

or

for (int i = 1 ; i <= 5 ; i++)
    cout << array[i - 1] << " ";
Athena
  • 543
  • 10
  • 30
Arya Sadeghi
  • 480
  • 2
  • 16
0

Your array is with 5 elements.

When x=5 in the loop, you are accessing the 6th element, so you are out of the bounds of the array.

vordhosbn
  • 333
  • 4
  • 16
0

I know that array is count from 0.

Correct.

But I want my program to start with 1.

Well, it doesn't. As you just said.

So, at for loop instead of x=0, I write x=1.

Simply don't do that.

Then at my last x it started to being weird.

Yes, because you tried to access an array element that doesn't exist.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055