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

int main()
{
    int n;
    cin>> n;

    int a[n];
   
    for(int i=0;i<n;i++)
    cin>>a[i];

    for(int i=0;i<n;i++)
    {
        for(int j=i;j<n;j++)
        {
            cout<<a[j]<<" ";
        }
        cout<<endl;
    }

    int sum=0;
    int mx=INT_MIN;
   

    for(int i=0;i<n;i++)
    {
        for(int j=i;j<n;j++)
            {
                for(int k=i;k<=j;k++)
                sum += a[k];

                mx = max(mx,sum);
                cout<<endl; 
            }
        }
        
    }
    
    cout<<mx;
    return 0;
}

The compiler gives these three errors.

MaxSubArrSum.cpp:40:5: error: 'cout' does not name a type
   40 |     cout<<mx;
      |     ^~~~
MaxSubArrSum.cpp:41:5: error: expected unqualified-id before 'return'
   41 |     return 0;
      |     ^~~~~~
MaxSubArrSum.cpp:42:1: error: expected declaration before '}' token
   42 | }
      | ^

I feel the third issue will resolve on its own if the second one gets addressed? The quoting of cout and return suggests that it is a general syntax error. I've tried to keep my code as clean as possible by putting spaces and brackers appropriately though.

1 Answers1

2

There was a } too many close to end of the program above cout<<mx;. Some other feedback and a fixed version here :



//#include<bits/stdc++.h> <== don't do this     
//using namespace std; <== don't do this either 

#include <iostream>
#include <vector>    // for dynamically resizable arrays
#include <limits>

int main()
{
    int n;
    std::cout << "how many values do you want to enter : "; // give info to user!
    std::cin >> n;

    // int a[n]; // this is NOT valid C++
    // for dynamically allocatable arrays use std::vector
    std::vector<int> a;

    for (int i = 0; i < n; i++)
    {
        int value;
        std::cout << "enter value : "; // give info to user!
        std::cin >> value;
        a.push_back(value); // add value to array
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            std::cout << a[j] << " ";
        }
        std::cout << "\n"; // avoid std::endl
    }

    int sum = 0;
    //int mx = INT_MIN; <== "C" style
    int mx = std::numeric_limits<int>::min();

    for (int i = 0; i < n; i++)
    {
        for (int j = i; j < n; j++)
        {
            for (int k = i; k <= j; k++) sum += a[k];
            mx = std::max(mx, sum);
        }
    }

    std::cout << mx;
    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19