You are not logged in. Please login at to post your questions!


[C++] Am I using for loops correctly?


My solution for CLEANUP goes as follows:

#include <iostream>
#include <set>
#include <vector>
#define lo unsigned long long
using namespace std;
void solve(lo M, lo N,lo a[]){
    set<lo> s;
    vector<lo> v;
    vector<lo> j;
    vector<lo> k;
    bool polar = true;
    //creates list of 1,2..M
    for (lo x  = 1; x<=M;x++){
    //deletes terms
    for (lo x  = 0; x<N;x++){
    for (auto x : s) {
    for (lo x = 0; x<v.size();x+=2){
    for (lo x = 1; x<v.size();x+=2){

    for (auto x : j) {
        cout << x << " ";
    cout << endl;
    for (auto x : k) {
        cout << x << " ";
    cout << endl;

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    lo T;
    cin >> T;
    lo M,N = 0;
    lo a[N];
    for (lo x = 0; x<T;x++){
        cin >> M >> N;
        for (lo y = 0; y<N;y++){
            cin >> a[y];

    return 0;

However I seem to be getting wrong answers. I suspect this is because of how I used for loops. I tried to use increments of 2, however I'm not sure if that causes overflow or not(goes past the set limit). Can anyone confirm? Thanks

asked 21 Jul '17, 03:44

jaycram's gravatar image

accept rate: 0%

hey everything is correct but you are doing a small mistake.

Initially N is 0 and you are declaring an array a[N] then you can store only one element in the array.

Declare array after scaning the value of N then you will get AC.I just changed that got is the link


answered 21 Jul '17, 04:26

hruday968's gravatar image

accept rate: 14%

ty so much, this explains why my last 3 solutions have been wrong lol

(21 Jul '17, 20:11) jaycram3★

Another thing I want to add with @hruday, it's always good practice to declare an array globally. There's always chance of stack overflow if you declare an array locally.


answered 21 Jul '17, 08:06

sudip_95's gravatar image

accept rate: 10%

The best way to understand loops is to understand the concept behind them. Loops are used in programming when you need to perform a task more than once. While loops and For loops exist to this end. the main difference in the two, besides the code you use to write them, is that while loops check the condition at the end of the loop, and Do My Essays will repeat the loop until the while condition is met.


answered 21 Jul '17, 15:47

jenniferjudy22's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 21 Jul '17, 03:44

question was seen: 391 times

last updated: 21 Jul '17, 20:11