Write a C/C++ program that prompts the user to enter N integer values and stores it in an array. Sort the elements of the array in ascending order. You may use any sorting algorithm.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a C/C++ program that prompts the user to enter N integer values and stores it in an array. Sort the elements of the array in ascending order. You may use any sorting algorithm.

"HAL CMPG121 2018\Praktiese\Practical 6\Memo\Deel 3\bin\Debug\Deel 3.exe"
How many elements should be stored in the array?
Enter value for element at index 0:
10
65
Enter value for element at index 1:
68
Enter value for element at index 2:
64
Enter value for element at index 3:
Enter value for element at index 4:
25
89
Enter value for element at index 5:
45
Enter value for element at index 6:
10
Enter value for element at index 7:
22
Enter value for element at index 8:
35
Enter value for element at index 9:
Sorted array:
Process returned e (exe)
Press any key to continue.
32
10 22 25 32 35 45 64 65 68 89
execution time : 29.266 s
Transcribed Image Text:"HAL CMPG121 2018\Praktiese\Practical 6\Memo\Deel 3\bin\Debug\Deel 3.exe" How many elements should be stored in the array? Enter value for element at index 0: 10 65 Enter value for element at index 1: 68 Enter value for element at index 2: 64 Enter value for element at index 3: Enter value for element at index 4: 25 89 Enter value for element at index 5: 45 Enter value for element at index 6: 10 Enter value for element at index 7: 22 Enter value for element at index 8: 35 Enter value for element at index 9: Sorted array: Process returned e (exe) Press any key to continue. 32 10 22 25 32 35 45 64 65 68 89 execution time : 29.266 s
Expert Solution
Step 1: Program

A required program in C++ language is as follows,

#include<iostream>

using namespace std;

void swap(int *a, int *b) 

    int tempV = *a; 

    *a = *b; 

    *b = tempV; 

//Define a function to sort the array elements in ascending order

void sortAsc(int arry[], int N) 

    //Declare necessary variables

    int ind, jind, min; 

    for (ind = 0; ind < N-1; ind++) 

    { 

        // Discover the minimum element in unsorted array 

        min = ind;

        for (jind = ind+1; jind < N; jind++) 

        if (arry[jind] < arry[min]) 

            min = jind; 

        /* Call the method to swap the minimum element

        found with the first element*/

        swap(&arry[min], &arry[ind]); 

    } 

// Define main() function

int main() 

    int N,ind;

    cout<<"How many elements should be sorted in the array?\t";

    //Get the number of elements to be stored in an array

    cin>>N;

    int arry[N];

    for(ind=0;ind<N;ind++)

    {

        cout<<"Enter value for element at index "<<ind<<":\t";

        //Get the values from the user

        cin>>arry[ind];

    }

    sortAsc(arry, N); 

    cout << "Sorted array:    "; 

    //Print the sorted array

    for (ind=0; ind < N; ind++) 

        cout << arry[ind] << " "; 

    return 0; 

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Randomized Select Algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education