Data Structure

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

ARBA MINCH

INSTITUTE OF TECHNOLOGY
UNIVERSITY

DEPARTMENT OF SOFTWARE ENGINEERING

DATA COMUNICATION AND NETWORKING ASSIGNMENT

Name: Mulatie Kindie


ID: NSR/1779/13
Section: B

Submitted to: Yilkal B.


Date: June 2023

DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT

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>

using namespace std;

void bubbleSort(int arr[], int n) {

for (int i = 0; i < n - 1; ++i) {

for (int j = 0; j < n - i - 1; ++j) {

if (arr[j] > arr[j + 1]) {

// Swap arr[j] and arr[j + 1]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

void printArray(int arr[], int n) {

for (int i = 0; i < n; ++i) {

cout << arr[i] << " ";

Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT

cout << endl;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter the elements of the array: ";

for (int i = 0; i < size; ++i) {

cin >> arr[i];

cout << "Array before sorting: ";

printArray(arr, size);

bubbleSort(arr, size);

cout << "Array after sorting: ";

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>

using namespace std;

void selectionSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT

// Swapping the current element with the minimum element

int temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter the elements of the array:\n";

for (int i = 0; i < size; i++) {

cin >> arr[i];

selectionSort(arr, size);

cout << "Sorted array: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

cout << endl;

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>

using namespace std;

void insertionSort(int arr[], int size) {

for (int i = 1; i < size; i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

Mulatie Kindie
DATA COMUNICATION AND NETWORKING INDIVIDUAL ASSIGNMENT

arr[j + 1] = key;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];

cout << "Enter the elements of the array: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

insertionSort(arr, size);

cout << "Sorted array: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

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

You might also like