Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program that sorts an array. One array is filled with 10 randomly

ID: 3758827 • Letter: I

Question

I am writing a program that sorts an array. One array is filled with 10 randomly generated numbers ranging from 0-99. The other array is filled with indexes that correspond to the values of the first array. The array of indexes needs to be sorted in ascending order according to the values in the first array. I almost have it figured out but everytime I try to compile it, my compiler does some weird things. The code compiles but it says something about a run time check failure. The sorting function almost works but it gives me a big negative number for some reason and it also seems to skip an index. Can anyone tell me what the problem could be?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//declare functions
void initializeArray(int numbers[]);

void indexSort(int numbers[], int indexes[]);

int main()
{
   //declare variables
   int numbers[10], indexes[10];

   //randomly initialize the array numbers
   initializeArray(numbers);

   //initialize the array indexes
   for (int i = 0; i < 10; i++)
   {
       indexes[i] = i;
   }

   //display the arrays before the sorting
   cout << "numbers:";
   for (int i = 0; i < 10; i++)
   {
       cout << numbers[i] << " ";
   }
   cout << " indexes:";
   for (int i = 0; i < 10; i++)
   {
       cout << indexes[i] << " ";
   }

   //sort the array
   indexSort(numbers, indexes);

   //display the arrays after the sorting
   cout << " ************************************** ";

   cout << "numbers:";
   for (int i = 0; i < 10; i++)
   {
       cout << numbers[i] << " ";
   }
   cout << " indexes:";
   for (int i = 0; i < 10; i++)
   {
       cout << indexes[i] << " ";
   }

   system("pause");
   return 0;
}

//generate 10 random integers from 0-99
//assign them to array numbers
void initializeArray(int numbers[])
{
   srand(time(0));
   for (int i = 0; i < 10; i++)
   {
       numbers[i] = rand() % 100;
   }

}

//sorts array indexes in ascending order according to
//the values in the array numbers
//does not alter array numbers in any way
void indexSort(int numbers[], int indexes[])
{
   int tempNumbers[10];
   for (int i = 0; i < 10; i++)
   {
       tempNumbers[i] = numbers[i];
   }
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (tempNumbers[j] > tempNumbers[j + 1])
           {
               int temp = tempNumbers[j];
               tempNumbers[j] = tempNumbers[j + 1];
               tempNumbers[j + 1] = temp;

               int tempIndexes = indexes[j];
               indexes[j] = indexes[j + 1];
               indexes[j + 1] = tempIndexes;
           }
       }
   }
}

Explanation / Answer

#include <iostream>

#include <time.h>

using namespace std;


void initializeArray(int numbers[]);

void indexSort(int numbers[], int indexes []);

int main()

{
  
  
int numbers[10], indexes[10];

  
initializeArray(numbers);
for(int i=0; i<10; i++)
      
indexes[i] = i;

  
cout << "numbers:" ;
  
for(int i=0; i<10; i++)
      
cout << numbers[i] << " ";

  
cout << " indexes:" ;

  
for(int i=0; i<10; i++)
      
cout << indexes[i] << " ";

  
indexSort(numbers, indexes);
  
  
cout << " ***************************** " ;
  
cout << "numbers:" ;
  
  
for(int i=0; i<10; i++)
      
cout << numbers[i] << " ";
  
cout << " indexes:" ;
  
  
for(int i=0; i<10; i++)
      
cout << indexes[i] << " ";
  
  
  
  
system("pause");
  
return 0;

}


void initializeArray(int numbers[])

{
     
  
srand(time(0));


for (int i = 0; i < 10; i++)
      
numbers[i] = rand() % 100;
  

}


void indexSort(int numbers[], int indexes [])

{
  
int temp;
  
int numbers2[10];

  
  
for (int i = 0; i < 10; i++)
      
numbers2[i] = numbers[i];

  
for (int i = 0; i < 10; i++)
  
{
      
for (int j = 0; j < 10; j++)
      
{
          

if (numbers2[i] < numbers2[j])
          
{
      
temp = numbers2[i];
              
numbers2[i] = numbers2[j];
              
numbers2[j] = temp;

                 
temp = indexes[i];
              
indexes[i] = indexes[j];
              
indexes[j] = temp;
          
}
      
}
  
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote