Question

Q5-pointers: Using pointer notation and dynamic array to: input m*n array of zeros and ones then write the following function​​​​​​​in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

According to the given data, the following as the C++ code.

Code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

//method to fill the even parity of rows
void even_parity_row(int *newArr, int m, int n)
{
for (int i = 0; i < m; i++)
{
int rowParity = 0;
int j;
for (j = 0; j < n; j++)
{
rowParity = rowParity + *(newArr + i*n + j);
}
if(rowParity%2==0)
*(newArr + i*n + j) = 1;
else
*(newArr + i*n + j) = 0;
}
}

//method to fill the even parity of columns
void even_parity_col(int *newArr, int m, int n)
{
for (int j = 0; j < m; j++)
{
int colParity = 0;
int i;
for (i = 0; i < n; i++)
{
colParity = colParity + *(newArr + i*n + j);
}
if(colParity%2==0)
*(newArr + i*n + j) = 1;
else
*(newArr + i*n + j) = 0;
}
}

//method to display the new array on the computer screen
void print_arrray(int *newArr, int m, int n)
{
for(int i=0; i<m+1; i++)
{
for(int j=0; j<n+1; j++)
{
cout<<*(newArr + i*n + j)<<" ";
}
cout<<endl;
}
}

int main()
{
int m = 3, n = 3;
int* arr = new int[m*n];

//initialize array with 0 and 1
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
*(arr + i*n + j) = rand() % 2;
}
}
  
int* newArr = new int[(m+1)*(n+1)];
  
  
// here initialize new array
for (int i = 0; i < m; i++)
{
int rowParity = 0;
int j;
for (j = 0; j < n; j++)
{
rowParity = rowParity + *(arr + i*n + j);
*(newArr + i*n + j) = *(arr + i*n + j);
}
}
  
//method calling
even_parity_row(newArr, m, n);
even_parity_col(newArr, m, n);
print_arrray(newArr, m, n);

return 0;
}

OUTPUT:

1 0 1 1
1 1 1 0
0 0 1 0
1 0 0 0

I hope it will be helpful, Please give ThumsUp

Thank you!!

Add a comment
Know the answer?
Add Answer to:
​​​​​​​in C++ Q5-pointers: Using pointer notation and dynamic array to: input m*n array of zeros and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT