Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write two functions a. main function i. Declares a two dimensional array of i

ID: 3630464 • Letter: 1

Question


1) Write two functions
a. main function
i. Declares a two dimensional array of ints 4 * 4
ii. Ask the user to enter initialization value for each of the elements of the array
iii. Calls a function that swaps the contents of the 1st row with the last row
iv. Display the array
b. swap array
a. takes the first row and the last row of the array
b. swap the contents


2) Given the previous question (You can append this to the previous program or write a new one)
Ask the user for an input
Search the array to see if the user’s input is inside the two dimensional array
if it is then say found otherwise insert the user’s input in the first cell of the two dimensional array

Explanation / Answer

please rate - thanks

#include <iostream>
#include <iomanip>
using namespace std;
void input(int[][4]);
void print(int[][4]);
void swap(int[][4]);
int search(int[][4],int);
using namespace std;
int main()
{
int n,m;       
int a[4][4];
input(a);
cout<<"matrix before swap ";
print(a);
swap(a);
cout<<"matrix after swap ";
print(a);
cout<<"Enter a number to search for: ";
cin>>n;
m=search(a,n);
if(m<0)
   {a[0][0]=n;
    cout<<"matrix after unfound number inserted ";
    print(a);
    }
else
    cout<<n<<" was found in the array ";
system("pause");
return 0;
}
void print(int a[][4])
{int i,j;
for(i=0;i<4;i++)
{for(j=0;j<4;j++)
     cout<<setw(5)<<a[i][j]<<" ";
   cout<<endl;
}
cout<<endl;
}
int search(int a[][4],int n)
{int i,j;
for(i=0;i<4;i++)
     for(j=0;j<4;j++)
         if(a[i][j]==n)
              return 0;
return -1;
}   
void input(int a[][4])
{int i,j;
cout<<"Enter the matrix ";
for(i=0;i<4;i++)
   for(j=0;j<4;j++)
     {cout<<"Enter element ["<<i<<"]["<<j<<"]: ";
      cin>>a[i][j];
    }
}
void swap(int a[][4])
{int i,j,t;
for(i=0;i<4;i++)
    {t=a[0][i];
    a[0][i]=a[3][i];
    a[3][i]=t;
   }
}