Doubts Hackerrank in C and C ++ compiler environment

Recently nothing to do Hackerrank of C ++ problem, brush the base question the second question Simple Array Sum on the problem, the same code to run out in C ++ and C environment may look different, C ++ to be wrong, and C does not will only know roughly where a problem but no detailed cause. I hope that the god of criticism.

The results submitted in the C ++ code snippets, and finally

int simpleArraySum(vector<int> ar,int n) {
    /*
     * Write your code here.
     */
    int sum = 0;
    for(int i = 0;i < n;i++){
        sum += ar[i];
    }
    return sum;
}

After the submission of the results of the code up as follows:
The first and second case is wrong
The original case is correct
find a lot of problems, but I could not find, so I tried to run a bit in Dev C ++, the step by step Look at the result, which is used Dev C ++ source code

#include <stdio.h>
#include <iostream>

using namespace std;

int simpleArraySum(int ar[],int n) {
    int sum = 0;
    for(int i = 0;i < n;i++){
        sum += ar[i];
    }
    return sum;
}

int main(){
	int n;
	cin >> n;
	int ar[n];
	for(int i = 0; i < n; i++){
		cin >> ar[i]; 
	}
	int sum1 = simpleArraySum(ar,n);
	cout << "sum = " << sum1;
	return 0;
}

Run showed no errors, and the associated Test case 1 is correct
所以就很奇怪
since it is the C ++ language, but not the same environment, causing the results are not the same, indicating that the operating environment in Hackerrank inside (on the website) could and Dev C ++ is not the same it was estimated that the code related to the compiler (chicken dish not a computer component principle, etc. By the end of the page code how it looked back out of).
在这里插入图片描述
Look at its source code can only find things on the web page content only and not to involve compiling operation, it can be concluded that roughly procedure code is compiled: edit content on the page, and then submit up or run through the submission button, cloud completion of the test, and then compile the results would customer feedback. This is no way to see in the end is where wrong.

It can only change the languages, C ++ is almost fully compatible with C (except for some special places both have similar features seen on an almost), so this short code should be no problem.

The results of submitting the same code fragment in the C

在C下的环境运行结果
在这里插入图片描述
Toss me so long to finish the basic problem finally over ... next time we note that the error may be because it compiles environmental problems, not necessarily the pot program, the compiler environment to try for the other languages.

Released five original articles · won praise 1 · views 107

Guess you like

Origin blog.csdn.net/weixin_41425032/article/details/103306638