wrong output

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1590 Views - Last Post: 04 December 2012 - 04:16 PM Rate Topic: -----

#16 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: wrong output

Posted 03 December 2012 - 04:18 PM

View Postshadowevil, on 03 December 2012 - 11:58 PM, said:

And I agree... IF you want to use this on other OS's more measures are to be taken to achieve that... But I don't program for Linux, or MAC OS or such other Operating Systems.


If by "more measures" you mean "simply don't call ZeroMemory, you're right. Otherwise you're full of crap.

There is absolutely no need to zero the arrays here and if there were this could be accomplished much more easily by simply initializing them to {0}.
Was This Post Helpful? 1
  • +
  • -

#17 d00n  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 18
  • Joined: 03-December 12

Re: wrong output

Posted 04 December 2012 - 10:28 AM

#include <iostream>
using namespace std;

	void swapArray(int array1[], int array2[], int i,int size);
	void swapArray(int array1[], int array2[], int i,int size )
	{
	    int temp;
	    if (i<size)
	    {temp = array1[i];               // Set temp to array1[i]
	    array1[i] = array2[i];          // Set array1[i] to array2[i]
	    array2[i] = temp;               // Set array2[i] to temp
        swapArray (array1,array2,i,size);
        }
	else
        cout<<"Not valid"<<endl;
	}

	int main()
	{

        const int size=5;
        int array1[size];
        int array2[size];
	    int element1=0,element2=0;

	    for (int i=0;i<size;i++)  // to take the 5 elements from the user
	    {
	        cout<<"please Enter Element of The first array"<<endl;
	        cin>>element1;
            cout<<"please Enter Element of The second array"<<endl;
            cin>>element2;
                 array1[i] = element1;      //then save them in the array to send them to the function and swap them
                 array2[i] = element2;
        swapArray(array1,array2,i,size);  //function call
                	    }

	   /* for(int i=0;i<size;i++)              // Display the arrays
	    {
	        cout<<"\n\n Array 1 elements is:"<<array1[i]<<endl;
	        cout <<"\n Array 2 elements is:"<<array2[i]<<endl;
	    }*/

	    return 0;
	}




in this code i having error says that windows can check online for a solution to the problem
also if i replace the recursive with loop in the function !!!
but without either of them (loop or recursion) its working ?? well i need it in both ways
Was This Post Helpful? 0
  • +
  • -

#18 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: wrong output

Posted 04 December 2012 - 04:16 PM

That's infinite recursion. You keep calling swapArray recursively with the same arguments over and over again. When writing a recursive function, at least one of the arguments should be different when calling the function recursively. There should also be a branch, which does not recurse and also does not produce an error message, i.e. a branch that represents that you're done.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2