How to turn this C ++ code into C?

0
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, i, n1, c = 0, c1, c2, max, pos, bar[501], arr[250001], j;
    cin >> n >> n1;
    max = pos = 0;

    for (i = 1; i <= n*n1; i++)
    {
        cin >> arr[i];
    }

    for (i = 1; i <= n; i++)
    {
        c = 0;
        for (j = i; j <= n1*n; j += (n))
        {
            c += arr[j];
        }

        if (max <= c)
        {
            max = c;
            pos = i;
        }

    }

    cout << pos << endl;

    return 0;
}
    
asked by anonymous 22.11.2017 / 14:31

2 answers

3

Except for stream this code is already C, poorly written, but it is. It was not written the way C ++ does, though it compiles.

Change% of% by cin and scanf() by cout .

Do not forget to include printf() and get everything connected to the stream in and out.

#include <stdio.h>
int main() {
    int n, n1, arr[250001];
    scanf("%d %d", &n, &n1);
    int max = 0;
    int pos = 0;
    for (int i = 1; i <= n * n1; i++) scanf("%d", &arr[i]);
    for (int i = 1; i <= n; i++) {
        int c = 0;
        for (int j = i; j <= n1 * n; j += n) c += arr[j];
        if (max <= c) {
            max = c;
            pos = i;
        }
    }
    printf("%d", pos);
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
22.11.2017 / 14:38
1

Just swap cin by scanf, and cout by printf and remove the using namespace and swap the library to stdio.h

#include <stdio.h>  // printf e scanf

int main()
{
    int n, i, n1, c = 0, c1, c2, max, pos, bar[501], arr[250001], j;
    scanf("%i%i", &n, &n1);
    max = pos = 0;

    for (i = 1; i <= n*n1; i++)
    {
        scanf("%i", &arr[i]);
    }

    for (i = 1; i <= n; i++)
    {
        c = 0;
        for (j = i; j <= n1*n; j += (n))
        {
            c += arr[j];
        }

        if (max <= c)
        {
            max = c;
            pos = i;
        }

    }

    printf("%i", pos);
    return 0;
}
    
23.11.2017 / 02:11