Value of for loop changing by one [duplicate] - c++

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.

Related

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

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;
}
}

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;

Array Pointer Returns Garbage [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
C++ correct way to return pointer to array from function
(7 answers)
Closed 5 years ago.
I am trying to subtract two integers of the same size without using an explicit loop using valarray.
For this purpose I've written a function i.e subtract(int *firstarray, int *secondarray). However, the subtraction occurs correctly in function as printed out. But when returned to main() the first two values of array contain garbage. What is my mistake?
int* subtract(int* lastline, int* firstline){// takes two user defined arrays and performs subtraction
std::valarray<int> foo (firstline, 7); // 6 6 5 4 5 6 7
std::valarray<int> bar (lastline,7); // 1 8 8 8 8 8 8
std::valarray<int> res (7);
res=bar-foo; //subtracts two valarrays
for (size_t i=0; i<NUMBEROFCLASSES;i++){
cout<<res[i]<<" "; //prints 5 -2 -3 -4 -3 -2 -1
}
return &res[0];
}
int main(){
int first[7]={6,6,5,4,5,6,7};
int second[7]={1,8,8,8,8,8,8};
int *e= subtract(first, second);
cout<<endl;
for(int i=0; i<7;i++){
cout<<e[i]<<" "; // prints 0 0 -3 -4 -3 -2 -1
}
return 1;
}
res is a variable with automatic storage duration, meaning that it will be destroyed right when the function exits. e is a dangling pointer, so accessing it is undefined behavior. You can return a std::valarray instead.
std::valarray<int> subtract(int* lastline, int* firstline){
// Stuff
return res;
}

Ordered merge problems

I've got a little problem with my program and I can't solve what's wrong. Basically, there are two arrays sorted in ascending order and I have to merge them into one.
I expect the output to be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
But it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 0
What am I doing wrong, that the last value in the output isn't 14? I think that the solution to this will be very simple, but I can't figure it out.
Here's the code:
#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
int arrA[]={1,3,5,7,9,11,13};
int arrB[]={2,4,6,8,10,12,14};
int arrC[sizeof(arrA)/sizeof(int)+sizeof(arrB)/sizeof(int)];
int sizeA=sizeof(arrA)/sizeof(int);
int sizeB=sizeof(arrB)/sizeof(int);
int sizeC=sizeof(arrC)/sizeof(int);
for (int i=0;i<sizeA;){
for (int j=0;j<sizeB;){
if (arrA[i]<=arrB[j]){
arrC[i+j]=arrA[i++];
}
else{
arrC[i+j]=arrB[j++];
}
}
}
for (int i=0; i<sizeC; i++){
cout << arrC[i] << " ";
}
return 0;
}
Actually you never get to the point where You assign value to arrC[13] which is the last 14 element. In last iteration for your outer loop i==6 and the same for the inner loop. So you end when i+j is equal to 12.

Why is my function returning wrong values? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
question about leading zeros
As in stackoverflow.com/questions/3232534/question-about-leading-zeros.
Number of trailing zeros, binary search from Hacker's Delight:
#include <iostream>
using namespace std;
int ntz(unsigned x){
int n;
if ( x==0) return 32;
n=1;
if ((x & 0x0000FFFF))==0) {n=n+16; x=x>>16;}
if ((x & 0x000000ff)==0) {n=n+8;x>>=8;}
if ( x &0x0000000F)==0) {n=n+4; x>>=4;}
if ((x & 0x00000003)==0) { n=n+2; x>>=2;}
return n-(x &1);
}
int main(){
unsigned x;
cin>>x;
cout<<ntz(x)<<endl;
return 0;
}
When i enter 8 it return 8 and when I enter 9 the same result why?
Firstly, your code doesn't compile. The parentheses in lines 9 and 11 are not balanced correctly.
That said, after fixing the errors and compiling, I get the following results:
$ ./a.out
8
3
$ ./a.out
9
0