[RESOLVED] code generation
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: [RESOLVED] code generation

  1. #1
    Join Date
    May 2017
    Posts
    173

    [RESOLVED] code generation

    hello there I need help with my code plz
    Last edited by david16; July 8th, 2017 at 07:47 AM.

  2. #2
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    So basically I have a function called int x ( I was trying a for loop ) that I need to pass to a void function to do some task can you give me suggestions so I can do it thx a lot for your help
    Last edited by david16; July 9th, 2017 at 08:34 AM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,714

    Re: code generation

    To seed rand you can use
    Code:
    srand((unsigned int)time(NULL));
    once at the start of the program.

    and to generate a random lower-case letter you could use
    Code:
    char rnd = (rand() % 26) + 'a';
    If you need more advice, you'll need to more specific.

    Cheers!
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  4. #4
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    ok I will try that and when I need to pass to a function if I got stuck somewhere I'll come back thx

  5. #5
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    I initialized main and then created a function to generate int x but I want to be able to call it to a multiple
    to generate that why I thought it should be better to put it in a function like this

    Code:
    int x random_f () {
    
       int rnd = (rand() % 26) + 4 ;
       
           return rnd;
    }
    I was trying this in main but I guess I have some issue

    [CODE] const int a = 45 , b = 7;

    int x1 [a][b];

    random_f ( x1 )

    what do you think ? any suggestions ? thx
    Last edited by david16; July 9th, 2017 at 08:34 AM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,714

    Re: code generation

    The function random_letter() doesn't take any arguments, but you are passing it an array.

    I don't understand where the array comes in? Do you want each element of the array to contain a random letter??
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  7. #7
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    yes I wanted the function char random_ letter to be called when needed to a 2d array first one I have is arr1 to generate random letter in that array but I guess I did something wrong any suggestions on that ? thx for ur help

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,714

    Re: code generation

    I think we have a slight language issue here as I guess English isn't your first language, but I still don't understand what is wanted? If this is an exercise, it would be helpful if you posted the spec.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  9. #9
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    sir sorry for that let me try to explain again . I want to create a function called int x that will accept function . I need that function to be called to that one . these functions can be different size you see what I mean now ? thx
    Last edited by david16; July 9th, 2017 at 08:36 AM.

  10. #10
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    I'm sorry for not being clear . if you want I can tell you what is the whole topic about then we will see that later ?? .. when I come back hopefully cause /I have something importsnt to do thx
    Last edited by david16; July 8th, 2017 at 09:59 AM.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,714

    Re: code generation

    a function called random_letter() that will accept 2d arrays
    This is a void function that does that
    Code:
    template<size_t N, size_t M>
    void random_letter(char (&array)[N][M])
    {
    	cout << N << endl << M << endl;
    }
    used from main() as
    Code:
    int main()
    {
    	char arr1[5][7];
    
    	random_letter(arr1);
    
    	return 0;
    }
    In this case, the function will display 5 & 7
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  12. #12
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    thx I will have a look on that but was is template<size_t N, size_t M> for ?? you called N and M in the function why you added this ??
    Last edited by david16; July 8th, 2017 at 04:58 PM.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,714

    Re: code generation

    you called N and M in the function why you added this
    Just as a test display of the passed array sizes.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,714

    Re: code generation

    Another way of doing this could be
    Code:
    const size_t N = 5, M = 7;
    
    using myArray = char[N][M];
    
    void random_letter(myArray arr)
    {
    
    }
    
    int main()
    {
    	myArray array1;
    
    	random_letter(array1);
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  15. #15
    Join Date
    May 2017
    Posts
    173

    Re: code generation

    that can be helpful I see . But I have a problem since in my program there is switch case and in each case I have a 2d array initialized with different size . SO is there a way to be able to define the 2d array in main and call the function random_letter to fill that 2d array with random letters ?? what I mean is to call different 2d array with different size to the same function but not same time you see ? thx for your help

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)