Given a two-digit number n, print both the digits of the number.
Input Format: The first line indicating the number of test cases T.
Next T lines will each contain a single number ni.
Output Format: T lines each containing two digits of the number ni separated by space.
Constraints
1 <= T <= 100000 10 <= ni <= 99
Error: Runtime Error (SIGSEGV)
I'm not able to pinpoint, where the problem is in the code as it is working fine for a two numbers while it gives the runtime error for 4 or more numbers. Is there another way of doing this problem other than using for loop twice?
#include <bits/stdc .h>
using namespace std;
int main()
{
int t;
int arr[t];
cin>>t;
for(int i=0;i<t;i )
{
cin>>arr[i];
}
int c;
int b;
for(int i=0;i<t;i )
{
c=(arr[i]/10);
if(c!=0)
{
b=arr[i]%(c*10);
}
else
{
b=arr[i];
}
cout<<c<<" "<<b<<endl;
}
return 0;
}
CodePudding user response:
Fist, you declare t
, but do not initialize it, so it is uninitialized. Trying to use the value leads to undefined behavior.
Second, VLA is not valid C , see here. You have to use std::vector
instead.
Third, you don't need to use an int
.
So, you should do:
#include <iostream>
#include <vector>
#include <string>
int main()
{
int t{};
std::cin >> t;
std::vector<std::string> arr(t);
for(int i = 0; i < t; i )
{
std::cin >> arr[i];
}
for(const auto &i : arr)
{
std::cout << i[0] << ' ' << i[1] << '\n';
}
}