Hi !

I have arrray A[n][n] and i can input numbers in the interval -100 and 100 . I need to copy array A to array C[n*n] , and copy the number who are in a user iputed interval [k;l] and sort array C. I need help . I out put some big numbers and i dond know were i got it wrong.

int main(int argc, char *argv[])
{   
    int n , k , l ;
    int i , j , z = 0 ;
    int temp = 0;

    cout << "Enter number of row and columns for 2d array A[N;N]:" ;
    cin >> n ;

    int a[n][n];
    int c[n*n];

    cout << "Enter interval [K;L] for the new array C "<< endl ;
    cout << "K and L must be between -100 and 100"<< endl ;

    do{
         cout<<"K =";
         cin>>k;
    }    
    while( !( k >= -100 ) || !( k <= 100 ) );

      do{
         cout<<"L =";
         cin>>l;
    }    
    while( !( l > k ) || !( l <= 100 ) );

   cout << "Enter A[N;N]"<< endl ;

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

         for(j = 0; j < n; j++){

               do{
                   cout<<"A["<< i << "][" << j << "]:";
                   cin>>a[i][j];
               }   
               while( !( a[i][j]>= -100 ) || !( a[i][j] <= 100 ) );

         }

   }
  cout << "N is:" << n << endl;
  cout << "K is:" << k << endl;
  cout << "L is:" << l << endl << endl;
  cout << "Array A:" << endl;

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

         for(j = 0; j < n; j++){

                   cout << a[i][j]<< " ";

         }

         cout << endl;
   } 

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

             for(j = 0; j < n; ++j){

                   if(( a[i][j]>= k ) && ( a[i][j] <= l )){

                          c[z] = a[i][j] ;
                          z++;
                   }     


             }
   }



   for(i=0;i<(n*n);i++){

            for(j=i+1;j<(n*n);j++){

                          if(c[i]>c[j]){

                          temp=c[i];
                          c[i]=c[j];
                          c[j]=temp;

                          }

            }

   }


     cout << "Sorted array C:" << endl;

     for(i = 0; i < (n*n); i++){


                   cout << c[i] << " ";

         }



    cout << endl;





    system("PAUSE");
    return EXIT_SUCCESS;
}

Why not use std::sort for the range input? For example:

#include <iostream>
#include <algorithm>


int main () {
    int xs[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
    for (int i = 0; i < 10; ++i) std::cout << xs[i] << " ";
    std::cout << std::endl;
    std::sort (xs + 3, xs + 7);
    for (int i = 0; i < 10; ++i) {
       if (i == 3) std::cout << '[';
       std::cout << xs[i];
       if (i == 6) std::cout << ']';
       std::cout << " ";
    }
    std::cout << std::endl;
    return 0;
}

Running that gives:

1 3 5 7 9 2 4 6 8 0 
1 3 5 [2 4 7 9] 6 8 0

You would only need to do something like: std::sort (C+k, C+l)

 if(( a[i][j]>= k ) && ( a[i][j] <= l )){
    c[z] = a[i][j] ;
    z++;

you are only putting the values between k and l into the c[] array, which possibly (likely) leaves many elements of c[] still at their uninitialized state. If your display of c[] is limited by z, the count of items you copied to c[], you should see only valid data.

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.