Data Structure
Data Structure
INSTITUTE OF TECHNOLOGY
UNIVERSITY
Here's the modified C++ program code that uses the `using namespace` directive and prompts
the user for the array size and elements:
```cpp
#include <iostream>
arr[j + 1] = temp;
Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT
int main() {
int size;
int arr[size];
printArray(arr, size);
bubbleSort(arr, size);
printArray(arr, size);
return 0;
```
Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT
In this code, the `using namespace std;` directive is added at the beginning to avoid the need for
explicit namespace qualification for standard library functions.
The main function prompts the user for the size of the array and the elements of the array using
`cin`. It then calls the `bubbleSort` function to sort the array and `printArray` function to display
the sorted array.
When you run this program, it will ask for the size of the array and the elements, sort the array
using the bubble sort method, and then display the sorted array.
Here's an example of a C++ program code that sorts an integer array using the Selection Sort
method, taking input for the array size and elements from the user:
```cpp
#include <iostream>
int minIndex = i;
minIndex = j;
Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT
arr[i] = arr[minIndex];
arr[minIndex] = temp;
int main() {
int size;
int arr[size];
selectionSort(arr, size);
Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT
return 0;
```
In this code, the `selectionSort` function implements the Selection Sort algorithm. The `main`
function takes input for the array size and elements from the user, calls the `selectionSort`
function to sort the array, and then displays the sorted array.
Sure! Here's an example of a C++ program that uses the insertion sort algorithm to sort an
integer array. The program prompts the user for the array size and array elements, and then sorts
the array in ascending order.
```cpp
#include <iostream>
int j = i - 1;
arr[j + 1] = arr[j];
j--;
Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT
arr[j + 1] = key;
int main() {
int size;
int arr[size];
insertionSort(arr, size);
return 0;
Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT
```
In this code, the `insertionSort` function implements the insertion sort algorithm. It iterates
through the array, starting from the second element (`i = 1`), and compares each element with the
previous elements. If an element is smaller, it shifts the previous elements to the right to make
space for the current element and inserts it in the correct position.
The `main` function prompts the user for the array size and elements, calls the `insertionSort`
function to sort the array, and then prints the sorted array.
Please note that using `using namespace std;` is generally not recommended in header files or at
the global scope of a program, as it can lead to naming conflicts. It's better to use the `std::`
prefix when referencing standard library elements, as shown in the example.
Mulatie Kindie