Question

(C++ program) Can someone help me with this homework? Write a function named selectPositive that given...

(C++ program) Can someone help me with this homework?

Write a function named selectPositive that given a table of n x m integers, copies the elements that are greater than zero ( > 0 ) to a 1D array, column by column. For instance:

int table[MAX_ROW][MAX_COL] =
{
{10, -2, -3, -4, 0, 85},
{-5, -6, 0, 80, -5, 10},
{90, 11, -1, 31, -3, -4},
{41, 51, 6, 71, -7, 19}
};
int n = 4, m = 6;  
int list[MAX_ROW * MAX_COL];

selectPositive(table, n, m, list, k);

After the call list will contain:
{10, 90, 41, 11, 51, 6, 80, 31, 71, 85, 10, 19}

and k = 13, the number of elements copied to list.

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

Here is the answer..

CODE:

#include<iostream>
using namespace std;
#define MAX_ROW 4
#define MAX_COL 6
int t=0;
int* selectPositive(int table[MAX_ROW][MAX_COL],int n,int m,int list[],int k)
{
   int i,j;
   for(i=0;i<m;i++)
   {
       for(j=0;j<n;j++)
       {
           if(table[j][i]>0)
           {
              
               list[t++]=table[j][i];
               if(t>k)
                   break;
           }
          
       }
   }
  
   return list;
}
int main()
{
  
   int table[MAX_ROW][MAX_COL] = {{10,-2,-3,-4,0,85}, {-5,-6,0,80,-5,10}, {90,11,-1,31,-3,-4}, {41,51,6,71,-7,19}};
   int n = 4, m = 6;
   int k = 13;
   int list[MAX_ROW*MAX_COL];
   int i;
   selectPositive(table,n, m, list,k);
   for(i=0;i<t;i++)
   {
       cout<<list[i]<<"\t";
   }
}

OUTPUT:

If you have any doubts please COMMENT...

If you understand the answer please give THUMBS UP..

Add a comment
Know the answer?
Add Answer to:
(C++ program) Can someone help me with this homework? Write a function named selectPositive that given...
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
  • Write C++ Program The aim of this is to develop various functions on an array. First,...

    Write C++ Program The aim of this is to develop various functions on an array. First, create an array (A) with 10 integers, which will be randomly selected from the range of 0 to 100. Print the items of the array on screen as one line. Write a function to copy A to B. The function signature should be "void Copy(int A[], int B[], int size)". This function copies content of A into B in the same order. - Write...

  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • Write a Python program that tests the function main and the functions discussed in parts a...

    Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Question 14; Write a C function named isSymmetric, the prototype of which is given below, that...

    Question 14; Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters. (10 marks) Clue: Array myArray of size n is symmetric if myArray[0] is equal to myArray[n-1], myArray[1] is equal to myArray[n-2], and...

  • Please help me with this program.You are to write a C++ program that will read in...

    Please help me with this program.You are to write a C++ program that will read in up to 15 students with student information and grades. Your program will compute an average and print out certain reports. Format: The information for each student is on separate lines of input. The first data will be the student�s ID number, next line is the students name, next the students classification, and the last line are the 10 grades where the last grade is...

  • I need to develop the 7 functions below into the main program that's given on top...

    I need to develop the 7 functions below into the main program that's given on top Write a C program to create array, with random numbers and perform operations show below. Il Project 3 (53-20). 1 Projects CS5,00 This file contains the function Programcution Dagine and one include <timo // Defining some constants definer_SIZE 20 define LOVE LIMIT 1 eine UPR UNIT define TALSE eine Tut 1 wold randomizery (int[], int, Int, int) wold pinay in. St, Int); En find...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • == Programming Assignment == For this assignment you will write a program that controls a set...

    == Programming Assignment == For this assignment you will write a program that controls a set of rovers and sends them commands to navigate on the Martian surface where they take samples. Each rover performs several missions and each mission follows the same sequence: deploy, perform one or more moves and scans, then return to base and report the results. While on a mission each rover needs to remember the scan results, in the same order as they were taken,...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

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