so. . . i guess you could say i'm kinda new here but. . . this site really helps me alot this past few days and i decided to join...

i was surfing all over the internet and i can't find what's the problem with my program.

this always says i need to input something inside the "[]" but i'm passing it to a function bubble sort . .. . i can't explain it well . . . i tried using quicksort but the same problem occurs. . . . can someone help me? here's the code.

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int swap(int array[],int i,int j){
     int temp=array[i];
     array[i]=array[j];
     array[j]=temp;
     }

int bubblesort(int array[],int size){
    int i=0;
    int swapped=1;
    while(swapped=1){
                        swapped=2;
                        i=0;
                        while(i<size-2){
                                        if (array[i]>array[i+1]){
                                                                 swap(array,i,i+1);
                                                                 swapped=1;
                                                                 }
                                                                 i++;
                                        }
                        }
}
          
int main() 
{
    int num;
    int index;
    index=0;
    int i;
    cout<<"Please enter a number."<<endl;
    cin>>num;
    num=0;
    int size;
    size=30;
    int array[size];
    srand((unsigned)time(0)); 
    for(int i=0; i<size; i++){ 
        array[i] = (rand()%30)+1;
        } 
 i=0;
 cout<<"Your random numbers are "<<endl;

 while(i!=size){
             while(i!=size){
             cout<<array[i]<<" ";
             i++;
             }             
               }
               cout<<endl;
               
bubblesort(array[],size);


i=size-1;
while(num<array[i]){
                    i--;
                    }
                    cout<<"the number lesser than "<<num;
                    cout<<"is "<<array[i];


    system("pause");
}

the program should get a number from the user, generate 30random numbers, and then on those numbers , the program should tell the user that "this is the next lower number"

sample .. . user enters 23. . . . the program generates 24, 12, 30, 17, 21 ,6

the next lower number is 21... that's why i should sort it... but the problem is just. . . i have no idea how tofix that single problem ... . . help will be appreciated thanks. .

>int array;
This won't work in standard C++. Array sizes must be compile time constants, and your size variable is not. If it compiles, you're relying on a compiler extension which isn't good when learning the language.

>bubblesort(array[],size);
The brackets aren't necessary when referring to the array object itself:

bubblesort(array, size);

Also, swap and bubblesort don't return a value but are defined to return int. And your bubblesort has a bug that will cause an infinite loop. You used = where == was intended.

bubblesort(array[],size);

to bubblesort(array,size);

ok. . . so i've changed some of those things and came up with this.

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

void swap(int array[],int i,int j){
     int temp=array[i];
     array[i]=array[j];
     array[j]=temp;
     }

void bubblesort(int array[],int size){
    int i=0;
    bool swapped;
    swapped=true;
    while(swapped=true){
                        swapped=false;
                        i=0;
                        while(i<size-2){
                                        if (array[i]>array[i+1]){
                                                                 swap(array,i,i+1);
                                                                 swapped=true;
                                                                 }
                                                                 i++;
                                        }
                        }
}
          
int main() 
{
    int num;
    int index;
    index=0;
    int i;
    cout<<"Please enter a number."<<endl;
    cin>>num;
    num=0;
    const int size=30;
    int array[size];
    srand((unsigned)time(0)); 
    for(int i=0; i<size; i++){ 
        array[i] = (rand()%30)+1;
        } 
 i=0;
 cout<<"Your random numbers are "<<endl;

 while(i!=size){
             while(i!=size){
             cout<<array[i]<<" ";
             i++;
             }             
               }
               cout<<endl;
               
bubblesort(array,size);


i=size-1;
while(num<array[i]){
                    i--;
                    }
                    cout<<"the number lesser than "<<num;
                    cout<<"is "<<array[i];


    system("pause");
}

i changed the parts you've mentioned and i changed the swap thing to boolean to be a little more "readable" . . . after the random numbers. . . the other half doesn't show anything . and also i don't understand what you mean by
>" You used = where == was intended."

I'm so sorry for being such a noob. and thanks again for responding

the other half doesn't show anything

That's because you're caught in an infinite loop. :icon_rolleyes:

and also i don't understand what you mean by
>" You used = where == was intended."

>while(swapped=true){
should be

while(swapped==true){

thanks :) problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.