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

I wrote a code in visual studios c++ that does the following: - Ask the user how

ID: 3727587 • Letter: I

Question

I wrote a code in visual studios c++ that does the following:

- Ask the user how many numbers they would like to enter (5-20).

- Check if the value they entered is within the range of 5-20. If not, ask them to enter a value between 5 and 20 until you receive a response in that range. Store this value in a variable named "N".

- When the user correctly enters N = the number of values (between 5-20), ask them for the first number. Tell them the number has to be between 1-100. Check if the number is between 1-100. If not, ask until they enter a number between 1-100.

- Repeat asking for new numbers and checking the validity of the numbers until the user enters the correct amount of numbers they specified in the beginning (N).

- Store these values in an array of size N. The name of the array should be "data".

- After the user finishes entering all the numbers, sort the numbers in ascending order using any exchange (swapping)-based sorting algorithm. Use a function named "sort" to accomplish the sorting, and within this function, call another function named "swap" to accomplish the swapping task. No part of the actual sorting or swapping should occur in the main function. You will need to pass the data array as a whole into the "sort" function. You will also need to pass two values that you are swapping to the "swap" function when needed.

- Print on the screen the numbers in ascending order.

code:

ConsoleApplication7 ConsoleApplication7.cpp 46 ConsoleApplication7consoleapplication7.cpp 46 ConsoleApplication7 consoleapplication7.cpp 49 ConsoleApplication7consoleapplica7cpp 53 ConsoleApplication7consoleapplication7.cpp 61 E0028 expression must have a constant value C2131 expression did not evaluate to a constant C3863 array type double [NJ is not assignable c4552 . >>": operator has no effect, expected operator with side-effect c4552 .

Explanation / Answer

The error you are getting is because of cin>>N. Actually N should be constant because you are creating a static array double data[N] and you should not change the N value. Make a constant N=20 and create another variable n for user input.

#include<iostream>
using namespace std;
void swap(double *xp, double *yp)
{
    double temp = *xp;
    *xp = *yp;
    *yp = temp;
}

void sort(double arr[], int n)
{
    int i, j, min_idx;


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

        min_idx = i;
        for (j = i+1; j < n; j++)
            if (arr[j] < arr[min_idx])
                min_idx = j;


        swap(&arr[min_idx], &arr[i]);
    }
}
int main()
{
    const int n=20;
    int N=1;
    while(N<5 || N>20)
    {
        cout<<"How many numbers they would like to enter (5-20):";
        cin>>N;
        if (N<5 || N>20)
        {
            cout<<"Please enter a value between 5 and 20 ";
        }
    }
    double data[n];
    for(int i=0; i<N; i++)
    {
        data[i]=-1;
        while (data[i]<1 || data[i]>100 )
        {
            cout<<"Enter number "<<i+1<<" between 1-100:";
            cin>>data[i];
        }

    }
    sort(data,N);
    cout<<"Sorted Array:";
    for(int i=0; i<N; i++)
    {
        cout<<data[i]<<" ";
    }
    return 0;
}


/*********************/output

How many numbers they would like to enter (5-20):1
Please enter a value between 5 and 20
How many numbers they would like to enter (5-20):21
Please enter a value between 5 and 20
How many numbers they would like to enter (5-20):7
Enter number 1 between 1-100:2
Enter number 2 between 1-100:45
Enter number 3 between 1-100:23
Enter number 4 between 1-100:78
Enter number 5 between 1-100:43
Enter number 6 between 1-100:13
Enter number 7 between 1-100:46
Sorted Array:2 13 23 43 45 46 78