Changing array values in function

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 4452 Views - Last Post: 01 July 2012 - 04:25 PM Rate Topic: -----

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Changing array values in function

Posted 30 June 2012 - 01:12 PM

I'm passing an array of floats to a function, the function then needs to change the elements in the array it got as a parameter.

My attempt:
function definition:
void change(float vals[]){
    vals[0] = 2.0f; 
}



call:
float vals[10];
change(vals);


Is This A Good Question/Topic? 0
  • +

Replies To: Changing array values in function

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Changing array values in function

Posted 30 June 2012 - 01:23 PM

What's your question? In what way did the code you tried not work or do what you expected?
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Changing array values in function

Posted 30 June 2012 - 01:23 PM

And??
Was This Post Helpful? 0
  • +
  • -

#4 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: Changing array values in function

Posted 30 June 2012 - 01:28 PM

View PostJackOfAllTrades, on 30 June 2012 - 01:23 PM, said:

And??


And my code didn't work, do you know a solution?

View Postsepp2k, on 30 June 2012 - 01:23 PM, said:

What's your question? In what way did the code you tried not work or do what you expected?


It didn't change the value
Was This Post Helpful? -1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Changing array values in function

Posted 30 June 2012 - 01:32 PM

View PostJava Student, on 30 June 2012 - 10:28 PM, said:

It didn't change the value


You either made a mistake when testing whether the value had changed, you somehow changed it back after the call to change, but before you tested it, or you corrupted your memory somehow, rendering your test useless.

It's impossible to say anything beyond that without an actual runnable code sample that reproduces your problem.
Was This Post Helpful? 1
  • +
  • -

#6 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: Changing array values in function

Posted 30 June 2012 - 02:18 PM

Yeah it was an error on my part with my program, it runs fine making a seperate program with just this code.
Was This Post Helpful? -1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: Changing array values in function

Posted 30 June 2012 - 02:33 PM

You have 305 posts; by now you should know how to

1. ask a proper question
2. provide a more accurate bug report than "it doesn't work"
Was This Post Helpful? 0
  • +
  • -

#8 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: Changing array values in function

Posted 30 June 2012 - 03:15 PM

View PostJackOfAllTrades, on 30 June 2012 - 02:33 PM, said:

You have 305 posts; by now you should know how to

1. ask a proper question
2. provide a more accurate bug report than "it doesn't work"


1.) I gave you a problem and my attempt.
2.) I said the function needs to change values of the array passes to the function. Then i gave you my code(function defn. + call to function). At this point, any rational person would see that the OP's code does not work and raises the bug report "Why is the call to change the variable inside the function not working?" You have 20k+ posts, i would think you could figure this out.

This post has been edited by Java Student: 30 June 2012 - 03:15 PM

Was This Post Helpful? -2
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Changing array values in function

Posted 30 June 2012 - 03:39 PM

I usually avoid giving negative rep, but you completely failed to report what makes you think that the function was not working correctly.

Looking at just the code in the OP, it works correctly.\
void change(float vals[]){
    vals[0] = 2.0f; 
}

int main(int argc, char* argv[])
{
    float vals[10];
    change(vals);
    printf("%f", vals[0]);
    return 0;
}



Results in vals[0] containing 2.0f instead of some uninitialized value.

This post has been edited by Skydiver: 30 June 2012 - 03:43 PM

Was This Post Helpful? 1
  • +
  • -

#10 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 483
  • Joined: 05-February 10

Re: Changing array values in function

Posted 30 June 2012 - 03:54 PM

View PostSkydiver, on 30 June 2012 - 03:39 PM, said:

I usually avoid giving negative rep, but you completely failed to report what makes you think that the function was not working correctly.

Looking at just the code in the OP, it works correctly.\
void change(float vals[]){
    vals[0] = 2.0f; 
}

int main(int argc, char* argv[])
{
    float vals[10];
    change(vals);
    printf("%f", vals[0]);
    return 0;
}



Results in vals[0] containing 2.0f instead of some uninitialized value.


"you completely failed to report what makes you think that the function was not working correctly." - Uhh, if i knew why it the function wasn't working correctly then i wouldn't have a problem.

Though it looks like you could find the problem, + the solution easily, i just was wondered why others couldnt find either.
Was This Post Helpful? -1
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Changing array values in function

Posted 30 June 2012 - 04:05 PM

But there is no problem in the original code you posted. You stated that you wanted the function to change the elements of an array that the function got. Your function did exactly that. This is why people were asking you go beyond "it doesn't work" and describe the behavior that you are seeing and what attempts you had made.

In the code I posted, I showed that your code as posted in the OP works correctly. (Somehow you tagged it as not helpful, but whatever.)

This post has been edited by Skydiver: 30 June 2012 - 04:06 PM

Was This Post Helpful? 1
  • +
  • -

#12 starrim  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 21
  • Joined: 08-May 12

Re: Changing array values in function

Posted 01 July 2012 - 09:34 AM

I guess you'd better show the complete code , because there is no significant error in the code you shown.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Changing array values in function

Posted 01 July 2012 - 10:25 AM

Java Student, why the down vote for post #11?

I don't see any factual errors in the post. I don't see any personal attacks in that post. It doesn't post any redundant information because it tries to explain why people have been asking you for more information. Even sepp2k, who is very sparing with his up votes, upvoted post #11.

So what is it about post #11 that was not helpful?
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: Changing array values in function

Posted 01 July 2012 - 11:19 AM

View PostSkydiver, on 01 July 2012 - 12:25 PM, said:

Java Student, why the down vote for post #11?

I don't see any factual errors in the post. I don't see any personal attacks in that post. It doesn't post any redundant information because it tries to explain why people have been asking you for more information. Even sepp2k, who is very sparing with his up votes, upvoted post #11.

So what is it about post #11 that was not helpful?

Not to hijack the thread but this is why the rep system is flawed IMO. Most people who aren't regulars here will give you a negative vote because you negative voted them even though it was more than likely warranted to negative vote them in the first place. It is what it is, and unless you're a stat "whore", it shouldn't really matter.
Was This Post Helpful? 1
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Changing array values in function

Posted 01 July 2012 - 11:34 AM

I'm not chasing rep points. I just wanted to learn what I did wrong to make the post not useful so that I can write better posts in the future. With English not being my native language, and having learned both American and British English in my grade school and high school years depending on what textbooks were available to our schools at particular times, I may at times try to say something in wry British humour but use American terms and thereby come across as pointed rather than tongue in cheek.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2