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]) ?

4
  • 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)? Commented Sep 22, 2019 at 7:27
  • input1<=100 And its their format, it does compile on their system,,, Commented Sep 22, 2019 at 7:32
  • 1
    You have to create an instance of the structure and put the data into it, then return it. Commented Sep 22, 2019 at 7:53
  • stackoverflow.com/questions/9653072/… Commented Sep 22, 2019 at 7:58

3 Answers 3

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; } 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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.

1 Comment

This answer doesn't address the question asked, or how to make the code work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.