c++ - Can we take input in array (a1,a2) in this way? -
suppose if having 2 integer array a1[2]
, a2[2]
, want take input
int a1[2],a2[2]; int i; cout<<"enter values in array a1\n"; for(i=0;i<2;i++) { cin>>a1[i]; // taking input in a1 separatly using loop } cout<<"enter values in array a2\n"; for(i=0;i<2;i++) { cin>>a2[i]; // taking input in a2 separatly using loop }
but can minimize using cin statements in code ..
for(j=1;j<3;j++) // loop taking input in a(j) array , value of j 1 first time input in array a1 { cout<<"enter values in array a"<<j<<endl; for(i=0;i<2;i++) { cin>>a(j)[i]; // can can take input using loop inside loop } }
i don't know should correct question title wants edit question title can do.
do mean following?
#include <iostream> #include <functional> int main() { const size_t n = 2; int a1[n]; int a2[n]; size_t j = 1; ( auto &r : { std::ref( a1 ), std::ref( a2 ) } ) { std::cout << "enter values in array a" << j++ << ": "; ( size_t = 0; < n; i++ ) { std::cin >> r.get()[i]; } } ( auto &r : { std::ref( a1 ), std::ref( a2 ) } ) { ( size_t = 0; < n; i++ ) std::cout << r.get()[i] << ' '; std::cout << std::endl; } }
if enter
1 2 3 4
then output like
enter values in array a1: 1 2 enter values in array a2: 3 4 1 2 3 4
using approach can use more 2 arrays. example
const size_t n = 2; int a1[n]; int a2[n]; int a3[n]; int a4[n]; size_t j = 1; ( auto &r : { std::ref( a1 ), std::ref( a2 ), std::ref( a3 ), std::ref( a4 ) } ) //...
Comments
Post a Comment