general question regarding arrays and functions

Writing another program for school and I've got something seriously screwed with this one. I may have to post code for this, but I have my main function that calls another function(switcher), I'm trying to pass two arrays to this function and have that function switch the lowest value in one array replace it with the lowest value in the second array and vice versa. The problem is when I compile the program, it builds with no errors or warnings, runs up to the point where I call the function(switcher) then has some type of serious error at runtime and closes that window, and allows me to send an error report to microsoft. Any ideas what I'm doing wrong before I post all the code



Answer this question

general question regarding arrays and functions

  • Chan Chi Hang

    This is the entire program, the error itself is when it runs into the call to the function "Switcher", somehow I have seriously messed up that funtion, if I comment out the call and the function it runs fine.

    //Chapter 6 problem 24 page 249 (lab7)
    #include <iostream.h>
    #include <stdlib.h>
     
     
     int seed=0; 
     void Fill_It(int []);
     void Fill_It2(int []);
     int Switcher(int [], int []);
     int FindLow(int [] );
     
    int main()
    {
     int i, array [20], arrayTwo[20];
     
     cout << "\n Please enter value for seed 1.";
     cin >> seed;
      srand(seed);
     
     cout << "\n Please enter value for seed 2.";
     cin >> seed;
      srand(seed);

     Fill_It(array);

    cout << "\n Array One"<<endl;
     for(i=0; i<20; ++i)
     {
      cout << arrayIdea << endl;
     }
     

    cout <<"\n Array Two"<<endl;
     Fill_It2(arrayTwo);
     
     for(i=0; i<20; ++i)
     {
      cout << arrayTwoIdea << endl;
     }
     Switcher(array, arrayTwo);

     return 0;
    }
    /*=====================================================================*/

    void Fill_It(int array[])

    {
     int i, rand_num;
     for(i=0; i<20; ++i)
     {
      rand_num=rand()%1000;
      arrayIdea=rand_num;
     }
    }

    /*=====================================================================*/

    void Fill_It2(int arrayTwo[])

    {
     int i, rand_num;
     for(i=0; i<20; ++i)
     {
      rand_num=rand()%1000+1000;
      arrayTwoIdea=rand_num;
     }
    }


    /*=====================================================================*/
    int Switcher(int array[], int arrayTwo[])
    {
     int low_one=0, low_two=0;
     int i, i_one, i_two;
     for (i=0; i<20; ++i)
     {
      if (arrayIdea<low_one)
      {
       low_one=arrayIdea;
       i_one=i;
      }
      if (arrayTwoIdea<low_two)
      {
       low_two=arrayTwoIdea;
       i_two=i;
      }
     }
     arrayTwo[i_two]=low_one;
     array[i_one]=low_two;

    cout << "\n Array One switcher"<< endl;
        for(i=0; i<20; ++i)
     {
      cout << arrayIdea << endl;
     }
    cout <<"\n Array Two switcher"<< endl;
     for(i=0; i<20; ++i)
     {
      cout << arrayTwoIdea << endl;
     }
     
    }

    I'm not even sure if that function will operate correctly after I find out the main problem, but of course I can't figure out unless I correct this problem first.


  • ruds

    Yeah thats for sure, only been taking C++ for 10 weeks now. I have no clue as to the different versions, which is better than the other, or anything of the sort, which is also why I tend to ask so many questions. I do apologize for all the questions, but without trying out each individual version, it's about the only way to learn something, lol. Thanks again,
                            Tony

  • TGA251

    No worries, glad to help.  VC6 is fine for you right now, but VC7 ( VS.NET2002 ) was a quantum leap in C++, you should pick it up if you can.  Every version since ( there are two, one is brand new ) is better, but that was the big one in terms of improvement from the previous one.

  • KvRaji

    How are you passing arrays around Are you sure you're not accessing them beyond their size


  • snap_ml

    First, iostream.h is not valid C++.  Use iostream instead ( without the .h ).  You'll also need to put 'using namespace std;'

    int i, i_one, i_two;

    The problem is that these are not given default values and you use them as indexes.

    Also, you should create variables as you need them, not all at the top of the function, unless you're using C instead of C++.



  • Stew2006

    The debugger is your friend here.  Use it!  :)

    You're probably reading from or writing to a bad memory address.

  • Fili

    They both work in VC6 ( which is a TERRIBLE C++ implimentation BTW, although you're probably a while away from finding that ), but only one is valid C++.  VC2005 finally refuses to accept iostream.h.


  • Melbert

    Setting them to 0 did fix the problem with the error. Microsoft Visual C++ 6.0 is what I'm using and I can use either:

    <iostream.h>
    or
    <iostream>
    using namespace std;

    std::cin; and std::cout;

    Overall, does it matter which way I access the library

  • andyhoff

    Oh and again the lightbulbs are "[  i  ]" which is the array index.
  • Van Vangor

    Thanks for the info, I appreciate all the help I've received from everyone in this forum, and I've also got the program working the way it should, thanks again,
    Tony

  • general question regarding arrays and functions