Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What should I put to return so that the program returns the array's numbers in order

+1 vote
asked Apr 2, 2018 by SavageMango (310 points)
edited Apr 3, 2018 by SavageMango
#include <iostream>

using namespace std;

int boi(int arr[], int size){
 cout<<"How many numbers are in your array?"<< endl;
 cin >> size;
 for(int x=0;x < size;x++){
    cout<<"Enter a number in your array"<< endl;
    cin >> arr[x];
    if(arr[x]>= arr[x+1]);
        arr[x] = arr[x+1];
        arr[x+1] = arr[x];
 }
}
    

int main(){
 int A[5];
 boi(A, sizeof A);
 return ;

}

I also tried this

#include <iostream>

using namespace std;

int boi(int arr[], int size){
 cout<<"How many numbers are in your array?"<< endl;
 cin >> size;
 for(int x=0;x < size;x++){
    cout<<"Enter a number in your array"<< endl;
    cin >> arr[x];
    if(arr[x]>= arr[x+1]);
        arr[x] = arr[x+1];
        arr[x+1] = arr[x];
 }
}
    

int main(){
 int A[5];
 boi(A, sizeof A);{
 cout << A[0-sizeof A] << endl ;
 }
}

2 Answers

0 votes
answered Apr 3, 2018 by Yog
Give the example for required output.
0 votes
answered Apr 4, 2018 by AngelT18 (180 points)

Hi, you're trying to do the bubble sort, so let's check this:

 for(int x=0;x < size;x++){
    cout<<"Enter a number in your array"<< endl;
    cin >> arr[x];
    if(arr[x]>= arr[x+1]);
        arr[x] = arr[x+1];
        arr[x+1] = arr[x];
 }

you are reading the pos "x" before read the pos "x+1", you need first read all the array,

by the way, the if doesn't need ";"

 for(int x=0;x < size;x++){
    cout<<"Enter a number in your array"<< endl;
    cin >> arr[x];
 }

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

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

           aux=arr[i];

           arr[i]=arr[i+1];

           arr[i+1]=aux;         // You need an auxiliar to change two numbers, because                                                 // when you do arr[i]=arr[i+1], the information of arr[i] is lost

      }

}

and finally print the array

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

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

}

commented Apr 4, 2018 by SavageMango (310 points)
thank you I was wondering that I was doing something wrong
 that would just make every element equal each other
commented Apr 4, 2018 by SavageMango (310 points)
This is in C++ btw and they don't allow  aux... so is there another  keyword with the same purpose?
commented Apr 4, 2018 by AngelT18 (180 points)
Oh Im sorry,i forgot that, the aux is another int ( int aux;)
and this is my code if you want to see
#include <bits/stdc++.h>
using namespace std;
const int MaxN=1005;
int arr[MaxN];
int main(){
    int N,i,j,p,q;
    cin>>N;
    for(i=0;i<N;i++)cin>>arr[i];
    for(i=0;i<N;i++){
        for(j=0;j<N-1;j++){
            if(arr[j]>arr[j+1]){
                swap(arr[j],arr[j+1]);
            }
        }   
    }
    for(i=0;i<N;i++){
        cout<<arr[i]<<" ";
    }
}
by the way, the bubble sort is O(N^2) so we need to do 2 for's
https://en.wikipedia.org/wiki/Bubble_sort
commented Apr 4, 2018 by AngelT18 (180 points)
and sorry for my bad english, I speak spanish xD
commented Apr 5, 2018 by SavageMango (310 points)
What is the point of const int [MaxN]
and declaring the arr?
commented Apr 5, 2018 by AngelT18 (180 points)
edited Apr 5, 2018 by AngelT18
Usually the problem gives you the limits of the MaxN, and the limits for the bubble sort is 1000, because 1 second = 10^7 operations and 1000^2=10^6 operations, so that's why I declare the MaxN, the const is because i dont want to change it
and declare the array is for pre declare the memory
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...