0

I am currently working on a project where we have to create an array of 1000 elements then pass it to another function to sort it. Everything I have seen online shows you how to pass it from main to another function, but not the other way around.

Please take a look at my code and help me pass Ar[1000] from Array() to ISort and ultimately main

#include <iostream>
#include <time.h>
using namespace std;

void Array()//function to make array
{
    int Ar[1000];//creating array     
    int i = 0;//random variable to be element #     
    int counter = 0;// counter variable     
    int randnum;//variable to old random number     
    srand(time(NULL));//seeding rand with time

    while (counter != 1000)    
    {    
        randnum = rand();     
        Ar[i] = randnum;     
        cout << Ar[i]<<endl;     
        counter++;
    }          
}

void ISort(int Ar[1000])//Iterative sort
{     
    int count = 0;//another counter variable     
    int count2 = 0;//counter variable # 3 because nested loops    
    int j=0;//Temp index # similar to i     
    int temp; //Temp variable to help switch elements     
    while (count != 1000)
    {
        if (Ar[count] < Ar[j])
        {
            temp = Ar[count];    
            Ar[count] = Ar[j];     
            Ar[j] = temp;
        }               
    }
}

/*void RSort(int Ar)//Recursive sort
{

}
*/

int main()
{
    Array();
    ISort();
    system("Pause");
    return 0;
}
zx485
  • 28,498
  • 28
  • 50
  • 59
Crimson
  • 21
  • 9

4 Answers4

2

Ar in your Array function will be destroyed once this function finishes, you need to have a way to prevent this, one way is to pass an array by parameter instead of making it function local variable:

void Array(int* Ar, int count)//function to make array
{

I would also change Your current ISort definition to:

void ISort(int* Ar, int acount)//Iterative sort

where acount is number of elements in Ar. This is because it makes no difference whether you use void ISort(int Ar[1000]) or void ISort(int* Ar) (read here for more on this). If you want to preserve array type then you must pass it by reference using: void ISort(int (&Ar)[1000]).

Finally changes in main:

   int Ar[1000];//creating array
    Array(Ar, 1000);
    ISort(Ar, 1000);
    system("Pause");
    return 0;

working code is here: http://coliru.stacked-crooked.com/a/678f581f802da85b

You also forgot to increment count inside your sorting loop.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
1

Your array int Ar[1000] variable inside an Array() function is a local variable. Make it a global variable by moving it out of the function scope:

int Ar[1000]; //creating array
// your functions here
int main()
{
    Array();
    ISort(Ar);
    return 0;
}

You should also modify the Array() function to accept array as parameter as pointed out in the comments below. Please note that I am omitting the array size part as it seems the number of the elements is set to 1000:

void Array(int Ar[]){
    //...
};

in which case the above code would be:

int Ar[1000]; //creating array
// your functions here
int main()
{
    Array(Ar);
    ISort(Ar);
    return 0;
}
0

Change the Array function declaration to:
int* Array() and make it return the array Ar. And in main get the returned value from Array function like this:
int* Ar = Array(); and pass it to the function ISort like this : ISort(Ar);.

Here is an example on SO passing an array to a function.

Community
  • 1
  • 1
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • An example of passing the array would be beneficial. – Thomas Matthews Dec 03 '16 at 00:40
  • If you will return a local variable then it will cause UB. It would work if this array was static inside this function, or if it was dynamically allocated. – marcinj Dec 03 '16 at 01:34
  • My answer has an issue that may cause undefined behaviors as mentioned by @marcinj, i suggest that you take a look at his answer and accept it. Sorry for that and good luck. – Mo Abdul-Hameed Dec 03 '16 at 08:18
0

The easiest solution would be to change Array function a bit:

int* Array() {  // change return type to be able to return array you create
   int Ar[1000];
   for (int i = 0; i < 1000; i++) {  // much better to use for loop than while
      Ar[i] = rand();  // no need to hold another variable for random number
      cout << Ar[i] << endl;
   }
   return Ar; // return the Ar
}

int main() {
    int* array = Array();
    ISort(array);
}

Hope that helps. Also there are many other solutions to this but I don't know what exact restrictions your task has. If you have any questions feel free to ask.

EDIT: So I totally forgot about that C arrays are just a plain old pointers... Well then the solution would be like this:

void Array(Ar[1000]& array) { // pass array to the function with reference
   for (int i = 0; i < 1000; i++) {  // much better to use for loop than while
      array[i] = rand();  // no need to hold another variable for random number
      cout << array[i] << endl;
   }
}

int main() {
    int[1000] array = Array();
    ISort(array);
}

Sorry for the error but using C style arrays really isn't common in C++ when you can use vectors and maps.

Maroš Beťko
  • 2,181
  • 2
  • 16
  • 42