Array question (C++)

  • Comp Sci
  • Thread starter Saitama
  • Start date
  • #26
3,816
92
Hmm, suppose n is even, say n=6.
Then the highest index you will get inside your loop is i=6.
Which 2 elements will be exchanged then?

If you are asking about the code i just posted, then the 2 elements will be exchanged are x[5] and x[6].

When the swapping loop starts, the values present in x[1], x[3] and x[5] gets stored in the tmp and are exchanged. For example, value in x[1] gets exchanged with x[2], x[3] gets exchanged with x[4] and x[5] exchange with x[6]. In the swapping loop, the value of i cannot reach 6. :smile:

[offtopic]Can you suggest me some good resources for learning C++? I missed some of my C++ classes on the looping section and i had to learn for-loop on my own. :( [/offtopic]
 
  • #27
D H
Staff Emeritus
Science Advisor
Insights Author
15,415
687
If you are asking about the code i just posted, then the 2 elements will be exchanged are x[5] and x[6].
[strike]
No, the last two elements that are exchanged are x[6] and x[7]. Look at your own code.

Does either one of those elements have an assigned value in your array?[/strike]

Edit
Ah, you are using Fortran indexing rather than C/C++. That is not a good idea.

You still have a problem though with odd n.
 
Last edited:
  • #28
I like Serena
Homework Helper
6,579
176
If you are asking about the code i just posted, then the 2 elements will be exchanged are x[5] and x[6].

When the swapping loop starts, the values present in x[1], x[3] and x[5] gets stored in the tmp and are exchanged. For example, value in x[1] gets exchanged with x[2], x[3] gets exchanged with x[4] and x[5] exchange with x[6]. In the swapping loop, the value of i cannot reach 6. :smile:

Darn! You're right! :wink:

All right, now let's take n=7. Then the highest value "i" can and shall take inside the loop is i=7.
Which 2 elements are swapped then?


[offtopic]Can you suggest me some good resources for learning C++? I missed some of my C++ classes on the looping section and i had to learn for-loop on my own. :( [/offtopic]

I can recommend: http://newdata.box.sk/bx/c/" [Broken].

The book I recommend is "The C++ Programming Language" from Bjarne Stroustrup, who invented the language.
 
Last edited by a moderator:
  • #29
3,816
92
Darn! You're right! :wink:

All right, now let's take n=7. Then the highest value i can and shall take inside the loop is i=7.
Which 2 elements are swapped then?

Thanks! :smile:

Ah, the code misbehaves at n=7. :redface:

When i did the hand-debugging process, i found that i reaches 7 and gets swapped with x[8] which is nothing in the program. Moreover, when i run the code in Turbo C++ and i gave the input:- 1234567, the output results to this:- 2143650.

Lol, its zero, does that mean x[8]=0?

I can recommend: http://newdata.box.sk/bx/c/" [Broken].

The book I recommend is "The C++ Programming Language" from Bjarne Stroustrup, who invented the language.

Thanks for the suggestions ILS, i will definitely look into these books. :smile:
 
Last edited by a moderator:
  • #30
I like Serena
Homework Helper
6,579
176
Thanks! :smile:

Ah, the code misbehaves at n=7. :redface:

When i did the hand-debugging process, i found that i reaches 7 and gets swapped with x[8] which is nothing in the program. Moreover, when i run the code in Turbo C++ and i gave the input:- 1234567, the output results to this:- 2143650.

Oops! Can you correct that?

Lol, its zero, does that mean x[8]=0?

Only if you are "lucky".
If you switch off the debugging flags in Turbo C++, I expect that you'll find it is not zero, but some very weird value.
 
  • #31
D H
Staff Emeritus
Science Advisor
Insights Author
15,415
687
I can recommend: http://newdata.box.sk/bx/c/" [Broken].
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:
  • #33
I like Serena
Homework Helper
6,579
176
What's the rush? ][PLAIN]http://abstrusegoose.com/strips/ars_longa_vita_brevis.PNG[/URL][/QUOTE] [Broken]

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:


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
3,816
92
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
D H
Staff Emeritus
Science Advisor
Insights Author
15,415
687
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
3,816
92
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
I like Serena
Homework Helper
6,579
176
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
3,816
92
Thanks for your explanation ILS! :smile:
 
  • #39
Borek
Mentor
28,816
3,323
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.
 

Related Threads on Array question (C++)

  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
12
Views
2K
Replies
4
Views
1K
  • Last Post
Replies
4
Views
15K
  • Last Post
Replies
1
Views
5K
  • Last Post
Replies
20
Views
4K
Replies
3
Views
2K
  • Last Post
Replies
5
Views
764
Top