Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the error of this mergesort C programming?

What I have tried:

C++
#include<stdio.h>
void mergesort();
void merge();
main(){
	int a[40],i,n;
	printf("Enter Array Size-");
	scanf("%d",&n);
	printf("Enter Array Elements-");
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	void mergesort(a,0,n-1);
	printf("sorted elements are-");
	for(i=0;i<n;i++){
		printf("%d",&a[i]);
	}
}
void mergesort(int a[],int first,int last){
	int mid;
	if(first<last){
		mid=(first+last)/2;
		 mergesort(a,first,mid);
		 mergesort(a,mid+1,last);
		 merge(a,first,mid,last);
	}
}
void merge(int a[],int first, int mid,int last){
	int b[40];
	int i,j,k;
	i=first;
	j=mid+1;
	k=first;
	while((i<=mid)&&(j<=last)){
		if(a[i]<=a[j]){
			b[k]=a[i];
			k++;
			i++;
		}
		else
		b[k]=a[j];
		k++;
		j++;
	}
	if(i>mid){
		while(j<=last){
			b[k]=a[j];
			k++;
			j++;
		}
	}
	else{
		while(i<=mid){
			b[k]=a[i];
			k++;
			i++;
		}
	}
	for(i=first;i<last;i++){
		a[i]=b[i];
	}
}
Posted
Updated 16-Apr-24 16:30pm
v4

One error is apparent - this line in the main function :
C
void mergesort(a,0,n-1);
should not have the void statement in it. It should be like this :
C
mergesort(a,0,n-1);
Another one I noticed is also in main :
C
printf( "%d", &a[i] );
should not have the ampersand because the data needs to be passed by value, not by address :
C
printf( "%d", a[i] );
Here's a tip. I can't remember the last time I used the function scanf. I prefer to use fgets and read data from a file. Here's how you could that :
C
int ReadInteger( FILE * fp )
{
    int value;
    #define TextSize 79
    char text[ TextSize + 1 ] = { 0 };
    fgets( text, TextSize, fp );
    value = atoi( text );
    return value;
}
Then you can obtain input like this :
printf( "Enter Array Size : " );

count = ReadInteger( stdin );
printf( "\n" );
printf( "Array size is %d\n", count );
printf( "Enter Array Elements : " );

for( i = 0; i < count; i++ )
{
    a[ i ] = ReadInteger( stdin );
}

printf( "\n" );
Passing stdin to the function tells it to read data from the standard input handle. This is no different than what your code does now. However, you can also redirect input so it comes from a file and that is much easier to deal with for debugging. You can redirect input by using the < operator followed by the name of a file.
 
Share this answer
 
v3
Toi add to what Rick has said, you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs. Or in your case, type void randomly into our code!

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

And spending a little time learning to understand syntax error messages will save you a huge amount of time in future: you waited at least 1/4 hour for Chris to reply, then your email system probably added another 10 minutes or so, plus the time it took you to type up the question once you had found this site and created an account. Chances are that you could have saved a significant chunk of that time if you knew how to read them!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!

Once you have got rid of the syntax errors, it's time to look for runtime errors - which means running your code and testing it. When your code does something you don't expect, it's time to find out why. Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900