In: Computer Science
I want to copy all the elements in f1() to f2() but in reverse order (C++)
this is my code:
#include <iostream>
#include <iomanip>
using namespace std;
const int R=10;
const int C=10;
int Array[R][C] ;
/////////////////////////////////////
void f1(){
for(int i=0 ; i<R ; i++){
for(int j=0 ; j<C ; j++){
Array[i][j]= (rand () %100) + 1;
}
}
for(int i=0 ; i<R ; i++){
for(int j=0 ; j<C ; j++){
cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Array[i][j]<<"
";
}
cout<<endl;
}
cout<<"\n\n\n";
}
void f2(){
cout<<"////////////////////////////////////"<<endl;
cout<<"\n";
const int R2=10;
const int C2=10;
int Array[R][C];
for (int i=0 ; R<R2 ;i++){
for ( int j=0 ; C<C2 ; j++){
int swaping = Array[R][C];
Array[R][C]=Array [R2][C2];
Array [R2][C2]=swaping;
Array[R][C]++;
Array [R2][C2]--;
cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Array[i][j]<<"
";
}
}
cout<<"\n\n";
}
int main()
{
f1();
f2();
return 0;
}
Program
#include <iostream>
#include <iomanip>
using namespace std;
const int R=10;
const int C=10;
int Array[R][C] ;
void f1(){
for(int i=0 ; i<R ; i++){
for(int j=0 ; j<C ; j++){
Array[i][j]= (rand () %100) + 1;
}
}
for(int i=0 ; i<R ; i++){
for(int j=0 ; j<C ; j++){
cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Array[i][j]<<"
";
}
cout<<endl;
}
cout<<"\n\n\n";
}
void f2(){
cout<<"////////////////////////////////////"<<endl;
cout<<"\n";
const int R2=10;
const int C2=10;
int Arraycopy[R][C];
int k=R2-1; //last index
int m=C2-1; //last index
for (int i=0 ; i<R2 ;i++){
m=C2-1;
for ( int j=0 ; j<C2 ; j++){
Arraycopy[i][j]=Array[k][m];
cout<<"["<<i<<"]["<<j<<"]="<<setw(3)<<Arraycopy[i][j]<<"
";
m--;
}
k--;
cout<<endl;
}
cout<<"\n\n";
}
int main()
{
f1();
f2();
return 0;
}
Screenshot of the program
Output
If you find this answer useful, please rate positive , thankyou