Why this c++ for loop is going into infinite loop [duplicate] - c++

This question already has answers here:
vector.size() - 2 leads to infinite loop - C++ [duplicate]
(2 answers)
How do unsigned integers work
(3 answers)
How compare vector size with an integer? [closed]
(3 answers)
Closed 9 months ago.
The value for i keeps decreasing forever. Technically it should stop after first iteration because ans.size()-k = 0, but it doesn't stop. If I put 0 manually over there then it works completely fine.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> ans(1);
int k = 1;
for (int i = ans.size() - 1; i >= ans.size() - k; --i)
{
cout << i << endl;
}
}

Related

recursive countdown. Trouble with sizeof(array), not returning full array size [duplicate]

This question already has answers here:
Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate]
(6 answers)
Passing Arrays to Function in C++
(5 answers)
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
I am trying to recursively count down from an array. I have done this previously in python, but I am having trouble finding the size of array. My sizeof() function is only returning a length of 2, which is odd since the array size should be 20 bytes/4bytes.
string cdown(int param[])
{
int length = sizeof(param)/sizeof(param[0]);
cout<<"length: "<< length << endl;
string blastoff = "blastoff";
for (int i=0;i<length;i++)
{
if (param[i]==0){return blastoff;}
else
{
cout<<param[i]<< " ";
param[i] = param[i+1];
param[length-1] = 0;
cdown(param);
}
}
}
int main(void)
{
int countdown[] = {5,4,3,2,1,};
cdown(countdown);
return 0;
}
Result
length: 2
5
length: 2
4
length: 2

Fraction pattern in c++ [duplicate]

This question already has answers here:
C++. Dividing 1 by any number gives 0
(3 answers)
Closed 1 year ago.
I need to write a program to run this pattern in c++:
S=1/2+2/3+3/4+4/5+...+N-1/N
I have tried but my code is showing 0.
And its the code that I have written:
#include <iostream>
using namespace std;
int main()
{
unsigned int N;
float S=0;
cout << "Enter N:";
cin >> N;
for (int I = 2; I <= N; I++)
{
S = S + (I - 1) / I;
}
cout << S;
return 0;
}
I have to write it with for-loop, while and do-while
(I - 1) / I only contains integers, therefore any remainder is discarded.
You can avoid this by simply subtracting - 1.f off of I instead.

Value of for loop changing by one [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 2 years ago.
using namespace std;
#include<iostream>
#include<cmath>
int main()
{
int n;
cin>>n;
int temp;
int m=0;
for(int i=0;i<n;i++)
{
cin>>temp;
m += pow(10,i)*temp;
cout<<"\n"<<m<<"\n";
}
return 0;
}
OUTPUT
5
1
1
2
21
3
320
4
4320
5
54319
I wanted to get 54321 but instead got this for some reason a 1 is getting removed in the 3rd and last loop.

Print a sequence of n^n using do while [duplicate]

This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
What is the C++ function to raise a number to a power?
(17 answers)
Closed 3 years ago.
Print a sequence of n^n for 1 to 15. This code only prints out 0. What am I doing wrong?
int main()
{
int i, n;
i = 0;
n=1;
do {
n = n^n;
cout << n;
n++;
i++;
}while(i<15);
return 0;

C++ Macro - Error in for loop [duplicate]

This question already has answers here:
A riddle (in C)
(4 answers)
Closed 5 years ago.
I have a problem with the following code.
The first for loop prints all the elments in the array, while the second for loop does not print any thing. Why?
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
//Working
for(d=0;d < (TOTAL_ELEMENTS);d++)
{
cout << array[d] <<endl;
}
//This does not work. Why does this code fail? Isn't it same as the one above?
//If I assing TOTAL_ELEMENTS to a variable and then use that in for loop (below), it works! Why?
for(d=-1; d < (TOTAL_ELEMENTS);d++)
{
cout << array[d + 1] << endl;
}
}
Any help is appreciated.
sizeof operator returns a size_t value, which is an unsigned integral type, so in this loop:
for(d=-1; d < (TOTAL_ELEMENTS);d++)
-1 is converted to a very big unsigned integral value.