The inner loop only runs once [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am curious as though, why when I use an array in for-loop in the given manner below, the inner loop only runs once.
Thank you for your reply.
#include<iostream>
using namespace std;
int main(){
int a[2]={0};
a[0]=5;
for(a[0];a[0]<10;a[0]+=1){
for(a[1];a[1]<10;a[1]+=1){
cout<<a[0]<<" "<<a[1]<<endl;
}
}
}

You are never resetting a[1], so once it hits 10 the first time through the inner loop, it will never execute again. Modify to for(a[1]=0;a[1]<10;a[1]+=1){

Related

Why am I not able to use Vectors in the Dev C/C++ IDE [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
So, here I wanted to just define a vector and then print the size of the vector's present state, but the compiler in dev c/c++ gives me a compile-time error that "vector was not declared in the scope()"
I wonder if I haven't included the right header files,or something.
#include<iostream>
using namespace std;
int main()
{
vector<int> v(6); //defining the vector
cout<<"Size= "<<v.size(); //printing the present size of the vector
return 0;
}
Just include vector header.
#include <vector>

python: check if all elements the same in an list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am using the following code to check if all the elements in a list are the same:
def sameItem(myList):
return all(x==myList[0] for x in myList)
However, in my test case:
myL1 = ['dog','cat','dog']
sameItem(myL1)
returns True. Shouldn't it be False? Or did I have a bug in the sameItem() function?
Also, I am using Jupyter Notebook, could it cause any problem is this scenario?
Thanks!
Your method should be correct and works for me. As an alternative, you can try this method to double check, which is a one line that does the same thing
return myList[1:] == myList[:-1]

Read from file negative numbers C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have to get numbers from file and some of them are negative. So, how can I do that? (C++)
Now I'm trying just this (without something special):
U1.txt file:
4 5 -1 6 -2
My code:
ifstream fd(FD);
int n1,n2,n3,n4,n5;
fd>>n1>>n2>>n3>>n4>>n5;
Your code is basically correct, except for that typo with n4 missing.
This:
ifstream fd("U1.txt");
int n1,n2,n3,n4,n5;
fd>>n1>>n2>>n3>>n4>>n5;
Will do what you expect, modulo error conditions.

a Sequential that decreased by 5 using a for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i want to print a Sequential that decreased by 5
using a for loop
but i don't know why it doesn't print the output :(
#include<bits/stdc++.h>
using namespace std;
int main(){
for(int j=60;j<=0;j-=5){
cout<<j<<endl;
}
}
whats the wrong?!
Change it to
for(int j=60;j>=0;j-=5){
^^

Binary sort on a vector [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am sorting my vector of strings in the following way, so that I can binary_search it later.
std::vector<std::string> vec;
...........
...........
std::sort(vec.begin(),vec.end());
Now I am searching it as follows.
if (!std::binary_search(vec.begin(), vec.end(), "SomeString"));
{
//Not Found
}
else
{
//Found
}
However, it seems that the binary_search is not working, and it returns a false to the "strings" that are present in the vector.
What might I be doing wrong?
Look at the very last character on this line:
if(!std::binary_search(vec.begin(),vec.end(),"SomeString"));
You have a misplaced ; there. Remove it and test again.