Glitches of the compiler?

Revision en1, by PostMunwai, 2017-04-17 11:36:05

Hello everybody! I'm new to programming, I am solving problem 160А, but the C ++ compiler surprised me very much.

According to the idea ** cout ** should not affect the logic (it's just ** cout **), but somehow effects.

With cout

Withoud cout

Here is my (awful) code:

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    int result = 0;
    int point = 0;
    int sum = 0;
    
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    // sort 
    int temp;
    int count = (sizeof(a)/sizeof(*a));
    for(int i = 0; i < count; i++) {
        for(int j = 0; j < count; j++) {
            if(a[j] > a[j+1]) {
                temp = a[j];
		a[j] = a[j+1];
		a[j+1] = temp;
            }
        }
    }
    
    
    //reverse
    int reverse_temp;
    for (int i = 0; i < count/2; ++i) {
        reverse_temp = a[count-i-1];
        a[count-i-1] = a[i];
        a[i] = reverse_temp;
    }
    
    
    

    for(int i = 0; i < count; i++) {
        point = point + a[i];
        // cout << "point: " << point << endl;
        result = result + 1;
        // cout << "result :" << result << endl;
        
        for(int j = result; j < count; j++) {
            sum = sum + a[j];
            // cout << "sum: " << sum << endl;
        }
        
        if(point > sum) {
            cout << "answer: " << result << endl;
            break;
        } else {
            sum = 0;
        }
        
    }
    
    return 0;
}
Tags cpp5, cpp11, compilation error, epic fail, awful, headache, wtf, 160a

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English PostMunwai 2017-04-17 11:36:05 1860 Initial revision for English translation
ru1 Russian PostMunwai 2017-04-17 10:51:19 1861 Первая редакция (опубликовано)