Thread: Dynamic Array called by function, help needed

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    7

    Dynamic Array called by function, help needed

    I am having trouble creating a code that ask the user to input the size of an array and that array will display random characters of the alphabet. Before I added the function it worked perfectly but after I get couple of errors because I am kind of new when it comes to dynamic functions and arrays. I added pics of it.
    Dynamic Array called by function, help needed-1-jpg
    Dynamic Array called by function, help needed-2-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Pasting code between [code][/code] tags is much better than pictures.

    Try
    Code:
    void showArray ( int *array, int N ) {
      // something
    }
    Make your prototype and call in main match.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    We are in the year 2017 now. There is no more need to use dynamic arrays, we have strings and vectors and....
    Have a look at this demo using modern C++.
    Code:
    #include <iostream>
    #include <string>
    
    std::string randomString(const size_t size)
    {
      const std::string chars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    
      std::string retval(size, ' ');
    
      for (size_t i = 0; i < size; i++)
      {
        retval[i] = static_cast<char>(chars[rand() % chars.size()]);
      }
      return retval;
    }
    
    int main()
    {
      std::cout << "Enter the number of characters the string should contain: ";
      size_t size;
      std::cin >> size;
      std::string s = randomString(size);
      std::cout << "Your string: " << s << "\n\n";
      return 0;
    }
    Enter the number of characters the string should contain: 17
    Your string: PhQGHuMEAyLnLFDxF

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
      for (size_t i = 0; i < size; i++)
      {
        retval[i] = static_cast<char>(chars[rand() % chars.size()]);
      }
    Why the unnecessary static_cast?

    And why not just use a ranged based loop, if you want to show "modern C++"?

    Code:
        for(auto& itr : retval)
            itr = chars[rand() % chars.size()];

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Yes , the static_cast was not necessary. I just hacked it together in 2-3 minutes without much thinking.

    I didn't use the ranged based loop because the for loop is easier to understand for a beginner.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    7
    Thanks and I used the dynamic array because my assignment told me to use it.

  7. #7
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Is it working now?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic 2D array to function
    By MaynardJ222 in forum C Programming
    Replies: 7
    Last Post: 07-15-2013, 11:48 AM
  2. skeletal 'dynamic array of strings' function - tweak needed
    By fundamental in forum C Programming
    Replies: 2
    Last Post: 04-18-2012, 02:15 PM
  3. more array / function help needed
    By l1ttledb in forum C Programming
    Replies: 5
    Last Post: 12-28-2010, 08:32 PM
  4. Passing a dynamic array to a function
    By esmeco in forum C Programming
    Replies: 15
    Last Post: 06-05-2010, 04:25 PM
  5. some major pointers/dynamic array help needed
    By kv2 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2004, 11:32 PM

Tags for this Thread