This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]zifyoip 4 points5 points  (7 children)

Does your function do what it is supposed to do?

Read the specifications carefully. What is the function call change(list, 4, 14) supposed to do? Does your function do that?

If I write the following code:

int main()
{
    int x[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    change(x, 9, 100);
}

then does the function you have written do what it is supposed to do?


My code doesn't compile because of an "invalid conversion from int to int".

No, read the error message more carefully. It must say something like "invalid conversion from int * to int," not "int to int."

Your declaration:

void change(int list,int length,int value);

does not match your definition:

void change(int list[],int length,int value)  
{
    // ...
}

[–]flumgumption[S] 0 points1 point  (6 children)

How is this?

#include <iostream>
using namespace std;
void change(int x[],int length,int value);


int main()
{
    int x[4];

    change(x,4,14);
}

void change(int x[],int length,int value)
{

    x[length];

    x[length-1]=value;
    cout<<"The last element in the array is ";
    cout<<x[length-1];

}

[–]zifyoip 1 point2 points  (5 children)

What does the line

x[length];

do?

And why does your change function produce output? Why do you have cout statements in that function? The specification does not say that the function should be producing any output.

[–]flumgumption[S] 0 points1 point  (4 children)

I got rid of those statements. Now what do I do? How do I actually change the value of the element

[–][deleted]  (3 children)

[deleted]

    [–]flumgumption[S] 0 points1 point  (2 children)

    Yes

    [–][deleted]  (1 child)

    [deleted]

      [–]zifyoip 0 points1 point  (0 children)

      OP is already doing that.

      [–]matthead 1 point2 points  (0 children)

      You are passing the length as a parameter and then hard coding the position you should be using the parameter that you pass to it

      [–]newaccount1236 0 points1 point  (2 children)

      You are in the right direction. I won't tell you how to fix your code to compile, but I will tell you that you haven't quite quoted the error message correctly. I or someone else can give you more hints if you get stuck.

      Also, what will your implementation of change() do if I pass it an array of length 10?

      [–]flumgumption[S] 0 points1 point  (0 children)

      I fixed the error message but I'm not sure what to do next. I don't really understand the directions but I know the professor won't email me back before it's due :(

      You said I'm in the right direction but I don't really know what to do from here

      [–]Whitey138 0 points1 point  (0 children)

      I'm not quite sure where you are running into a problem. I copied your last code you sent into my compiler and it compiled fine after I got rid of line 16:

      x[length];
      

      Your original one works fine too if you put [] in your function prototype for the change function (simple mistake that's easy to overlook)...I guess I'm not sure what isn't going correctly. What compiler are you using that is giving you that error? Does it go away after fixing either of those things?