+ 6
[ SOLVED ]how can we change to another array in a for loop?
how can we change to another array in a single for loop.. i have used some logic but it is showing an error what to do now?? this is just an example. suppose we are working on more than 5 or 6 arrays inside a single for loop than how can we skip to the next array help me (fast..) here is the code.. https://code.sololearn.com/cCmMutvGXucE/?ref=app
13 Answers
+ 5
you must code variable names somehow. other choice is 2 dimensional array.
string arr[2][2]={{"hii","hello"},{"bye","good night"}};
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
cout<<arr[i][j]<<endl;
outputting:
hii
hello
bye
good bye
+ 7
The real question is do the arrays have the same size? This type of array requires them to be indentical in length. Varying can be done with classes such as vector.
+ 5
AK-47 thnx for the help!!!
+ 4
John Wells sir i think your solution would do it....
AK-47 yep
if the elements of a are finished switch to b
+ 4
thank you very much sir....
+ 3
show an example of the output you expect. without knowing what you are looking for, it is impossible to help. All I know now is why your getting an error.
+ 3
in this case : i want to print all the elements within those arrays in a single for loop
in general how can i iterate over multiple arrays using a single for loop
edit: also plz tell me why i am getting the error
+ 3
If you do:
for(int i=0;i<2;i++)
cout << a[i] << " " << b[i] << " ";
you get:
hii bye hello good night
does that do what your looking for?
+ 3
no no...
what if i have more arrays...
i cant do that a[i]....b[i]....c[i] and so on
i want to iterate over a large no. of arrays (eg 8 different arrays)
how to make it work in a for loop..
+ 3
AK-47 no..
actually char(97+i)= a
when i=0
so i thought it would print a[j] and so on...
but it is not doing so...đ
+ 3
vector<string> v1 = {"a","b","c","d"};
vector<string> v2 = {"e","f"};
vector<string> v3 = {"g","h","i"};
vector<string> v4 = {"j"};
vector<string> vTotal;
vTotal.reserve( v1.size() + v2.size() + v3.size() + v4.size() );
vTotal.insert( vTotal.end(), v1.begin(), v1.end() );
vTotal.insert( vTotal.end(), v2.begin(), v2.end() );
vTotal.insert( vTotal.end(), v3.begin(), v3.end() );
vTotal.insert( vTotal.end(), v4.begin(), v4.end() );
for (const auto &i : vTotal)
cout << i << endl;
Output:
a
b
c
d
e
f
g
h
i
j
+ 2
At least, you forgot to put insertion operator and one of the array name (maybe two of em)
cout<<char(97+i)<< a[j]<<endl;
+ 2
Saurabh Tiwari
You wanna switch between n arrays, right?
What would be the condition for a switch for example from a to b?