C++ Array Interchange: Solve Homework Problem

  • Context: Comp Sci 
  • Thread starter Thread starter Saitama
  • Start date Start date
  • Tags Tags
    Array
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
38 replies · 6K views
I like Serena said:
I can recommend: http://newdata.box.sk/bx/c/" .
ars_longa_vita_brevis.PNG


What's the rush? http://norvig.com/21-days.html

The book I recommend is "The C++ Programming Language" from Bjarne Stroustrup, who invented the language.
That I can concur with.
 
Last edited by a moderator:
Physics news on Phys.org
D H said:

Lol, that is really funny! :smile:
I like Serena said:
Oops! Can you correct that?

The only way i found was changing the swapping loop. :smile:
for(i=1;i<=n-1;i+=2)
 
Last edited:
D H said:
What's the rush? ][PLAIN]http://abstrusegoose.com/strips/ars_longa_vita_brevis.PNG[/URL][/QUOTE]

I just learned that it would only take me about 20 more years to build a flux capacitor.
I already did the other stuff.
Anyone willing to help me with that? :biggrin:
Pranav-Arora said:
The only way i found was changing the swapping loop.
for(i=0;i<=n-1;i+=2)

Good!
Now for instance with n=6 and with n=7 the proper elements will be swapped! :smile:

Now, what if i=0?
Which elements will be swapped?
 
Last edited by a moderator:
I like Serena said:
Good!
Now for instance with n=6 and with n=7 the proper elements will be swapped! :smile:

Now, what if i=0?
Which elements will be swapped?

Sorry ILS, i should have written for(i=1;i<=n-1;i+=2) :shy:

In my code, "i" can never be 0. :)
 
Pranav-Arora said:
In my code, "i" can never be 0. :)
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.

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.
 
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:
 
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.
 
Thanks for your explanation ILS! :smile:
 
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.