0

so I made a simple loop that finds out if an array has the elements with the values of 0 and 1. if the loop indeed finds 0 or 1 inside of the array, it will say "YES", otherwise "NO". yes, the program works just fine, but at the end of the program it prints out "YES" or "NO" as many times as i put cin>>dim to. for example if dim which means (dimension[of the array]) is 5 it's going to print either "YESYESYESYESYES" or "NONONONONO" I have to use return 0 in order to make it print it out like once, but I feel like this is not the right way to do it. Please help me with this. thanks!

#include <bits/stdc++.h>
using namespace std;
int main()
{
int i, dim, v[100];
cin>>dim;
for(i=0;i<dim;i++)
    cin>>v[i];
for(i=0;i<dim;i++)
    if(v[i]==0 || v[i]==1){
        cout<<"YES"; return 0;}
    else{
        cout<<"NO"; return 0;}
  return 0;
}
  • 3
    You can use `break` as any [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will tell you. But why do you write a loop in the first place if you are terminating it at the end of the first iteration? – Lukas-T Feb 17 '20 at 20:38
  • Exiting the loop can be done with `break`. If you only want the output once, then you need to work out what the answer is within the loop (e.g. by setting the value of a variable to something) and do the output AFTER the loop. You can't expect code that produces output on every iteration to only perform output once. – Peter Feb 17 '20 at 20:53
  • I'm curious as to what you are trying to do with this program. It looks like it will ask for how many values then read in that many values and then checks the first array element to see if it is either 0 or 1 and then prints "YES" or if it is something else in which it prints "NO". Do you want to only check the first array element, `v[0]`, only or do you want to check the entire array? – Richard Chambers Feb 17 '20 at 21:34
  • @churill sorry, how could I have done it differently? I'm in 3 months of C++ and i'm still learning tons, I haven't finished all advanced material yet. – jaketherazvy Feb 18 '20 at 07:22

3 Answers3

0

The break statement can be used to break out of loops. The example from cppreference:

for (int j = 0; j < 2; j++) {
    for (int k = 0; k < 5; k++) {         //only this loop is affected by break
        if (k == 2) break;
        std::cout << j << k << " ";
    }
}

As the comment suggests, break only breaks the innermost loop.

In your code you always exit from the loop on the very first iteration, hence you do not need the loop in the first place. This will have the same output as your code:

int main() {
    int i, dim, v[100];
    cin >> dim;
    for(i=0; i < dim; i++)
        cin >> v[i];

    if(v[0] == 0 || v[0] == 1) {
        cout << "YES";
    } else {
        cout << "NO";
    }
}

After reading the question again...

I made a simple loop that finds out if an array has the elements with the values of 0 and 1

If you exit the loop after checking the first element then you only check the first element. If you want to see if an array contains only 1 or 0 or it contains at least one element which is 0 or 1 (not 100% clear which one you want), then you rather need this:

bool only_zero_or_one = true;
bool one_zero_or_one = false;
for (int i = 0; i < dim; ++i) {
     zero_or_one = ( v[i] == 0 | v[i] == 1);
     only_zero_or_one = zero_or_one && only_zero_or_one;
     one_zero_or_one = zero_or_one || one_zero_or_one;
}

Only for one_zero_or_one you can break the loop once zero_or_one == true.

Moreover, you should rather use a std::vector. In your code, if the user enters a dim which is greater than 100 you write beyond the bounds of v. This can be avoided easily:

 size_t dim;
 std::cin >> dim;
 // construct vector with dim elements
 std::vector v(dim);               
 // read elements
 for (size_t i=0; i < v.size(); ++i) std::cin >> v[i];
 // .. or use range based for loop
 for (auto& e : v) std::cin >> e; 
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Hmm I do get what you're saying. I haven't got to the std::vector yet, I don't really know what it does and how it works, I don't know how to use v.size() since I don't know what the () do, I don't know quite a lot of things yet from the last code you've written but the other ones I can actually understand pretty well and they're good ideas. EDIT : can you please tell me why you use std:: everytime instead of just writing code without it? – jaketherazvy Feb 18 '20 at 07:28
  • @jaketherazvy glad to help. I put the vector in the end, because I didnt want to put too much, but you should take a look at it at some point. Counterquestion: Why do you not want to write `std::` ? Honestly I never understood the advantage. Also see here on what harm `using namespace std;` can do: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – 463035818_is_not_an_ai Feb 18 '20 at 10:20
0

but I feel like this is not the right way to do it

Returning is an entirely right way to break out from a loop.

Another right way is the break statement, which jumps to after the loop.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Even better, you can actually check if v[i]==0 or 1 inside the input for loop immediately after taking input and set a flag to true. Depending on requirement, you can either break or wait until the entire input is read and then come out and check for flag==true and then print "YES" and print "NO" if flag==false. This will save you running the loop again to check for 0 or 1.

rootkonda
  • 1,700
  • 1
  • 6
  • 11