user1527227
user1527227

Reputation: 2258

Returning Array from C++ Function

I'm teaching myself C++ and doing a problem from a textbook. So far I have covered the basics such as data types, declarations, displays, assignment, interactive input, selection (if-else), and repetition (for/while loops...), functions and arrays. I have NOT done anything with pointers, but I know what they are...

I came across this problem:

The answers to a true-false test are as follows: T T F F T. Given a two-dimensional answer array, in which each row corresponds to the answers provided on one test, write a function that accepts the two-dimensional array and number of tests as parameters and returns a one-dimensional array containing the grades for each test. (Each question is worth 5 points so that the maximum possible grade is 25.) Test your function with the following data:

enter image description here

My understanding is that C++ functions cannot return arrays--At least this is what I read on other posts on this forum. Is this correct? If so, how are they expecting you to do this problems because I haven't covered pointers yet. The only other way I thought MIGHT be possible is by passing in the array by reference.... but the question stem only says there are 2 arguments to the function so I thought maybe that method was ruled out. That method would require a third argument which is the array your modifying so its implicitly returned.

I have some code, but its not correct (only my calcgrade function needs work) and I'm not sure how to move forward.Could someone please advise? Thank you!!

#include<iostream>

// Globals
const int NROW = 6, NCOL = 5;
bool answers[NCOL] = {1, 1, 0, 0, 1};
bool tests[][NCOL] = {1, 0, 1, 1, 1,
                      1, 1, 1, 1, 1,
                      1, 1, 0, 0, 1,
                      0, 1, 0, 0, 0,
                      0, 0, 0, 0, 0,
                      1, 1, 0, 1, 0};
int grade[NROW] = {0};

// Function Proto-Types
void display1(bool []);
void display2(bool [][NCOL]);
int calcgrade(bool [][NCOL], int NROW);


int main()
{


    calcgrade(tests, NROW);
    display2(tests);

    return 0;
}

// Prints a 1D array
void display1(bool answers[])
{
    // Display array of NCOL
    for(int i = 0; i < NCOL; i++)
        std::cout << std::boolalpha << answers[i] << std::endl;
    return;
}

// Print 2d Array
void display2(bool answers[][NCOL])
{
    // Display matrix:  6x5
    for(int i = 0; i < NROW; i++)
    {
        for(int j= 0; j < NCOL; j++)
        {
            std::cout << std::boolalpha << answers[i][j] << std::endl;
        }
        std::cout << std::endl;
    }

    return;
}

int calcgrade(bool tests[][NCOL], int NROW)
{

    for(int i = 0; i < NROW; i++)
    {
        for(int j = 0; j < NROW; j++)
        {
            if(tests[i][j]==answers[j])
                grade[i] += 5;
        }
        printf("grade[%i] = %i", i, grade[i]);
    }

    return grade;
}

Upvotes: 2

Views: 1231

Answers (4)

Steve Henningsgard
Steve Henningsgard

Reputation: 56

Another way of doing this would be to create the Answer array in your main function, then pass it along with the T/F array to your grading function. Then, your grading function could operate on the Answer array, and wouldn't even need to return anything.

Essentially, when you pass arrays in functions, you're actually passing pointers to the arrays, and thus you can operate on them as if they were passed by reference (which they were).

semi-pseudocode ex:

void getGrades( const int answerVector, int gradeVector ) {
    // code that computes grades and stores them in gradeVector
}

Upvotes: 0

Paweł Stawarz
Paweł Stawarz

Reputation: 4012

If you're passing the number of tests as the second parameter, it means you actually know the number of tests, so you don't need to use a vector. You can return an dynamically allocated array (either using new or malloc).

The code would look like this:

int* calcgrade(bool tests[][NCOL], int NROW){
  int* array = new int[NROW];
  for(int i=0;i<NROW;i++)
    array[i] = calculatedGrade;
  return array;
}

Upvotes: 1

Scony
Scony

Reputation: 4138

Try to use std::vector.

Vectors are sequence containers representing arrays that can change in size.

You can do so:

vector<bool> function()
{
  vector<bool> vec;

  vec.push_back(true);
  vec.push_back(false);
  vec.push_back(true);

  return vec;
}

Upvotes: 3

silmeth
silmeth

Reputation: 700

You can:

  • as you said, return pointer to dynamically allocated array,
  • you can make structure type with word struct that includes static array, read more on C++ Reference and return/take as argument structure of your type.

Upvotes: 0

Related Questions