-2

for example, I have to take input in the format:

2 // no of test cases

7 // n->size of an array

9 7 5 9 2 20 3 //n array elements

5 // second test case array size

4 5 8 96 6 // second test case array elements

I don't know what to write in the main function.

void sort(int arr[],int n)
{
    for(int i=1;i<n;i++)
    {
        int current =arr[i];
        int j;
        for(j=i-1;j>=0;j--)
        {
            if(current<arr[j])
            {
              arr[j+1]=arr[j];
            }
            else
            {
                break;
            }
        }
        arr[j+1]=current;
    }
}

 int main(){
 // what should I write in the main func to test my problem for t no of times
 // I have to take all inputs first then print the sorted arrays  given by the user
 // separated by space and new line for the next sorted array
 }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

5 Answers5

1

I think this what you want.

const int N = 10;
int main(){
  int t, n, arr[N];
  cin >>t;
  for(int T;T<t;++T){
    cin >>n;
    for(int i=0;i<n;++i){
      cin>>arr[i];
    }
    sort(arr, n);

    for(int i=0;i<n;++i){
      cout <<arr[i]<<" ";
    }
    cout <<endl;
  }

Make sure to put the value if N is the maximum possible size of the array. Or you can declare the array as a dynamic array with the entered n (or use one of the STL like vector)

for example:

int main(){
  int t, n;
  cin >>t;
  for(int T;T<t;++T){
    cin >>n;
    int * arr = new int [n];
    for(int i=0;i<n;++i){
      cin>>arr[i];
    }
    sort(arr, n);

    for(int i=0;i<n;++i){
      cout <<arr[i]<<" ";
    }
    cout <<endl;
  }

Or by using Vector:

#include<vector>
using namespace std;
//your sort func here, but make sure to change it to have a vector instead of an array
int main(){
  int t, n;
  vector<int>arr;
  cin >>t;
  for(int T;T<t;++T){
    arr.clear();
    cin >>n;
    arr.resize(n);
    for(int i=0;i<n;++i){
      cin>>arr[i];
    }
    sort(arr, n);

    for(int i=0;i<n;++i){
      cout <<arr[i]<<" ";
    }
    cout <<endl;
  }

PS: I would recommend to see websites like codeforces, Hackerrank and others to practice on competitive programming question which contains a lot of question that will help (If you're interested)

Kasper
  • 588
  • 3
  • 16
0
int main()
{
    int t;         // number of test cases
    std::cin >> t; 
    while (t--)    // repeat t times
    {
        // what you had before
    }
}

jyl
  • 401
  • 3
  • 12
0

to test you can

  • define an input and a desired output, i.e an array "unsorted" and how it SHOULD look like after sorted

  • define a method that sorts input "a" and allows you to verify that by eather returning the sorted array or modifying the reference given as parameter.

  • defined a compare method that takes x1 and x2 as parameter and returns a bool indicating if the operation failed or no. x1 can be the desired sorted array and x2 the array returned from step1

  • loop in a for with DIFFERENT unsorted arrays and theis according sorted values...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

this is a code example for what I mean

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    int numOfTest = 0;
    std::vector<int>vec;
    std::cin>>numOfTest ;
    for(int j=; j<=numOfTest; ++j) // loop for num of tests
    {
        int n=0;
        for(int i=0; i<n; ++i) // loop for num of veriabels in one test
        {
            int var=0;
            std::cin >> var;
            vec.push_back(var);
        }
        // option one
        int* arr = new int[n] ;
        std::copy(vec.begin(),vec.end(),arr);
        sort(arr, n);
        //option two
        sort(&vec[0], n);
        //option three
        sort(vec.data(), n);
        for(int f=0; j<=n ++j)
        {
            std::cout << arr[f] << " "; // print the sorted array;
        } 
        delete [] arr;
    }
    return 0;
}

if your choosing option two or three you can using range base loop instead of for loop: it will look like this:

for(auto& e: vec)
{
    std::cout << e << " ";
}
yaodav
  • 1,126
  • 12
  • 34
  • 1
    what do you mean? – yaodav Jun 09 '20 at 10:45
  • Why do you dynamically allocate `arr` and use it instead of the vector? Not to mention that you forgot to `delete[]` it. You also don't need to include `` or `` (or include `` twice). – Some programmer dude Jun 09 '20 at 10:46
  • where should i call my function in this – Harsh Yadav Jun 09 '20 at 10:59
  • @Someprogrammerdude thanks for your comment on the includes it was a copy-paste from an old code of mine, also on the delete, I use dynamically allocate arr and not in the vector because the sort function signature gets arr and not a vector. – yaodav Jun 09 '20 at 11:28
  • @yaodav There are ways to get a pointer to the data as expected by the shown function, even from a vector. For example by using the [`data`](https://en.cppreference.com/w/cpp/container/vector/data) function. Of for older compilers, just use the address-of operator to get a pointer to the first element, as in `&vec[0]`. – Some programmer dude Jun 09 '20 at 11:33
  • @Someprogrammerdude thank you I didn't know about [data](https://en.cppreference.com/w/cpp/container/vector/data), also so send &vec[0] it a nice trick to know, I will edit my answer do add these options – yaodav Jun 09 '20 at 11:38
0
    int main()
        {
           int t;
          cin >> t;
          while (t--)
      {
          int size;

    cin >> size;
    int *arr = new int[size];

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

    sort(arr, size);

    for (int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }

    delete[] arr;
    cout << endl;
}

return 0;

}

I think this will work