0

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sriram S
  • 13
  • 1
  • 4
    You've forgotten to explain what's wrong with the code you've written. – cigien Sep 25 '21 at 18:49
  • 1
    See [Why should I not #include ?](https://stackoverflow.com/Questions/31816095/) and [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/) – Remy Lebeau Sep 25 '21 at 19:16
  • `makeArray(int a, int b)` is an invalid function declaration. It must have a type, like `void` – Ted Lyngmo Sep 25 '21 at 19:30

1 Answers1

2

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]]
DiKetarogg
  • 170
  • 8
  • Please add a text description of what code you've changed, and why that solves the OP's problem. – cigien Sep 25 '21 at 19:29