Deck 7: Advanced Array Techniques
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
سؤال
فتح الحزمة
قم بالتسجيل لفتح البطاقات في هذه المجموعة!
Unlock Deck
Unlock Deck
1/8
العب
ملء الشاشة (f)
Deck 7: Advanced Array Techniques
1
Using a Bubble Sort
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
Exactly how many passes are made over the data in the array? Specify a number.
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
Exactly how many passes are made over the data in the array? Specify a number.
There are totally "8" passes are made over the data in array; because the outer "for" loop in the code is used to pass each value in array.
• The outer "for" loop is executed for "8" times (That is, "0" to "7").
• The given code uses "numPasses" variable to monitor the number of passes count. At each iteration in the "for" loop, the "numPasses" variable is incremented by "1".
• At end of iteration, the value of "numPasses" variable is ends with "8".
Therefore, the total number of passes made over the data in the array is "
".
Program code:
Program used to demonstrate about sorting data using a bubble sort is as follows:
// Include header files
#include
using namespace std;
Define a main function to sort set of values in an array.
// Function main
int main()
{
// Integer array with values
int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};
// Declare required variables
const int NUM_ITEMS = 9;
int tmp;
int numPasses = 0, numCompares = 0, numSwaps = 0;
Sort the values in an array.
// Outer loop to parse each value in array
for(int j = 0; j NUM_ITEMS - 1; j++)
{
// Count number of passes made in array
numPasses++;
// Inner loop to compare values in array
for(int k = 0; k NUM_ITEMS - 1; k++)
{
/* Count number of comparisons made over array values */
numCompares++;
Swap the values to sort the values.
// Compare each value in array
if(num[k] num[k+1])
{
/* Count number of swaps performed over the array values */
numSwaps++;
/* Store greater value into a temporary value */
tmp = num[k+1];
// Store smaller value into a variable
num[k+1] = num[k];
/* Store the value in temporary variable into a variable */
num[k] = tmp;
}
}
}
Display the sorted items in array.
cout "Sorted values in array : " endl;
// Display the sorted value
for(int p = 0; p NUM_ITEMS; p++)
{
cout num[p] " ";
}
Display the count number of passes performed.
// Display number of passes count
cout "\n\nNumber of passes made over data: " numPasses endl;
Display the count number of comparions performed.
// Display number of comparisons count
cout "Number of comparisons made : " numCompares endl;
Display the count of number of swaps performed.
// Display number of swaps count
cout "Number of swaps performed : " numSwaps endl;
// Pause the screen
system("pause");
}
Sample output:
Screenshot of "sorting.exe" file
![There are totally 8 passes are made over the data in array; because the outer for loop in the code is used to pass each value in array. • The outer for loop is executed for 8 times (That is, 0 to 7). • The given code uses numPasses variable to monitor the number of passes count. At each iteration in the for loop, the numPasses variable is incremented by 1. • At end of iteration, the value of numPasses variable is ends with 8. Therefore, the total number of passes made over the data in the array is . Program code: Program used to demonstrate about sorting data using a bubble sort is as follows: // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in array numPasses++; // Inner loop to compare values in array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over array values */ numCompares++; Swap the values to sort the values. // Compare each value in array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in array. cout Sorted values in array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count of number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cb_a5f9_a881_5b117a38c1af_SM6534_00.jpg)
• The outer "for" loop is executed for "8" times (That is, "0" to "7").
• The given code uses "numPasses" variable to monitor the number of passes count. At each iteration in the "for" loop, the "numPasses" variable is incremented by "1".
• At end of iteration, the value of "numPasses" variable is ends with "8".
Therefore, the total number of passes made over the data in the array is "
![There are totally 8 passes are made over the data in array; because the outer for loop in the code is used to pass each value in array. • The outer for loop is executed for 8 times (That is, 0 to 7). • The given code uses numPasses variable to monitor the number of passes count. At each iteration in the for loop, the numPasses variable is incremented by 1. • At end of iteration, the value of numPasses variable is ends with 8. Therefore, the total number of passes made over the data in the array is . Program code: Program used to demonstrate about sorting data using a bubble sort is as follows: // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in array numPasses++; // Inner loop to compare values in array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over array values */ numCompares++; Swap the values to sort the values. // Compare each value in array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in array. cout Sorted values in array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count of number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cb_a5f8_a881_995154643ef2_SM6534_11.jpg)
".
Program code:
Program used to demonstrate about sorting data using a bubble sort is as follows:
// Include header files
#include
using namespace std;
Define a main function to sort set of values in an array.
// Function main
int main()
{
// Integer array with values
int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};
// Declare required variables
const int NUM_ITEMS = 9;
int tmp;
int numPasses = 0, numCompares = 0, numSwaps = 0;
Sort the values in an array.
// Outer loop to parse each value in array
for(int j = 0; j NUM_ITEMS - 1; j++)
{
// Count number of passes made in array
numPasses++;
// Inner loop to compare values in array
for(int k = 0; k NUM_ITEMS - 1; k++)
{
/* Count number of comparisons made over array values */
numCompares++;
Swap the values to sort the values.
// Compare each value in array
if(num[k] num[k+1])
{
/* Count number of swaps performed over the array values */
numSwaps++;
/* Store greater value into a temporary value */
tmp = num[k+1];
// Store smaller value into a variable
num[k+1] = num[k];
/* Store the value in temporary variable into a variable */
num[k] = tmp;
}
}
}
Display the sorted items in array.
cout "Sorted values in array : " endl;
// Display the sorted value
for(int p = 0; p NUM_ITEMS; p++)
{
cout num[p] " ";
}
Display the count number of passes performed.
// Display number of passes count
cout "\n\nNumber of passes made over data: " numPasses endl;
Display the count number of comparions performed.
// Display number of comparisons count
cout "Number of comparisons made : " numCompares endl;
Display the count of number of swaps performed.
// Display number of swaps count
cout "Number of swaps performed : " numSwaps endl;
// Pause the screen
system("pause");
}
Sample output:
Screenshot of "sorting.exe" file
![There are totally 8 passes are made over the data in array; because the outer for loop in the code is used to pass each value in array. • The outer for loop is executed for 8 times (That is, 0 to 7). • The given code uses numPasses variable to monitor the number of passes count. At each iteration in the for loop, the numPasses variable is incremented by 1. • At end of iteration, the value of numPasses variable is ends with 8. Therefore, the total number of passes made over the data in the array is . Program code: Program used to demonstrate about sorting data using a bubble sort is as follows: // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in array numPasses++; // Inner loop to compare values in array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over array values */ numCompares++; Swap the values to sort the values. // Compare each value in array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in array. cout Sorted values in array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count of number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cb_a5f9_a881_5b117a38c1af_SM6534_00.jpg)
2
Using a Bubble Sort
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
How many comparisons are made? Specify a number.
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
How many comparisons are made? Specify a number.
In the given code, there are totally "64" comparisons are made.
• This is because, the code uses two "for" loops to compare each value in the array.
• The outer "for" loop is executed for "8" times and the inner "for" loop is executed for "8" times.
• The comparisons are made inside the inner "for" loop, so it totally takes 64 comparisons (That is,
).
In the given code, the "numCompares" variable is used to monitor the number of comparisons count.
• At each iteration, the "numCompares" variable is incremented by "1"; at end of iteration, value of "numCompares" variable is "64".
Therefore, the total number of comparisons made over the array is
.
Program code:
The following program is used to demonstrate about sorting data using a bubble sort.
// Include header files
#include
using namespace std;
Define a main function to sort set of values in an array.
// Function main
int main()
{
// Integer array with values
int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};
// Declare required variables
const int NUM_ITEMS = 9;
int tmp;
int numPasses = 0, numCompares = 0, numSwaps = 0;
Sort the values in an array.
// Outer loop to parse each value in the array
for(int j = 0; j NUM_ITEMS - 1; j++)
{
// Count number of passes made in the array
numPasses++;
// Inner loop to compare values in the array
for(int k = 0; k NUM_ITEMS - 1; k++)
{
/* Count number of comparisons made over the array values */
numCompares++;
Swap the values to sort the values.
// Compare each value in the array
if(num[k] num[k+1])
{
/* Count number of swaps performed over the array values */
numSwaps++;
/* Store greater value into a temporary value */
tmp = num[k+1];
// Store smaller value into a variable
num[k+1] = num[k];
/* Store the value in temporary variable into a variable */
num[k] = tmp;
}
}
}
Display the sorted items in the array.
cout "Sorted values in the array : " endl;
// Display the sorted value
for(int p = 0; p NUM_ITEMS; p++)
{
cout num[p] " ";
}
Display the count number of passes performed.
// Display number of passes count
cout "\n\nNumber of passes made over data: " numPasses endl;
Display the count of number of comparions performed.
// Display number of comparisons count
cout "Number of comparisons made : " numCompares endl;
Display the count number of swaps performed.
// Display number of swaps count
cout "Number of swaps performed : " numSwaps endl;
// Pause the screen
system("pause");
}
Sample output:
Screenshot of "sorting.exe" file
![In the given code, there are totally 64 comparisons are made. • This is because, the code uses two for loops to compare each value in the array. • The outer for loop is executed for 8 times and the inner for loop is executed for 8 times. • The comparisons are made inside the inner for loop, so it totally takes 64 comparisons (That is, ). In the given code, the numCompares variable is used to monitor the number of comparisons count. • At each iteration, the numCompares variable is incremented by 1; at end of iteration, value of numCompares variable is 64. Therefore, the total number of comparisons made over the array is . Program code: The following program is used to demonstrate about sorting data using a bubble sort. // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in the array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in the array numPasses++; // Inner loop to compare values in the array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over the array values */ numCompares++; Swap the values to sort the values. // Compare each value in the array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in the array. cout Sorted values in the array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count of number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cc_423c_a881_b9f98c0c88a3_SM6534_00.jpg)
• This is because, the code uses two "for" loops to compare each value in the array.
• The outer "for" loop is executed for "8" times and the inner "for" loop is executed for "8" times.
• The comparisons are made inside the inner "for" loop, so it totally takes 64 comparisons (That is,
![In the given code, there are totally 64 comparisons are made. • This is because, the code uses two for loops to compare each value in the array. • The outer for loop is executed for 8 times and the inner for loop is executed for 8 times. • The comparisons are made inside the inner for loop, so it totally takes 64 comparisons (That is, ). In the given code, the numCompares variable is used to monitor the number of comparisons count. • At each iteration, the numCompares variable is incremented by 1; at end of iteration, value of numCompares variable is 64. Therefore, the total number of comparisons made over the array is . Program code: The following program is used to demonstrate about sorting data using a bubble sort. // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in the array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in the array numPasses++; // Inner loop to compare values in the array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over the array values */ numCompares++; Swap the values to sort the values. // Compare each value in the array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in the array. cout Sorted values in the array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count of number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cc_1b2a_a881_1d27c36e357d_SM6534_11.jpg)
).
In the given code, the "numCompares" variable is used to monitor the number of comparisons count.
• At each iteration, the "numCompares" variable is incremented by "1"; at end of iteration, value of "numCompares" variable is "64".
Therefore, the total number of comparisons made over the array is
![In the given code, there are totally 64 comparisons are made. • This is because, the code uses two for loops to compare each value in the array. • The outer for loop is executed for 8 times and the inner for loop is executed for 8 times. • The comparisons are made inside the inner for loop, so it totally takes 64 comparisons (That is, ). In the given code, the numCompares variable is used to monitor the number of comparisons count. • At each iteration, the numCompares variable is incremented by 1; at end of iteration, value of numCompares variable is 64. Therefore, the total number of comparisons made over the array is . Program code: The following program is used to demonstrate about sorting data using a bubble sort. // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in the array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in the array numPasses++; // Inner loop to compare values in the array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over the array values */ numCompares++; Swap the values to sort the values. // Compare each value in the array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in the array. cout Sorted values in the array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count of number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cc_423b_a881_655bef9019c1_SM6534_11.jpg)
.
Program code:
The following program is used to demonstrate about sorting data using a bubble sort.
// Include header files
#include
using namespace std;
Define a main function to sort set of values in an array.
// Function main
int main()
{
// Integer array with values
int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};
// Declare required variables
const int NUM_ITEMS = 9;
int tmp;
int numPasses = 0, numCompares = 0, numSwaps = 0;
Sort the values in an array.
// Outer loop to parse each value in the array
for(int j = 0; j NUM_ITEMS - 1; j++)
{
// Count number of passes made in the array
numPasses++;
// Inner loop to compare values in the array
for(int k = 0; k NUM_ITEMS - 1; k++)
{
/* Count number of comparisons made over the array values */
numCompares++;
Swap the values to sort the values.
// Compare each value in the array
if(num[k] num[k+1])
{
/* Count number of swaps performed over the array values */
numSwaps++;
/* Store greater value into a temporary value */
tmp = num[k+1];
// Store smaller value into a variable
num[k+1] = num[k];
/* Store the value in temporary variable into a variable */
num[k] = tmp;
}
}
}
Display the sorted items in the array.
cout "Sorted values in the array : " endl;
// Display the sorted value
for(int p = 0; p NUM_ITEMS; p++)
{
cout num[p] " ";
}
Display the count number of passes performed.
// Display number of passes count
cout "\n\nNumber of passes made over data: " numPasses endl;
Display the count of number of comparions performed.
// Display number of comparisons count
cout "Number of comparisons made : " numCompares endl;
Display the count number of swaps performed.
// Display number of swaps count
cout "Number of swaps performed : " numSwaps endl;
// Pause the screen
system("pause");
}
Sample output:
Screenshot of "sorting.exe" file
![In the given code, there are totally 64 comparisons are made. • This is because, the code uses two for loops to compare each value in the array. • The outer for loop is executed for 8 times and the inner for loop is executed for 8 times. • The comparisons are made inside the inner for loop, so it totally takes 64 comparisons (That is, ). In the given code, the numCompares variable is used to monitor the number of comparisons count. • At each iteration, the numCompares variable is incremented by 1; at end of iteration, value of numCompares variable is 64. Therefore, the total number of comparisons made over the array is . Program code: The following program is used to demonstrate about sorting data using a bubble sort. // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in the array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in the array numPasses++; // Inner loop to compare values in the array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over the array values */ numCompares++; Swap the values to sort the values. // Compare each value in the array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in the array. cout Sorted values in the array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count of number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cc_423c_a881_b9f98c0c88a3_SM6534_00.jpg)
3
Using a Bubble Sort
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
Do the variables named numPasses, numCompares, and numSwaps accurately keep track of the number of passes, compares, and swaps made in this bubble sort? Explain your answer.
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
Do the variables named numPasses, numCompares, and numSwaps accurately keep track of the number of passes, compares, and swaps made in this bubble sort? Explain your answer.
Values of variables:
, the given code accurately maintains the values of the variables "numPasses", "numCompares", and "numSwaps".
The variable "numPasses" is used count the number of passes occurred in the array.
• In the given code, the loop statement passes "8" values in array; as same as the variable "numPasses" results "8".
Hence, the value of variable is correct.
The variable "numCompares" is used to count number of comparisons made in the loop statement.
• In the given code, the inner "for" loop statement makes "64" comparisons in array; as same as the variable "numCompares" results "64".
Hence, the value of the variable is correct.
The variable "numSwaps" is used to count number of swaps occurred in the array.
In the given code, the conditional statement in inner "for" loop compares the values of the array and performs swap operation if the first value is greater than second value.
• Totally it performs "19" swap operations.
• As same as the variable "numSwaps" results "19".
Hence, the value of the variable is correct.
Program code:
Program to demonstrate about sorting data using a bubble sort is as follows:
// Include header files
#include
using namespace std;
Define a main function to sort set of values in an array.
// Function main
int main()
{
// Integer array with values
int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};
// Declare required variables
const int NUM_ITEMS = 9;
int tmp;
int numPasses = 0, numCompares = 0, numSwaps = 0;
Sort the values in an array.
// Outer loop to parse each value in the array
for(int j = 0; j NUM_ITEMS - 1; j++)
{
// Count number of passes made in the array
numPasses++;
// Inner loop to compare values in the array
for(int k = 0; k NUM_ITEMS - 1; k++)
{
/* Count number of comparisons made over the array values */
numCompares++;
Swap the values to sort the values.
// Compare each value in array
if(num[k] num[k+1])
{
/* Count number of swaps performed over the array values */
numSwaps++;
/* Store greater value into a temporary value */
tmp = num[k+1];
// Store smaller value into a variable
num[k+1] = num[k];
/* Store the value in temporary variable into a variable */
num[k] = tmp;
}
}
}
Display the sorted items in array.
cout "Sorted values in the array : " endl;
// Display the sorted value
for(int p = 0; p NUM_ITEMS; p++)
{
cout num[p] " ";
}
Display the count number of passes performed.
// Display number of passes count
cout "\n\nNumber of passes made over data: " numPasses endl;
Display the count number of comparions performed.
// Display number of comparisons count
cout "Number of comparisons made : " numCompares endl;
Display the count number of swaps performed.
// Display number of swaps count
cout "Number of swaps performed : " numSwaps endl;
// Pause the screen
system("pause");
}
Sample output:
Screenshot of "sorting.exe" file
![Values of variables: , the given code accurately maintains the values of the variables numPasses, numCompares, and numSwaps. The variable numPasses is used count the number of passes occurred in the array. • In the given code, the loop statement passes 8 values in array; as same as the variable numPasses results 8. Hence, the value of variable is correct. The variable numCompares is used to count number of comparisons made in the loop statement. • In the given code, the inner for loop statement makes 64 comparisons in array; as same as the variable numCompares results 64. Hence, the value of the variable is correct. The variable numSwaps is used to count number of swaps occurred in the array. In the given code, the conditional statement in inner for loop compares the values of the array and performs swap operation if the first value is greater than second value. • Totally it performs 19 swap operations. • As same as the variable numSwaps results 19. Hence, the value of the variable is correct. Program code: Program to demonstrate about sorting data using a bubble sort is as follows: // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in the array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in the array numPasses++; // Inner loop to compare values in the array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over the array values */ numCompares++; Swap the values to sort the values. // Compare each value in array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in array. cout Sorted values in the array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cc_b76e_a881_b394c777ac32_SM6534_00.jpg)
![Values of variables: , the given code accurately maintains the values of the variables numPasses, numCompares, and numSwaps. The variable numPasses is used count the number of passes occurred in the array. • In the given code, the loop statement passes 8 values in array; as same as the variable numPasses results 8. Hence, the value of variable is correct. The variable numCompares is used to count number of comparisons made in the loop statement. • In the given code, the inner for loop statement makes 64 comparisons in array; as same as the variable numCompares results 64. Hence, the value of the variable is correct. The variable numSwaps is used to count number of swaps occurred in the array. In the given code, the conditional statement in inner for loop compares the values of the array and performs swap operation if the first value is greater than second value. • Totally it performs 19 swap operations. • As same as the variable numSwaps results 19. Hence, the value of the variable is correct. Program code: Program to demonstrate about sorting data using a bubble sort is as follows: // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in the array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in the array numPasses++; // Inner loop to compare values in the array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over the array values */ numCompares++; Swap the values to sort the values. // Compare each value in array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in array. cout Sorted values in the array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cc_b76d_a881_add6448a2d67_SM6534_11.jpg)
, the given code accurately maintains the values of the variables "numPasses", "numCompares", and "numSwaps".
The variable "numPasses" is used count the number of passes occurred in the array.
• In the given code, the loop statement passes "8" values in array; as same as the variable "numPasses" results "8".
Hence, the value of variable is correct.
The variable "numCompares" is used to count number of comparisons made in the loop statement.
• In the given code, the inner "for" loop statement makes "64" comparisons in array; as same as the variable "numCompares" results "64".
Hence, the value of the variable is correct.
The variable "numSwaps" is used to count number of swaps occurred in the array.
In the given code, the conditional statement in inner "for" loop compares the values of the array and performs swap operation if the first value is greater than second value.
• Totally it performs "19" swap operations.
• As same as the variable "numSwaps" results "19".
Hence, the value of the variable is correct.
Program code:
Program to demonstrate about sorting data using a bubble sort is as follows:
// Include header files
#include
using namespace std;
Define a main function to sort set of values in an array.
// Function main
int main()
{
// Integer array with values
int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};
// Declare required variables
const int NUM_ITEMS = 9;
int tmp;
int numPasses = 0, numCompares = 0, numSwaps = 0;
Sort the values in an array.
// Outer loop to parse each value in the array
for(int j = 0; j NUM_ITEMS - 1; j++)
{
// Count number of passes made in the array
numPasses++;
// Inner loop to compare values in the array
for(int k = 0; k NUM_ITEMS - 1; k++)
{
/* Count number of comparisons made over the array values */
numCompares++;
Swap the values to sort the values.
// Compare each value in array
if(num[k] num[k+1])
{
/* Count number of swaps performed over the array values */
numSwaps++;
/* Store greater value into a temporary value */
tmp = num[k+1];
// Store smaller value into a variable
num[k+1] = num[k];
/* Store the value in temporary variable into a variable */
num[k] = tmp;
}
}
}
Display the sorted items in array.
cout "Sorted values in the array : " endl;
// Display the sorted value
for(int p = 0; p NUM_ITEMS; p++)
{
cout num[p] " ";
}
Display the count number of passes performed.
// Display number of passes count
cout "\n\nNumber of passes made over data: " numPasses endl;
Display the count number of comparions performed.
// Display number of comparisons count
cout "Number of comparisons made : " numCompares endl;
Display the count number of swaps performed.
// Display number of swaps count
cout "Number of swaps performed : " numSwaps endl;
// Pause the screen
system("pause");
}
Sample output:
Screenshot of "sorting.exe" file
![Values of variables: , the given code accurately maintains the values of the variables numPasses, numCompares, and numSwaps. The variable numPasses is used count the number of passes occurred in the array. • In the given code, the loop statement passes 8 values in array; as same as the variable numPasses results 8. Hence, the value of variable is correct. The variable numCompares is used to count number of comparisons made in the loop statement. • In the given code, the inner for loop statement makes 64 comparisons in array; as same as the variable numCompares results 64. Hence, the value of the variable is correct. The variable numSwaps is used to count number of swaps occurred in the array. In the given code, the conditional statement in inner for loop compares the values of the array and performs swap operation if the first value is greater than second value. • Totally it performs 19 swap operations. • As same as the variable numSwaps results 19. Hence, the value of the variable is correct. Program code: Program to demonstrate about sorting data using a bubble sort is as follows: // Include header files #include using namespace std; Define a main function to sort set of values in an array. // Function main int main() { // Integer array with values int num[] = {432, -5, 54, -10, 36, 9, 65, 21, 24}; // Declare required variables const int NUM_ITEMS = 9; int tmp; int numPasses = 0, numCompares = 0, numSwaps = 0; Sort the values in an array. // Outer loop to parse each value in the array for(int j = 0; j NUM_ITEMS - 1; j++) { // Count number of passes made in the array numPasses++; // Inner loop to compare values in the array for(int k = 0; k NUM_ITEMS - 1; k++) { /* Count number of comparisons made over the array values */ numCompares++; Swap the values to sort the values. // Compare each value in array if(num[k] num[k+1]) { /* Count number of swaps performed over the array values */ numSwaps++; /* Store greater value into a temporary value */ tmp = num[k+1]; // Store smaller value into a variable num[k+1] = num[k]; /* Store the value in temporary variable into a variable */ num[k] = tmp; } } } Display the sorted items in array. cout Sorted values in the array : endl; // Display the sorted value for(int p = 0; p NUM_ITEMS; p++) { cout num[p] ; } Display the count number of passes performed. // Display number of passes count cout \n\nNumber of passes made over data: numPasses endl; Display the count number of comparions performed. // Display number of comparisons count cout Number of comparisons made : numCompares endl; Display the count number of swaps performed. // Display number of swaps count cout Number of swaps performed : numSwaps endl; // Pause the screen system(pause); } Sample output: Screenshot of sorting.exe file](https://d2lvgg3v3hfg70.cloudfront.net/SM6534/11eb69f9_89cc_b76e_a881_b394c777ac32_SM6534_00.jpg)
4
Using Multidimensional Arrays
In this exercise, you use what you have learned about using multidimensional arrays to answer Questions.
A two-dimensional array declared as int myNums[6][2]; has how many rows?
In this exercise, you use what you have learned about using multidimensional arrays to answer Questions.
A two-dimensional array declared as int myNums[6][2]; has how many rows?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 8 في هذه المجموعة.
فتح الحزمة
k this deck
5
Using Multidimensional Arrays
In this exercise, you use what you have learned about using multidimensional arrays to answer Questions.
A two-dimensional array declared as int myNums[6][2]; has how many columns?
In this exercise, you use what you have learned about using multidimensional arrays to answer Questions.
A two-dimensional array declared as int myNums[6][2]; has how many columns?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 8 في هذه المجموعة.
فتح الحزمة
k this deck
6
Using Multidimensional Arrays
In this exercise, you use what you have learned about using multidimensional arrays to answer Questions.
Consider the following array declaration, int myNums[6][2]; Are the following C++ statements legal?
In this exercise, you use what you have learned about using multidimensional arrays to answer Questions.
Consider the following array declaration, int myNums[6][2]; Are the following C++ statements legal?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 8 في هذه المجموعة.
فتح الحزمة
k this deck
7
Swapping Values
In this exercise, you use what you have learned about swapping values to answer the following question.
Suppose you have declared and initialized two string variables, product1 and product2, in a C++ program. Now, you want to swap the values stored in product1 and product2 but only if the value of product1 is greater than the value of product2. Write the C++ code that accomplishes this task. The declarations are as follows:
string product1 = "hammer";string product2 = "wrench";
In this exercise, you use what you have learned about swapping values to answer the following question.
Suppose you have declared and initialized two string variables, product1 and product2, in a C++ program. Now, you want to swap the values stored in product1 and product2 but only if the value of product1 is greater than the value of product2. Write the C++ code that accomplishes this task. The declarations are as follows:
string product1 = "hammer";string product2 = "wrench";
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 8 في هذه المجموعة.
فتح الحزمة
k this deck
8
Using a Bubble Sort
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
Does this code perform an ascending sort or a descending sort? How do you know?
int numbers[] = {432, -5, 54, -10, 36, 9, 65, 21, 24};const int NUM_ITEMS = 9;int j, k, temp;int numPasses = 0, numCompares = 0, numSwaps = 0;for(j = 0; j numbers[k+1]){numSwaps++;temp = numbers[k+1];numbers[k+1] = numbers[k];numbers[k] = temp;}}}
Does this code perform an ascending sort or a descending sort? How do you know?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 8 في هذه المجموعة.
فتح الحزمة
k this deck