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]) ?
Resulthas no memberoutput, onlyoutput1unless that was a transcribing error). Do you know how to copy an arrays elements into another array? Is your array of n integers theoutput1[100]orinput1? Isinput1assumed to be less than 100? Why not just make aResult(instead ofa)?