1

I was giving a company's coding interview test (on mettl.com) and this was the problem : -

Given an array of "n" integers, add "2" to every element of the array and return the array.

And this was their format of code (I cannot change their format, I can just write code inside the function. Also, I don't have to read the input, it is passed through function already and also no "main-function" is allowed).

Here is what the code looked like in C++:

#include<bits/stdc++.h>
using namespace std;
//Read only region starts, you cannot change the code here
//Assume following return types when writing code for this question

struct Result{
    Result() : output(){};
    int output1[100];
};
Result arrange(int input1, int input2[])
{
    //Read only region end...now...you can write whatever you want 
    int n;
    n=input1;
    int i=0;
    int a[n];
    while(i<n)
    {
        a[i]=input2[i]+2;
        i++;
    }

//...now..I am super confused...how do I return the array 'a' to result structure??
//I have very less idea about structures and objects in C++

}

My answer is in array - 'a' but I don't know how do I return it to the structure (output1[100]) ?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Shalini Tomar
  • 55
  • 1
  • 7
  • Well straight away their code doesn't compile (`Result` has no member `output`, only `output1` unless that was a transcribing error). Do you know how to copy an arrays elements into another array? Is your array of n integers the `output1[100]` or `input1`? Is `input1` assumed to be less than 100? Why not just _make_ a `Result` (instead of `a`)? – Tas Sep 22 '19 at 07:27
  • input1<=100 And its their format, it does compile on their system,,, – Shalini Tomar Sep 22 '19 at 07:32
  • 1
    You have to create an instance of the structure and put the data into it, then return it. – Marichyasana Sep 22 '19 at 07:53
  • https://stackoverflow.com/questions/9653072/return-a-struct-from-a-function-in-c – Marichyasana Sep 22 '19 at 07:58

3 Answers3

2

The function is declared to return a Result struct. So the function needs to create an instance of that struct in order to return it. And since the struct already has an array in it, you don't need to create your own array, just fill in the one that already exists.

Try this:

#include <bits/stdc++.h>
using namespace std;
//Read only region starts, you cannot change the code here
//Assume following return types when writing code for this question

struct Result{
    Result() : output1(){};
    int output1[100];
};
Result arrange(int input1, int input2[])
{
    //Read only region end...now...you can write whatever you want

    Result res;
    for(int i = 0; i < input1 && i < 100; ++i)
    {
        res.output1[i] = input2[i] + 2;
    }

    return res;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

To just answer the question, make a struct object in the function (“Result R;”) and use it’s member output1 array to copy into instead of array “a” (“R.output1[i] = ...;”). So just delete “a” array and replace with the struct object’s output1. Then return that struct object.

xv8
  • 183
  • 2
  • 11
-1

Structure can be passed to function through its object therefore passing structure to function or passing structure object to function is same thing because structure object represents the structure. Like normal variable, structure variable(structure object) can be pass by value or by references / addresses.