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

// i get it to display the random chart of numbers but their is a weird number a

ID: 3774060 • Letter: #

Question

// i get it to display the random chart of numbers but their is a weird number and its not sorting from highest to lowest please help. //Thank you

//8.4 //
#include
#include
#include
using namespace std;
const int DAYSOFWEEK = 7;

//generate the Employee hours per week
void initializeEmployeeHours(int x[][DAYSOFWEEK], int r) {
   for (int i = 0; i < r; i++) {
       for (int j = 0; j < DAYSOFWEEK; j++) {//generate a random number between 0 and 8 assign to hour assign to x[i][j]
           x[i][j] = rand() % 10; // numbers added from 1-10 //10 + rand() % 90; // number between 10-99
           cout << x[i][j] << " ";
       }
       cout << endl;
   }
}
// this give us sum
void totalHours(int x[][DAYSOFWEEK], int r, int t[]) // r is the same for both t[] and x[] so you dont have to restate it
{
   for (int i = 0; i < r; i++)
   {
       int sum = 0;
       for (int j = 0; j < r; j++)
       {
           sum = sum + x[i][j]; // give us some of one row
           t[i] = sum; // total hours that a employee works is stored in a t[] array.
       }
   }
}

//saturday class for selection sort // remember to add i and a do this for the following functions
void swap(int x[], int y[])
{
   for (int i = 0; i < 7; i++)
   {
       int temp = x[i];
       x[i] = y[i];
       y[i] = temp;
   }

}


int minIndex(int x[], int start, int size) // int start makes it start at some point till the end
                                       // when you enter 3 parameters it uses this if not it uses the other with 2 parameters
{
   // can also write for loop as for (int i = 1; i < size; i++) can be written as the one on the right as well :(int i = size -1; i > 0; i--)
   int min = x[start]; //
   int index = start; //
   for (int i = start; i < size; i++)
   {

       if (min > x[i]) // if min is greater than x[i] or array then
       {
           min = x[i]; // the minimum is greater than what i have update the min.
           index = i; // this updates the index that is being compared rigth above
                   // need to keep track
                   // the i here doesnt exist out side the if loop so the i in the for loop is different
       }
       return index; // then returns index of the minimum value  
   }
}
// change out with book function version since its not working
void selectionSort(int x[], int size)
{
   for (int i = 0; i < size - 1; i++)
   {
       double currentMin = x[i];
       int currentMinIndex = i;

       for (int j = i + 1; j < size; j++)
       {
           if (currentMin > x[j])
           {
               currentMin = x[j];
               currentMinIndex = j;
           }
       }


       // calling fucntion within the function // always use variable of function above since i is 0 and chanes
       // you use that to find minimum index.

       //after you have the minimum index you swap the index untill you sort the numbers thats what the swap function does.

       if (currentMinIndex != i)
       {
           x[currentMinIndex] = x[i];
           x[i] = currentMin;
       }
   }
}


/*void printArray(int x[], int size)
{
   for (int i = 0; i < size; i++)
       cout << x[i] << " ";
   cout << endl;
}*/


void printArray(int x[], int size) // when calling an array it need to be delcared n th header as (x[]) then the size
{
   for (int i = 7; i >= 0; i--)
       cout << x[i] << " ";
   cout << endl;

}

int main()
{
   srand(time(0));
   int employeeHours[7][7] ;
       // sever days by seven employee
   //call initialize employee hours;
   int employeeTotalHour[7]; // there are seven employees their total hours // not initialized
                           //call totalHours and send and send employeeHours, number of Employees = 7, employeetotalhours
   initializeEmployeeHours(employeeHours, 7);
   totalHours(employeeHours, 7, employeeTotalHour);
   //selection sort and print the result.
   cout << "the sort is" << endl;
   selectionSort(employeeTotalHour, DAYSOFWEEK);
   printArray(employeeTotalHour, DAYSOFWEEK);

   return 0;
}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int DAYSOFWEEK = 7;
//generate the Employee hours per week
void initializeEmployeeHours(int x[][DAYSOFWEEK], int r) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < DAYSOFWEEK; j++) {//generate a random number between 0 and 8 assign to hour assign to x[i][j]
x[i][j] = rand() % 10; // numbers added from 1-10 //10 + rand() % 90; // number between 10-99
cout << x[i][j] << " ";
}
cout << endl;
}
}
// this give us sum
void totalHours(int x[][DAYSOFWEEK], int r, int t[]) // r is the same for both t[] and x[] so you dont have to restate it
{
for (int i = 0; i < r; i++)
{
int sum = 0;
for (int j = 0; j < r; j++)
{
sum = sum + x[i][j]; // give us some of one row
t[i] = sum; // total hours that a employee works is stored in a t[] array.
}
}
}
//saturday class for selection sort // remember to add i and a do this for the following functions
void swap(int x[], int y[])
{
for (int i = 0; i < 7; i++)
{
int temp = x[i];
x[i] = y[i];
y[i] = temp;
}
}

int minIndex(int x[], int start, int size) // int start makes it start at some point till the end
// when you enter 3 parameters it uses this if not it uses the other with 2 parameters
{
// can also write for loop as for (int i = 1; i < size; i++) can be written as the one on the right as well :(int i = size -1; i > 0; i--)
int min = x[start]; //
int index = start; //
for (int i = start; i < size; i++)
{
if (min > x[i]) // if min is greater than x[i] or array then
{
min = x[i]; // the minimum is greater than what i have update the min.
index = i; // this updates the index that is being compared rigth above
// need to keep track
// the i here doesnt exist out side the if loop so the i in the for loop is different
}
return index; // then returns index of the minimum value
}
}
// change out with book function version since its not working
void selectionSort(int x[], int size)
{
for (int i = 0; i < size - 1; i++)
{
double currentMin = x[i];
int currentMinIndex = i;
for (int j = i + 1; j < size; j++)
{
if (currentMin > x[j])
{
currentMin = x[j];
currentMinIndex = j;
}
}

// calling fucntion within the function // always use variable of function above since i is 0 and chanes
// you use that to find minimum index.
//after you have the minimum index you swap the index untill you sort the numbers thats what the swap function does.
if (currentMinIndex != i)
{
x[currentMinIndex] = x[i];
x[i] = currentMin;
}
}
}

/*void printArray(int x[], int size)
{
for (int i = 0; i < size; i++)
cout << x[i] << " ";
cout << endl;
}*/

void printArray(int x[], int size) // when calling an array it need to be delcared n th header as (x[]) then the size
{
for (int i = 6; i >= 0; i--)
cout << x[i] << " ";
cout << endl;
}
int main()
{
srand(time(0));
int employeeHours[7][7] ;
// sever days by seven employee
//call initialize employee hours;
int employeeTotalHour[7]; // there are seven employees their total hours // not initialized
//call totalHours and send and send employeeHours, number of Employees = 7, employeetotalhours
initializeEmployeeHours(employeeHours, 7);
totalHours(employeeHours, 7, employeeTotalHour);
//selection sort and print the result.
cout << "the sort is" << endl;
selectionSort(employeeTotalHour, DAYSOFWEEK);
printArray(employeeTotalHour, DAYSOFWEEK);
return 0;
}

===========================

Isuue was during printing : int i =7 is changed to int i = 6 in the for loop of print function.

===========================

output

===========================

1 1 8 1 8 0 8
3 7 4 1 1 8 1
3 8 9 2 2 8 3
4 6 9 1 3 7 7
2 3 0 5 5 0 7
3 0 7 8 8 2 1
1 2 3 6 0 2 0
the sort is
37 35 29 27 25 22 14

===========================