code mulfunction
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: code mulfunction

  1. #1
    Join Date
    May 2017
    Posts
    173

    code mulfunction

    hello guys . So I was working on a code where I should accept numbers from user and store threm only if there unique in that array . so the prob is that I'm getting a wrong input . This is my code below :

    Code:
    #include <iostream>
    using namespace std;
    
    void addToArray ( int array1[], int x )
    {
    
    	int max = 0;
    
    	bool duplicate = false; //assume its false and prove the opposite if any.
    
    	
    	for ( int i = 0; i <= max; ++i ) //no number stored ( no duplicates yet ).
    	{
    		if ( array1[i] == x )
    		{
    			duplicate = true;
    			break;
    		}
    	}
    
    	
    	if ( !duplicate ) //from here we only add the number if unique.
    	{
    		array1[max] = x;
    		++max;
    	}
    }
    
    const int Max = 20; //max allowed is 20 ( const use )
    
    int main () 
    
    {
    	int arr [Max] = {};
    	int n;
    
    	for (int i = 0; i < Max; ++i) {
    
    		cout << "\nEnter value between 10 and 100 (" << i + 1 << "/20) :\n" << endl;
    		cin >> n;
    
    		if (n >= 10 && n <= 100)
    
    			addToArray(arr, n);
    
    		else {
    			
    			cout << "\nWrong input!\n " << endl;
    			i--;
    		}
    	}  
    	
    		for ( int i = 0 ; i< Max ; ++i ) 
    	{
    		if ( arr[i] != 0)
    			
    		cout << arr[i] << endl;
    
      }
    	cout << "\n";
    	system("pause");
    	return 0;
    }
    thx for you help

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

    Re: code mulfunction

    Code:
    int max = 0;
    Every time the function is called, max is set to 0. So
    Code:
    array1[max] = x;
    ++max;
    Always sets element 0! max is incremented but when the function exits the value of max is 'lost' when goes out of scope. Next time the function is called max is set back to 0 again!

    Have you covered static variables yet? (Hint!).
    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)

  3. #3
    Join Date
    May 2017
    Posts
    173

    Re: code mulfunction

    no did cover static yet . is there a way to do it without that ?

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

    Re: code mulfunction

    max could be global (but global variables are not considered good practice). Another way would be to have max as a function parameter and return max from the function.
    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)

  5. #5
    Join Date
    May 2017
    Posts
    173

    Re: code mulfunction

    Can you help me do it with function ?? could be way easier to understand I think

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

    Re: code mulfunction

    The function would be used like this
    Code:
    int max = 0;
    ...
    max = addToArray(arr, n, max);
    so addToArray() takes current max as the argument and returns the new value of max. The function needs to be changed accordingly.
    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 mulfunction

    Code:
    int Max = 0;  // put it out of main 
    
    int main () 
    
    {
    	int arr [Max] = {}; // I have now prob here because it only accept constant value should I leave it empty or something ?
    	int n;
    
    	for (int i = 0; i < Max; ++i) {
    
    		cout << "\nEnter value between 10 and 100 (" << i + 1 << "/20) :\n" << endl;
    		cin >> n;
    
    		if (n >= 10 && n <= 100)
    
    			 addToArray(arr, n, Max);

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

    Re: code mulfunction

    You're confusing Max (const variable) with max (variable)! These are completely different variables.

    If you're going to use a global variable, then you don't need to pass it as a parameter to the function. From the code in post #1, you only need
    Code:
    int max = 0;
    
    void addToArray ( int array1[], int x )
    {
    
    	//int max = 0;
    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 mulfunction

    no I got confused . I just lost the sense of it . I'm looking at post #1 original code but I don;t know how to change it . I will try it on visual studio

  10. #10
    Join Date
    May 2017
    Posts
    173

    Re: code mulfunction

    I have 2 variables max and const int Max . I have trouble passing to the function since I can't pass it from main its not going to work . something is wrong with this can you show me what you were trying to do thx
    Last edited by david16; July 7th, 2017 at 01:12 PM.

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

    Re: code mulfunction

    Using a parameter with addToArray(), consider
    Code:
    #include <iostream>
    using namespace std;
    
    int addToArray(int array1[], int x, int max)
    {
    	bool duplicate = false; //assume its false and prove the opposite if any.
    
    	for (int i = 0;  (i <= max) && !(duplicate = (array1[i] == x)); ++i); //no number stored ( no duplicates yet ).
    
    	if (!duplicate)	//from here we only add the number if unique.
    		array1[max++] = x;
    
    	return max;
    }
    
    const int Max = 20; //max allowed is 20 ( const use )
    
    int main()
    {
    	int arr[Max] = {0};
    	int n, mx = 0;
    
    	for (int i = 0; i < Max; ++i) {
    		cout << "\nEnter value between 10 and 100 (" << i + 1 << "/" << Max << ") :\n" << endl;
    		cin >> n;
    
    		if (n >= 10 && n <= 100)
    			mx = addToArray(arr, n, mx);
    		else {
    			cout << "\nWrong input!\n " << endl;
    			--i;
    		}
    	}
    
    	for (int i = 0; i < Max; ++i)
    		if (arr[i])
    			cout << arr[i] << endl;
    
    	cout << "\n";
    	system("pause");
    	return 0;
    }
    Last edited by 2kaud; July 7th, 2017 at 02:50 PM.
    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 mulfunction

    oh so I had to add it in main and pass it to the function by value and do the rest I see . thx

  13. #13
    Join Date
    Feb 2017
    Posts
    147

    Re: code mulfunction

    Quote Originally Posted by david16 View Post
    So I was working on a code where I should accept numbers from user and store threm only if there unique in that array
    The standard solution to this problem is to store the numbers in an std::set or std::unordered_set. It works because these containers don't store duplicates. You can always copy the set to an array afterwards when all numbers have been entered.

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

    Re: code mulfunction

    Quote Originally Posted by david16 View Post
    oh so I had to add it in main and pass it to the function by value and do the rest I see . thx
    You could also pass it to the function by reference then you wouldn't need to return the new value.
    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,711

    Re: code mulfunction

    Quote Originally Posted by wolle View Post
    The standard solution to this problem is to store the numbers in an std::set or std::unordered_set. It works because these containers don't store duplicates. You can always copy the set to an array afterwards when all numbers have been entered.
    Yes, but I suspect he hasn't covered these yet in his lessons.
    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)

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)