Comp Sci C++ Array Interchange: Solve Homework Problem

  • Thread starter Thread starter Saitama
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
The discussion revolves around a C++ program designed to interchange elements of an array. The initial code fails to produce the correct output for arrays with more than five elements due to incorrect loop conditions. Participants suggest debugging techniques, including printing array values before and after swapping, and adjusting loop limits to prevent accessing uninitialized memory. The final solution involves modifying the loop to ensure it only swaps valid indices, specifically using "for(i=0; i
  • #31
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
  • #32
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:
  • #33
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:
  • #34
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. :)
 
  • #35
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.
 
  • #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:
 
  • #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 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K