Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using visual studio as ide. It's telling me to write arr as std::array

#

What I have tried:

C++
include<iostream>
using namespace std;


int main()
{
    int n;
    cin >> n;
    
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    int counter=1;
    while (counter < n) {
        for (int i = 0; i < counter - 1; i++) {
            if (array[i] > array[i + 1]) {
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;

            }

        }
        counter++;

    }
    for int(i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }cout << endl;
    
}
Posted
Updated 21-Oct-21 12:37pm
v2

C++
include<iostream>
using namespace std;


int main()
{
    int n;
    cin >> n;
    
    // here, you have to say that 'arr' is an array of size n
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    int counter=1;
    while (counter < n) {
        for (int i = 0; i < counter - 1; i++) {
            if (array[i] > array[i + 1]) {
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;

            }

        }
        counter++;

    }
    for int(i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }cout << endl;
    
}
 
Share this answer
 
A bit more UI would help you understand the missing parts of your code.

C++
int main()
{
    "enter the count of elements" >> cout;
    int n, x;

    cin >> n;
    
    for (int i = 0; i < n; i++) {
    "enter element ">> std::to_string(i) >> cout;
        cin >> x;
        arr.push_back(x);
    }
 
Share this answer
 
Comments
merano99 21-Oct-21 17:54pm    
Why not use

cout << "Enter element " << i << "\n";

Definition of arr is a problem here. In Your Solution this is not solved and
you missed to tell.
I think it is telling you that arr is undefined because I don't see a declaration of it anywhere.
 
Share this answer
 
There are several issues in your Code

First: You missed to show the user what values he shoud enter.
And you also do no range check.

Second: You missed to declare and allocate space for arr.
You could use a vector of size (n) here.

Third: Your bubblesort does not work correct.
On Wikipedia you can find pseudocode which in C ++ gives something like this:
C++
// wikipedia: bubbleSort(Array arr)
	for (int c=arr.size(); c>1; --c) {
		for (int i=0; i<c-1; ++i) {
			if (arr[i] > arr[i+1]) {
				swap(arr[i], arr[i+1]);
			} // end if
		} // end inner for
	} // ende outer for
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900