C++ Array Interchange: Solve Homework Problem

  • Comp Sci
  • Thread starter Saitama
  • Start date
  • Tags
    Array
In summary: So then i try x[4], x[3], x[2], x[1] and finally x[0], and it work!In summary, the problem is that the limit is incorrect.
  • #36
D H said:
That's Fortran indexing, and it is best to stick with the standard idiom of the language with which you are working. C and C++ use zero based indexing.

Sorry D H but i don't understand what do you mean by indexing.
120px-Puzzled.svg.png


I know you're just a student, but you may well find yourself programming during your job -- even if you aren't a programmer by name or degree. There are plenty of science and engineering based organizations that do a considerable amount of programming, and they typically hire scientists and engineers rather than CS majors to do that programming.

Thank you D H for the information but i am no good at Science so i don't think i would ever look into these science based organizations and here in my country, to enter the engineering, we have to choose Science and Maths in High School. There are optional subjects which we can choose from and i have selected CS. So i am not sure what will i do after High School. :smile:
 
Physics news on Phys.org
  • #37
Pranav-Arora said:
Sorry D H but i don't understand what do you mean by indexing.

1-based indexing is like this:
Code:
for (i=1; i<=n; ++i)
{
   cin >> x[i];
}
This reads x[1], x[2], ..., x[n] from standard input.

The 1-based indexing convention is used in Fortran and Pascal.
Often (but not always) in math we start counting at 1 as well.




Zero-based indexing is like this:
Code:
for (i=0; i<n; ++i)
{
   cin >> x[i];
}
It reads x[0], x[1], ..., x[n-1].

In C++ it is the convention to always use zero-based indexing.
 
  • #38
Thanks for your explanation ILS! :smile:
 
  • #39
In C (and C++) first element of an array is a[0], in some other languages it is a[1], or even its index is directly declared (something like ARRAY[-3..3] in Pascal - first object of the array declared this way is a[-3]). Indexing in this context means getting value of the object from the array using an index.

Edit: ILS posted while I was checking these Pascal declarations.
 

Similar threads

Replies
2
Views
3K
Replies
3
Views
992
Replies
7
Views
1K
Replies
2
Views
2K
Replies
14
Views
4K
Replies
21
Views
2K
Replies
3
Views
996
Replies
3
Views
1K
Back
Top