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.
Related
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;
}
}
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.
This question already has answers here:
Casting int to bool in C/C++
(3 answers)
Closed 2 years ago.
I was wondering if you could clarify me the difference between the conditions while(k) and while(k > 0) when do they differ?
#include <iostream>
int main() {
int k;
std::cin >> k;
while(k) {
std::cout << "Hello" << "\n";
k--;
}
}
#include <iostream>
int main() {
int k;
std::cin >> k;
while(k > 0) {
std::cout << "Hello" << "\n";
k--;
}
}
The two code snippets you present will give equivalent results if – and only if – the input value for k is not negative. Try it.
Specifically, for the first snippet, if a value of -1 is input for k, the --k; line inside the while loop will (maybe) never reduce k to zero, or (maybe) just take a long time (until k reaches INT_MIN – something like -2147483648), depending on how the platform you are using handles signed integer underflow.
The while (k) loop will run until k is zero.
However, in the second snippet, a negative input for k will mean that the loop never runs (k will not be greater than zero on the first test).
This question already has answers here:
How does the scanf function work in C?
(7 answers)
Closed 8 years ago.
Write a program to read an integer number and keep on adding the digits till
we get a number with a single digit. For example, 7976 yields an output of 2
(7976 - - t 29 - - t 11 - - t 2).
For this, your main() function must call the function sumdigits(0) to solve
the problem, and then print the final result.
I have solved this problem and the logic is correct, I am not getting any output When i give my input it just move to next line.
#include <stdio.h>
sumdigits(int x)
{
int n = x;
int y = 0;
while(n>0) {
y = y + n % 10;
n = n / 10;
}
return y;
}
int main(void)
{
int a;
scanf("%d",&a);
while(a>10) {
a=sumdigits(a);
}
printf("%d",a);
}
Problem is with this line
scanf("%d",a);
It should be
scanf("%d", &a);
//----------^ use &
This question already has an answer here:
Integer division always zero [duplicate]
(1 answer)
Closed 8 years ago.
I know this has to be something extremely idiotic, but I really can't find my mistake.
I'm trying to write a program to calculate (2/i)^i, where i is an integer put in input by the user. If i = 0, the function returns 1.
This is the function I have been using.
double f (int x)
{
double y = 1.0;
if (x == 0)
return 1;
else
for (int i = 1; i <= x; i++)
y *= pow (2/i, i);
return y;
}
int main ()
{
for(int i=0;i<=10;++i)
cout << "f(" << i << ") =" << f(i) << endl;
}
Now.. This is supposed to be an extremely simple code. Yet, it doesn't work. It compiles correctly, but the results are not what I'm expecting. After some iterations, I always get 0 as a result. What am I doing wrong?
Integer division:
change to:
y *= pow (2.0/i, i);
^^^
Also, why do you need a loop?