Answers for "c++ function that return array"

C++
2

c++ function return array

#include <iostream>
#include <ctime>

using namespace std;

// function to generate and retrun random numbers.
int * getRandom( ) {

   static int  r[10];

   // set the seed
   srand( (unsigned)time( NULL ) );
   
   for (int i = 0; i < 10; ++i) {
      r[i] = rand();
      cout << r[i] << endl;
   }

   return r;
}

// main function to call above defined function.
int main () {

   // a pointer to an int.
   int *p;

   p = getRandom();
   
   for ( int i = 0; i < 10; i++ ) {
      cout << "*(p + " << i << ") : ";
      cout << *(p + i) << endl;
   }

   return 0;
}
Posted by: Guest on May-08-2020
0

return array of string in function c++

string* getNames() {
 string* names = new string[3];
 names[0] = "Simon";
 names[1] = "Peter";
 names[2] = "Dave"; 

 return names;
}
Posted by: Guest on September-24-2020
-1

return function in cpp

void printChars(char c, int count) {
    for (int i=0; i<count; i++) {
       cout << c;
    }//end for
   
    return;  // Optional because it's a void function
}//end printChars
Posted by: Guest on June-27-2020
-1

return function in cpp

// Multiple return statements often increase complexity.
int max(int a, int b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}//end max
Posted by: Guest on June-27-2020

Code answers related to "c++ function that return array"

Browse Popular Code Answers by Language