C++ Array Interchange: Solve Homework Problem

  • Context: Comp Sci 
  • Thread starter Thread starter Saitama
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving a C++ program that reads an array of integers and interchanges each element with the next one. Participants are troubleshooting issues with the implementation, particularly regarding the logic for swapping elements in the array based on the number of inputs.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their code and describes issues with the output when the number of elements exceeds five, indicating that the swapping logic is flawed.
  • Another participant suggests printing the array before and after swapping to diagnose input issues.
  • Concerns are raised about the loop conditions in the swapping logic, particularly the use of n/2 and how it affects the number of swaps performed.
  • Some participants propose that the swapping loop should be rewritten entirely to handle edge cases more effectively.
  • There is a discussion about the implications of odd versus even numbers of elements, with suggestions that the last element should not be swapped if there is no pair.
  • Technical corrections are mentioned regarding the use of outdated C++ syntax, such as including and using void main() instead of int main().
  • One participant expresses confusion about the logic when reaching the end of the array during swapping, particularly when the index exceeds the array bounds.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct implementation of the swapping logic, and multiple competing views on how to address the issues remain unresolved.

Contextual Notes

There are limitations in the discussion regarding the understanding of C++ standards, as some participants are using outdated practices. Additionally, there is uncertainty about the correct loop limits for different values of n, particularly for odd and even cases.

Who May Find This Useful

Students learning C++ programming, particularly those dealing with array manipulation and debugging logic in their code.

  • #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 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K