Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int* twoSum(int* nums, int numsSize, int target, int* returnSize);


int* twoSum(int* nums, int numsSize, int target, int* returnSize)
{
    int i,j, k= 0;

    for(i=0; i<numsSize; i++)
    {
        for(j= i+1; j<numsSize; j++)
        {
            if((nums[i]+nums[j])== target)
            {
                nums[k]= i;
                k++;
                nums[k]= j;
            }
            else
                printf("Target Not Found!!\n");
        }
    }
    *returnSize= k;

    return nums;
}

int main()
{
    int n;
    printf("Please enter the number of elements exist in the array entered: ");
    scanf("%d", &n);
    int arr[n]; 
    int i;

    for(i=0; i<n; i++)
    {
        printf("Array element no.%d: ",i);
        scanf("%d", &arr[i]);
    }

    for(i=0; i<n; i++)
    {
        printf("Array element no.%d: %d\n",i, arr[i]);
    }

    int target;
    printf("Please Enter the target you want: ");
    scanf("%d", &target);

    int size;
    int* result = twoSum(arr, n, target, &size);

    for(i=0; i<2; i++)
    {
        printf("Array element no.%d: %d\n",i, arr[i]);
    }
    return 99;
}


What I have tried:

Code works on CodeBlocks and on LeetCode compiler it does not!!!
LeetCode compiler keeps giving me these types of errors:
Line 63: Char 5: error: redefinition of ‘main’ [solution.c]
 int main(int argc, char *argv[]) {
     ^~~~

and the program is not 63 lines.
Posted
Updated 14-Mar-23 11:08am
v3

I just built that code at LeetCode and got a different error message:
Quote:
Line 36: Char 13: runtime error: variable length array bound evaluates to non-positive value -1428798312 (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:41:13

This mesage is, of course, correct, as you cannot create dynamic arrays on the stack in most C compilers.

The message you refer to above, suggests that you did not delete all the text that was in the edit box when you tried your code.
 
Share this answer
 
Comments
merano99 14-Mar-23 17:05pm    
+5, I come to the same conclusion here.
CPallini 15-Mar-23 3:12am    
5.
Just like Richard, I can't understand the given error message with main() here.
The code shown here could report an error here:
C
int arr[n];

1> error C2057: Constant expression expected.
1> error C2466: Assignment of an array of constant size 0 not possible.
1> error C2133: "arr": Unknown size

The Variable dynamic arrays feature is supported only by newer C compilers and C versions.
However, you can easily eliminate the bug by allocating the memory dynamically.
 
Share this answer
 
Comments
CPallini 15-Mar-23 3:12am    
5.

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