Click here to Skip to main content
15,353,432 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Question:

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.


Error :

Line 28: Char 5: error: redefinition of ‘main’ [solution.c]
int main(int argc, char *argv[]) {
^~~~

What I have tried:

C++
#include<stdio.h>
int indexof(int nums[],int target)
{
    int i;
    for(i=0;i<4;i++)
    {if(target==nums[i])
            return i;}
    i=0;
    while(nums[i]<target)
        i++;
    return i;
        
}
int main()
{
    int nums[4]={1,3,5,6};
    int target=5;
    int c=indexof(nums,target);
    printf("%d",c);
    return 0;
}
Posted
Updated 11-Jun-21 22:15pm
Comments
Patrice T 12-Jun-21 3:39am
   
Wrong code, there is no line 28.
_-_-_-me 12-Jun-21 3:43am
   
But it is showing error really.
Patrice T 12-Jun-21 3:52am
   
Look again, there is no line 28 in this code.
_-_-_-me 12-Jun-21 3:57am
   
Yes, There is no line 28.
But, it is showing the same error.
Again I compiled it and checked the output.
It is showing the same error.
Richard MacCutchan 12-Jun-21 4:00am
   
There is no error in that code. Whatever you are compiling on your system, it is not the code you have posted above.
_-_-_-me 12-Jun-21 4:00am
   
I switched off the Pc, switched on again and refreshed the browser, but it is showing the same error.
There is no line 28.
Richard MacCutchan 12-Jun-21 4:01am
   
Why do you think that would help?
_-_-_-me 12-Jun-21 7:16am
   
Hehehe. I too don't know. I thought the PC is in problem as there is no line 28.
Thank you very much!

You aren't.
That code does not generate compiler errors - despite being very badly indented so it's hard to read.

I would suggest that you have a close look at the actual file "solution.c" because the chances are you are compiling the wrong file.
   
Comments
_-_-_-me 12-Jun-21 7:14am
   
Okay , thank you very much !
With out writing the main () function and writing in binary search procedure, I am getting no error.
I think compiler writes its main() function.
Is it possible?
OriginalGriff 12-Jun-21 7:32am
   
No, and the lack of enough lines to match the error report implies that you're compiling the wrong file: you don't have 28 lines, and the function signature that is reported doesn't match the one in your text. Unless you have modified stdio.h (which would have been a very silly thing to do) that isn't the code that's compiling!

Try this: compile so you get the error.
Rename the file to "whatever.c.bak"
Try exactly the same compile instruction again.

See what happens.
Quote:
Yes, There is no line 28.
But, it is showing the same error.

Then, it is not the file you are compiling.
There is nothing we can do for you until you find what file you are compiling.
Quote:
You must write an algorithm with O(log n) runtime complexity.

This means that in a list of 1000, you find position in looping 10 times.
Binary search algorithm - Wikipedia[^]
   
v2
Comments
_-_-_-me 12-Jun-21 7:11am
   
Thank you very much.
Yes , I will follow Binary search algorithm.

With out writing the main () function and writing in binary search procedure, I am
getting no error.
The error is quiet simple: you have 2 main functions in your code. Names must be unique in code, so the compiler knows which to use.

For your task you need to know about search algorithms. A common usable algorithm is the divide and conquer algorithm.

Tip: write test code and use clear names
   
Comments
_-_-_-me 12-Jun-21 7:10am
   
Thank you very much
With out writing the main () function and writing in binary search procedure, I am getting no error.
Does the compiler itself writes the main function?
KarstenK 12-Jun-21 13:40pm
   
No, normally your coding tool is using some project template which does it.

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