Search code examples
c++arraysstdvector

Vector of Pairs of integers


I'm asked to get a value N and I will then get N pairs of values. These pairs will be the size of my 2D array, and this 2D array's elements will range from 1 to the size of the 2D array.

Sample input:

N = 2

Two pairs of values are

(2,3)
(3,4)

Sample output:

{(1,2,3),(4,5,6)}
{(1,2,3,4),(5,6,7,8),(9,10,11,12)}

The below code is what I have tried. I'm still a beginner so kindly help me.

#include<bits/stdc++.h>
using namespace std;

makeArray(int a, int b){
   int arr[a][b];
   int temp = 1;
   cout<<"[";
   for(int i=1; i<=a; i++){
       cout<<"[";
       for(int j=1; j<=b; j++){
           cout<<temp<<",";
       }
       cout<<"]";
   }
   cout<<"]";
}

int main() {
   int n;
   cin>>n;
   int a,b;
   vector<pair<int,int>> ip;
   for(int i=0; i<n; i++){
       cin>>a;
       cin>>b;
       ip.push_back(make_pair(a,b));
   }
   for (int x=0; x<n; x++)
   {
       makeArray(ip.first, ip.second);
       cout<<endl;
   }
   return 0;
}

Solution

  • I did not quite get what the task is, but I fixed few errors in the code, so that it runs and satisfies the sample you've provided.

    #include<bits/stdc++.h>
    using namespace std;
    
    void makeArray(int a, int b){
       //int arr[a][b];
       int temp = 0;
       cout<<"[";
       for(int i=1; i<=a; i++){
           cout<<"[";
           if (1 <= b)
               cout << ++temp;
           for(int j=2; j<=b; j++){
               cout << "," << ++temp;
           }
           cout<<"]";
       }
       cout<<"]";
    }
    
    int main() {
       int n;
       cin>>n;
       int a,b;
       vector<pair<int,int>> ip;
       for(int i=0; i<n; i++){
           cin>>a;
           cin>>b;
           ip.push_back(make_pair(a,b));
       }
       for (int x=0; x<n; x++)
       {
           makeArray(ip[x].first, ip[x].second);
           cout<<endl;
       }
       return 0;
    }
    

    To be more specific, in the main() function, second for loop:

       for (int x=0; x<n; x++)
       {
           //makeArray(ip.first, ip.second);
           makeArray(ip[x].first, ip[x].second);
           cout<<endl;
       }
    

    You forgot to use the brackets operator [] to access the elements of the vector.

    In the makeArray function:

    1. arr variable is not used.

    2. Forgot the return type (void).

    3. You do not increment temp variable, so you end up printing 1 instead of a range form 1 to a * b

    4. You have a trailing comma after printing the last integer

    Fix:

    // return type is required
    void makeArray(int a, int b){ 
       //int arr[a][b]; variable not used.
       //temp is later incremented, so the first
       //element printed will be 1.
       int temp = 0;
       
       cout<<"[";
       for(int i=1; i<=a; i++){
           cout<<"[";
           //We do not know, if we only need 1
           //integer printed, so to not have a trailing
           //comma at the end, we print the first integer
           //outside the array
           if (1 <= b)
                   cout << ++temp;
           //because the first integer is already printed,
           //we start from the second
           for(int j=2/*1*/; j<=b; j++){
               //cout<<temp<<",";
               cout << "," << ++temp;
           }   
           cout<<"]";
       }   
       cout<<"]";
    }
    

    Input:

    2
    2 3 3 4
    

    Output:

    [[1,2,3][4,5,6]]
    [[1,2,3,4][5,6,7,8][9,10,11,12]]