Please help cant figure it out fully Write a C++ program that : 1. Allow the use
ID: 3870731 • Letter: P
Question
Please help cant figure it out fully
Write a C++ program that :
1. Allow the user to enter the size of the array such as N. N must
be an integer > 1 or 0 to quit the program .
2. Create a vector array of size N x N and populate it with random
number of 0’s and 1’s only. Use a random number generator to
generate the 0’s and 1’s.
3. Display the 2 d vector array.
4. Pass the vector array to a function. The purpose of the to do the
following :
a. Asks for two integer indices i and j (less or equal to n) or -1 .
b. Return the number of nonzero elements of the array that are
adjacent to the i-th and j-th element of the array
c. repeat steps a and b until -1 is entered to exit the function.
5. Repeat steps 1 – 4 until the user terminates the program by
entering n/N.
Validations:
• The size of the array must be an integer > 1 or 0.
Otherwise , error message should be displayed .
• i-th row and j-column must be integers between 0 and N or
-1 to terminate. Otherwise an error should be displayed
• The program must be running until the user decides to terminated it by
entering n or N. Otherwise an error should be displayed
Sample run:
You are given an n x n array whose elements are either 0 or 1.
Then program then will ask for two integer indices i and j
( less or equal to n or -1 ) and will return the number of nonzero
elements of the array that are adjacent to the i-th and j-th
element .
Enter the size of the array ( > 1 or 0 to quit ) : 3
0 1 0
0 0 0
1 0 1
Enter two integer indices i and j (between 0 and n ) or -1 to exit
-5 2
Invalid. Indices must be between 0 and n ) or -1 to exit
2 A
Invalid. Indices must be between 0 and n ) or -1 to exit
4 3
Invalid. Indices must be between 0 and n ) or -1 to exit
-1 X
Enter the size of the array ( > 1 or 0 to quit ) : 0
End of the program.
Explanation / Answer
#include<iostream>
#include<algorithm>
#include <vector>
#include<stdlib.h>
using namespace std;
void display(vector<vector<int> > &vec1,int n){
int i,j;
for (int i=0; i<vec1.size(); i++) {
for (int j=0; j<vec1[i].size() ;j++)
cout << vec1[i][j] << " ";
cout << endl;
}
}
void vec_fun(vector<vector<int> > &vec1,int n){
while(1){
int i,j;
cout<<" Enter two indices for i and j or -1 to quit:-";
cin>>i>>j;
if(i== -1)
break;
if(i>n || j>n || i<0 || j<0){
cout<<" Invalid. Indices must be between 0 and n or -1 to quit:-";
continue;
}
cout<<"Element adjacent to i and j is:-"<<vec1[i][j]<<endl;
}
}
int main(){
while(1){
int n,i,j,r;
char ch;
cout<<"Enter the size of the vector:- ";
cin>>n;
if(n==0)
break;
vector<vector<int> >vec(n, vector<int>(n,0));
r = rand()%2;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
vec[i][j] = r;
/* cout<<"Enter elements 0 or 1:= ";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>vec[i][j];*/
cout<<"1.Display ";
cout<<"2.Vector Functions ";
cout<<"N or n for exit ";
cout<<"Enter your choice ";
cin>>ch;
switch(ch){
case '1':display(vec,n);break;
case '2':vec_fun(vec,n);break;
case 'n':exit(0);
case 'N':exit(0);
default: cout<<"Wrong choice";
}
}
}
//you can also make user input (I commented user input part for now) as rand() function mostly generate 1 than 0.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.