4 Replies - 3612 Views - Last Post: 25 February 2015 - 02:06 PM Rate Topic: -----

#1 Darkspyda04  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-May 11

I just goofed

Posted 01 January 2015 - 09:29 PM

Take a look at this snippet of code:

void swapValues(int refArray[], int x)
{
	int nextElement = refArray[x + 1];

	refArray[x + 1] = refArray[x];
	refArray[x] = refArray[nextElement];
}


This function is supposed to swap two elements of the array. Every time I ran the program, I'd get an error. I managed to narrow the problem down to the above function but for the life of me couldn't tell what was wrong.

Can you guess?


Let's say for instance the value of the element at refArray[x + 1] is 366
Well nextElement receives that value via this line:
int nextElement = refArray[x + 1];


So when I run this:
refArray[x] = refArray[nextElement];


I'm setting the value stored at refArray[x] to the value stored at refArray[366]

The array only contains 7 elements.

:whistling:

Is This A Good Question/Topic? 0
  • +

Replies To: I just goofed

#2 lulu1122  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 17-January 15

Re: I just goofed

Posted 20 January 2015 - 04:06 AM

change the last line

refArray[x] = nextElement;

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Too busy for this
  • member icon

Reputation: 2330
  • View blog
  • Posts: 9,381
  • Joined: 03-December 12

Re: I just goofed

Posted 20 January 2015 - 07:26 AM

FYI, this forum is a "guess what I did", not an I need help section.
Was This Post Helpful? 0
  • +
  • -

#4 Soulyy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 12-February 15

Re: I just goofed

Posted 25 February 2015 - 02:03 PM

View Postastonecipher, on 20 January 2015 - 07:26 AM, said:

FYI, this forum is a "guess what I did", not an I need help section.

He's not asking for help, he knows what he did wrong and is sharing his silly error with us.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13491
  • View blog
  • Posts: 53,888
  • Joined: 12-June 08

Re: I just goofed

Posted 25 February 2015 - 02:06 PM

I believe that was referring to post #2.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1