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
  • solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function...

    solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function for a dynamic array which returns a new dynamic array that is a subset of the original. (15pt) input parameters: array - (the array and any related parameters) start - index of the first element end - index of the last element This function returns a new dynamic array containing the elements from the start thru the end indices of the original array. All...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as...

    Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its size to pointers; depending on...

  • IN C PROGRAMMING, NOT C++ Rewrite the program you submitted for A8 to use pointers, pointer...

    IN C PROGRAMMING, NOT C++ Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its...

  • 1. Write a C++ Program to display address of elements of an array using both array...

    1. Write a C++ Program to display address of elements of an array using both array and pointers 2. Write a C++ Program to insert and display data entered by using pointer notation. 3. Write a C++ Program to access Array Elements Using Pointer

  • Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array...

    Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array to a function, named Summation. In main: Define array, pass array, print out the array and the result returned from Summation on screen. In function Summation: 1)take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4)if the value of f(i) is negative, add -3*f(i) to...

  • Write a C++ program that will test function described below that use pointers and dynamic memory...

    Write a C++ program that will test function described below that use pointers and dynamic memory allocation. Functions: You will write the function described below. Then you will call them from the main function, to demonstrate their correctness. concat_array: takes two int arrays and the arrays' sizes as arguments (that's 4 arguments). It should create a new array big enough to store both arrays. Then it should copy the contents of the first array to the new array, and then...

  • Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory...

    Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to Co Sci 839 or google: "binary...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

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