Thread: Confused with array&function

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    18

    Confused with array&function

    Hi,it seems the more i read the more it started to be confusing.maybe better ask someone who knows..
    Code:
    int i=0,j=0;
    while(i<11)
    {
      if (!(a==i))
      {
      new_array[j]=array[i]
      j++;
      }
      i++;
    }
    i want to produce one array from another as you see , i can do what i want in main but i want to make it with function . i want to send the "a" value to function and get new_array return. how to do that?

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Well.. you will have to have 2 arrays, and 1 value somewhere or another. I'm assuming you want to pass 1 array, and 1 value, to a function, the function then will assign the values of this temp. array (j) to the values of the passed array (i), unless you are on the element (a) of the value you passed (a), and you are going to assume there are only 10 elements total (i = 0, i < 11).

    I dont know what type of arrays these are, so I'm just putting 'int' as the return type. So.. simple fill in the parts you havent done yet:

    Code:
    int ChangeArray(int array[10], int a)
    {
      int i=0, j=0;
      int new_array[10];
    
      while(i<11) {
        if (!(a==i)) {
          new_array[j]=array[i];
          j++;
        }
        i++;
      }
    
      return new_array[10];
    }
    You then assign the array (from wherever you are calling this function from) to this function (ie. in main() you have someArray[10], assign someArray[10] = ChangeArray(someArray, 6);.

    I would suggest not returning an array unless its necessary, you could have simply passed a pointer to the first element of 'array' and since its a pointer, it would have modified the array from whereever it came from; you would not need to return anything. For example, in the first algorithm you are just removing an element, the one specified in a, well you could go and assign it all to a new array, which could be who knows how big, or you could use a simple booleon to determine when you've found the point you were looking for, and start copying from there, but instead of copying to a new array, you copy to the current array but 1 element up (++i), and it continues to do so from then on (founda = 1):

    Code:
    void ChangeArray(int *array, int a)
    {
      int i=0;
      bool founda;
    
      while(i<11) {
        if (a==i || founda == 1) {
          array[i] = array[++i];
          founda = 1;
        }
        
      }
    
    }
    
    int main()
    {
      int someArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    
      for(int i = 0; i < 10; ++i)
      std::cout << someArray[i];
    
      std::cin.get();   
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    well you already made a great help ..it would be of course better if i do it with pointer..
    but i shouldnt change first array,coz what i want to do is to compose sub array for each array elements i.e :i will use this function in a for loop and lets think i have an array [0,1,2,3,4] so i want to have sub array for 0 [1,2,3,4] or for 3 [0,1,2,4] and so on.and i dont need to save each of this sub arrays coz i will use them intermadiate.so how to make that new array in that function

    or something like this

    Code:
    int main()
    {
    //some codes goes here
    
    for (i=0;i<5;i++)
    make_new_array(i);
    
    //some more here :)
    
    }
    
    make_new_array(???)
    
    and here i make the new array
    Last edited by turkertopal; 10-11-2005 at 10:59 AM.

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by turkertopal
    well you already made a great help ..it would be of course better if i do it with pointer..
    but i shouldnt change first array,coz what i want to do is to compose sub array for each array elements i.e :i will use this function in a for loop and lets think i have an array [0,1,2,3,4] so i want to have sub array for 0 [1,2,3,4] or for 3 [0,1,2,4] and so on.and i dont need to save each of this sub arrays coz i will use them intermadiate.so how to make that new array in that function

    or something like this

    Code:
    int main()
    {
    //some codes goes here
    
    for (i=0;i<5;i++)
    make_new_array(i);
    
    //some more here :)
    
    }
    
    make_new_array(???)
    
    and here i make the new array
    ... Then use the code I first posted, if you read it - it may not have the function name MakeNewArray, but it does just that.

    ..... .... "make_new_array()"? in my last post I already showed you how, int ChangeArray(int array[10], int a), send the array you want a sub of and the number you want to have a sub, then assign a new array to whatever it returns.

    If thats not enough to understand..


    Code:
    int MakeSubArray(int array[10], int a) //removes element of a
    {
      int i=0, j=0;
      int newArray[10];
    
      while(i<11) {
        if (!(a==i)) {
          newArray[j]=array[i];
          j++;
        }
        i++;
      }
    
      return newArray[10]; //return the new array
    }
    
    int main()
    {
      int someArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
      int subArray[9]; //the subarray for element 7
    
      subArray[9] = MakeSubArray(someArray, 7); //there will only be 9 in this array
    
      for(int i = 0; i < 9; ++i)
        std::cout << someArray[i]; //print the result
    
      std::cin.get();
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    mmm still not working..i feel like dummy head ... but here is how i compile it:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int MakeSubArray(int array[10], int a) //removes element of a
    {
      int i=0, j=0;
      int newArray[9];
    
      while(i<10) {
        if (!(a==i)) {
          newArray[j]=array[i];
          j++;
        }
        i++;
      }
    
      return newArray[9]; //return the new array
    }
    
    int main()
    {
      int someArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9,10};
      int subArray[9]; //the subarray for element 7
    
      subArray[9] = MakeSubArray(someArray, 7); //there will only be 9 in this array
    
      for(int i = 0; i < 9; ++i)
        cout << subArray[i]<<endl; //print the result we need to print subArray here so i changed it like this
     
      cin.get();
    }
    the result is not even one wrong array but a few wrong arrays..

    and what is confusing me is return type of function is int.so how do we return whole array without pointer?

    anyone can suggest good tutorial explains everything bout arrays and functions?
    Last edited by turkertopal; 10-11-2005 at 03:48 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I didn't read the thread, but it looks like Dae's MakeSubArray function is wrong. You should not return a local variable, and it is attempting to return newArray. (I'm going to ignore the fact that that would not be how you would return an array, an int, or anything else and that the return statement is accessing data outside the array bounds.)

    The correct way to use a function for this is to pass both the source and target arrays to the function. Your function prototype should look like this:
    Code:
    void MakeSubArray(int array[10], int newArray[9], int a)
    Hopefully you can figure out the rest from there.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    ...and you are going to assume there are only 10 elements total (i = 0, i < 11).
    In that case, the loop should be while (i<10) since 10 is not a valid index for an array with ten elements. Or better yet, use a for loop
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    I didn't read the thread, but it looks like Dae's MakeSubArray function is wrong. You should not return a local variable, and it is attempting to return newArray. (I'm going to ignore the fact that that would not be how you would return an array, an int, or anything else and that the return statement is accessing data outside the array bounds.)

    The correct way to use a function for this is to pass both the source and target arrays to the function. Your function prototype should look like this:
    Code:
    void MakeSubArray(int array[10], int newArray[9], int a)
    Hopefully you can figure out the rest from there.
    There is no right and wrong when both ways work, only the best way to do it, or most effective, or efficient. Obviously using a for loop is a better way to do it, but I left parts of the code as similar to his code as possible on purpose.

    Its good you pointed that out though, the better way to do it.
    Last edited by Dae; 10-11-2005 at 06:38 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is right and wrong when one way is illegal and might cause a crash, or worse, silently incorrect output. My post that you quoted wasn't talking about while loop versus for loop.

    It was talking about returning newarray[10] which is illegal because it is out of bounds (valid indexes for a 10 element array are 0-9). It is also incorrect because it is attempting to return the local array. Even if the function returned a pointer (the correct way to return an array), that would be a pointer to a local array that goes out of scope when the function ends.

    Your code has helpful ideas, but it shouldn't be used by the OP as is.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    Thanks a lot guys it finally worked..and i learned to do this both with or without pointers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM