hello guys ! i have created a program just to understand how to return a value from using a recursive program

what it does it thati have have two variable i.e first and last
the value of first is zero and last is 10

not i want the function to continue until last ==to first
if not it would simply return the value of the last and decrement here is the code and the prob listed and yes i am noob

#include<conio.h>
#include "stdafx.h"
#include <iostream>
# define size 10;
int mergesort(int,int);
using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	int first=0,last=10;
	
	//cout <<" enter the vlaues";
	
	//for(int i=0;i<10;i++)
	//cin>>arr[i];
	cout<<mergesort(first,last);
	system("pause");
	system("pause");
	return 0;
}

int mergesort(int first,int last,int mid)
{ int a=34343434;
   cout<<"mergesort called";
    if(first==last)
    {
                cout<<"its the end of the world";
				
				return a;
	}
  
if(first=!last)

{last--;
 cout<<"\n"<<last;
 mergesort(first,last,mid);
 return last;
}
return first;
}

here is the prob and plz tell me how to over come it

1>------ Build started: Project: merge sort, Configuration: Debug Win32 ------
1>Linking...
1>merge sort.obj : error LNK2019: unresolved external symbol "int __cdecl mergesort(int,int)" (?mergesort@@YAHHH@Z) referenced in function _wmain
1>C:\Documents and Settings\muqeet\My Documents\Visual Studio 2005\Projects\merge sort\Debug\merge sort.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\muqeet\My Documents\Visual Studio 2005\Projects\merge sort\merge sort\Debug\BuildLog.htm"
1>merge sort - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

i created this program in visual c++ 2005

Recommended Answers

All 2 Replies

Not sure what you want to do but this does something.
I don't have windows so I removed and replaced some things.
Many things going on here so take look and ask questions.

#include <iostream>
//# define size 10;

int mergesort(int,int);
using namespace std;

int main(int argc, char* argv[]) {
   int first=0,last=10;
   //cout <<" enter the vlaues";
   //for(int i=0;i<10;i++)
   //cin>>arr[i];
   cout << mergesort(first,last) << endl;
   return 0;
}

int mergesort(int first,int last)
{ 
   int a=34343434;
   cout<<"mergesort(" << first << "," << last << ")" << endl;
   if(first==last) {
        cout<<"its the end of the world" << endl;
        return a;
   }
  
    if(first!=last) {
       last--;
       mergesort(first,last);
       return last;
    }
    // Can't get here
    return first;
}

Output:

$ ./a.out
mergesort(0,10)
mergesort(0,9)
mergesort(0,8)
mergesort(0,7)
mergesort(0,6)
mergesort(0,5)
mergesort(0,4)
mergesort(0,3)
mergesort(0,2)
mergesort(0,1)
mergesort(0,0)
its the end of the world
9

In your declaration of mergesort() , you are giving it two arguments.

int mergesort(int,int);

But when you actually define the function, you gave it three.

int mergesort(int first,int last,int mid)

That is a mismatch, and it results in the linker error you ended up with. You fix this by changing either your declaration or the actual function so they both match.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.