Question

I cannot figure out how to get the function to work in separate source files in...

I cannot figure out how to get the function to work in separate source files in C++

Given that arrayIntValues [MAX_ROWS][MAX_COLUMNS] is a 2 dimensional array of positive integers, write a C++ function howManyEven to find the total number of even elements in the array. It should have input parameter array arrayIntValues. The function should return an integer. Also create a C++ function called printArray, with the input parameter array arrayIntValues, to print out the elements in the array (be sure to have row and column labels and proper formatting (i.e. column values line up).

Main must be in one source file and all other functions in a second source file.

The constants MAX_ROWS and MAX_COLUMNS must be accessed globally by defining the following global variables:const int MAX_ROWS = 3;or#define MAX_ROWS 3const int MAX_COLUMNS = 2;or#define MAX_COLUMNS 2The array arrayIntValues is initialized by the following statement within main():int arrayIntValues [MAX_ROWS][ MAX_COLUMNS] = { {3 , 2}, {4, 5}, {2, 2} };

Print out the array and the count of positive even numbers (label appropriately) in function printArray.

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

Hey There!

The solution is pretty easy.

Step 1: Create a folder, inside create a file as "ARRAYFUNCTIONS.h".

Code for the file :

ARRAYFUNCTIONS.h

#include<iostream>

#ifndef ARRAYFUNCTIONS

#define ARRAYFUNCTIONS

#define MAX_COLUMNS 2

#define MAX_ROWS 3

int howManyEven(int arrIntValues[][MAX_COLUMNS]){

    int count = 0,i,j;

    for(i=0;i<MAX_ROWS;i++){

        for(j=0;j<MAX_COLUMNS;j++){

            if(arrIntValues[i][j] % 2 == 0){

                count += 1;

            }

        }

    }

    return count;

}

void printArray(int arrIntValues[][MAX_COLUMNS]){

    int i,j;

    std::cout<<"The 2-D array is :"<<std::endl;

    for(i=0;i<MAX_ROWS;i++){

        for(j=0;j<MAX_COLUMNS;j++){

            std::cout<<arrIntValues[i][j]<<" ";

        }

        std::cout<<std::endl;

    }

}

#endif

Step 2: In the same folder, add another file named as "mainFunction.cpp".

Code:

mainFunction.cpp

#include "ARRAYFUNCTIONS.h"

using namespace std;

int main(){

    int arrayIntValues [MAX_ROWS][ MAX_COLUMNS] = { {3 , 2}, {4, 5}, {2, 2} };

    printArray(arrayIntValues);

    cout<<"Even number is the array : "<<howManyEven(arrayIntValues)<<endl;

}

Step 3: Save these files.

Step 4: Compile the header file with the command.

Step 5: Compile the main file with the command.

Step 6: Run the generated file.

The Output is something like this:

Comment for more help.

Do give a thumbs up, if you liked the answer.

Add a comment
Know the answer?
Add Answer to:
I cannot figure out how to get the function to work in separate source files in...
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
  • in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call...

    in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed. Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Netflix into the array. Call a function called getAverage to calculate and display the average of the hours entered. Call a function called...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • Below is my code and the instructions for the code. I can't figure out what's wrong....

    Below is my code and the instructions for the code. I can't figure out what's wrong. Please help. Tasks This lab has two parts: Writing a searching function for 1D arrays. Writing a simple class. Part 1 – Searching Continuing with arrays, we will now expect you to find information in an array and derive useful properties from the information stored in an array. Start Eclipse. Change the workspace directory location to something "safe". You may want to temporarily choose...

  • Create qz5.c to include all of the following function prototypes: void check1(char *, char, int *);...

    Create qz5.c to include all of the following function prototypes: void check1(char *, char, int *); void check2(char *, char, int *); void display(char, int); Then, implement main() to perform the tasks below: Define a 10-element char array with initial values of any lower case letters of your selection. Values can duplicate. Define a pointer that points to the above array. Print the array completely with double spaces before each character. See screenshot below for a sample. Call check1() with...

  • Create a program that creates an array of 500 random numbers between -10 and 10. Write...

    Create a program that creates an array of 500 random numbers between -10 and 10. Write several functions: Function 1 - prints the array with a certain number of elements per line. Prototype: void printArray(int array[], int numElements, int numPerLine, ostream &os) Function 2 prints only the array elements that are evenly divisible by an input number. Prototype: void printDivBy(int array[], int numElements, int numPerLine, int divBy, ostream &os) Function 3 takes the input array and returns the maximum, the...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • please complete the missing function only to figure out how many numbers fall within the range...

    please complete the missing function only to figure out how many numbers fall within the range of 90 through 99 total of 29 values in C 6 finclude "lab5.h" 8 const char *FILENAME() - / array of the data file names * ("lab5a.dat", "lab5b.dat", NULL); 12 int main(void) 13 int file count = 0; keeps track of which file we are on/ int check overflow - 0; / counter to prevent array overflow int real filesize = 0; /actual count...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

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