Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
#include <iostream>
using namespace std;
int f(int i){
int k=0;
if(i>0)
{
int k=i*10;
}
else {
int k= i++;
}
cout <<k;
return i;
}
int main()
{
cout << f(1);
cout << ".";
cout << f(0);
return 0;
}
This is the code, compiler shows "01.01" which i quite don't understand, any help will be very much welcomed!
int k = i * 10; and int k = i++; are declarations of k that shadow the outer k. The statement std::cout << k; in the outer scope therefore always outputs zero.
The only effect of the if body is to increase i by 1. And it only does that if i is zero (or less). That value of i is returned printed.
Thus the output is 01.01. Armed with a line by line debugger, the shadowing effect will be obvious.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 26 days ago.
Improve this question
Sorry for bad English .
I was trying to write a program that gets a number and see if the digits of an entered number are repeated or not . I did try to if(analyse[0]==analyse[1]==analyse[2]==...) but since I don't know exactly how many elements will array have, it didn't work
#include<iostream>
int main(){
int number,number_help;
const int count{10};
std::cin>>number;
number_help = number ;
int digitcount{0};
while(number_help>0){
number_help/=10;
digitcount+=1;
}
int analyse[count]{};
for(size_t i {0}; i<digitcount ; i++){
analyse[i] = number%10;
number/=10;
}
//I don't know what to code here
return 0;
}
Change your approach: count how many there are of each digit instead of comparing them to each other.
This is much simpler.
Example:
#include<iostream>
int main(){
int number;
std::cin >> number;
const int count{10};
int frequency[count]{};
do {
frequency[number % 10] += 1;
number /= 10;
} while (number != 0);
for (int i{0}; i < count; i++) {
if (frequency[i] > 1) {
std::cout << i << " was repeated " << frequency[i] << " times.\n";
}
}
}
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 3 years ago.
Improve this question
I was writing a code which displayed first three multiples of an integer using functions. But here when I run this it shows only the first multiple.
#include <iostream>
#include <vector>
// Defined first_three_multiples() here:
std::vector <int> first_three_multiples(int num) {
std::vector <int> output;
for (int i=1; i<4; i++) {
output.push_back(num*i);
return output;
}
}
int main() {
for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}
}
Expected Output: 8 16 24
Actual Output: 8
You have put the return statement inside the for loop. It is advised to follow proper code indentations in order to avoid these kind of errors from happening again.
#include <iostream>
#include <vector>
std::vector<int> first_three_multiples(int num)
{
std::vector<int> output;
for (int i = 1; i < 4; i++) {
output.push_back(num * i);
# you put a return statement here, which returns only 8
}
return output; # this would return all three values
}
int main()
{
for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}
}
Simple error
for (int i=1; i<4; i++) {
output.push_back(num*i);
return output;
}
should be
for (int i=1; i<4; i++) {
output.push_back(num*i);
}
return output;
It pays to get your indentation right as it makes errors like this much easier to spot (and avoid in the first place).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was debugging this, and debugger skipped the last 'if' even 'sum' was equal to 'n' and jump straight to 'else', I don't know why. Please help.
P/s: Can I use dynamic array to increase the mobility of my program?
#include <iostream>
#include <math.h>
using namespace std;
int exponent_of_10(); // set position for digits
int exponent_of_10(int a, int b){
for(int j = b; j>0;j--)
{
a *= 10;
}
return a;
}
main() //check if the number was palindromic
{
int n;
int a[6]={0,0,0,0,0,0};
int i = 0;
int temp;
int S;
cout<< "Input n (maximum of 6 digits): ";
cin>> n;
do
{
if(n<1)
{break;}
temp=n%10;
a[i]=temp;
n=(n-temp)/10;
i++;
}
while (n!=0);
int sum = 0;
for(int j=0; j<=5; j++)
{
exponent_of_10(a[j],j);
S = exponent_of_10(a[j],j);
if (S==0)
{break;}
sum +=S;
}
if(sum==n)
{
cout<< "Congratz, this is PALIDROMIC NUMBER !!";
}
else
cout<< "Sad life, this is NOT palidromic number";
return 0;
}
When the code exits the do ... while() loop, n is 0. For the test in the if to be meaningful, the code should save the original value of n somewhere and compare sum to that original value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
please someone help me finding the error in this code. i've its python counterpart which is working fine, but when i code this in c++ , giving values(N) >=5 gives me an infinite loop.
[in this problem, you have to find the no. of trailing zeros in the factorial of a given number]
[here T is no. of test cases, N is the number, and z is output]
#include <iostream>
using namespace std;
int main(){
long int T,N,Z;
cin>>T;
for(int x=0;x<T;x++){
Z=0;
cin>>N;
for(int y=1;y<=N;y++){
if(y%5==0){
while(y/5!=0 && y%5==0){
Z+=1;
y/=5;
}
}
}
cout<<Z<<endl;
}
}
my python code is (which is working fine)
T=int(raw_input())
for x in range(1,T+1):
Z=0
N=int(raw_input())
for y in range(1,N+1):
if y%5==0:
while y/5!=0 and y%5==0:
Z=Z+1
y=y/5
print Z
change this:
for(int y=1;y<=N;y++){
int temp=y;
if(temp%5==0){
while(temp/5!=0 && temp%5==0){
Z+=1;
temp/=5;
}
}
}
your logic is not correct. Look at this, which i correctly submitted , hope it helps:-
#include<iostream>
using namespace std;
int tailing_zero(int n){
int ans=0;
int DIV=5;
while(n/DIV >0){
ans=ans+n/DIV;
DIV=DIV*5;
}
return ans;
}
int main(){
int run_count;
cin>>run_count;
int input[run_count];
for(int i=0;i<run_count;i++){
cin>>input[i];
}
for(int i=0;i<run_count;i++){
cout<<tailing_zero(input[i])<<endl;
}
return 0;
}
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 9 years ago.
Improve this question
I wanna get the sum of the first five natural numbers, but there in this this code something is wrong, need to find the misstake ? Help
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i = 1, thesum;
while(i <= 5)
{
thesum += i;
i++;
}
cout << thesum;
return 0;
}
You haven't initialized thesum variable. Initialize it to 0.
int i = 1, thesum = 0;
Otherwise it will invoke undefined behavior.
As it was already pointed out you did not initialize local variable thesum. So it has some arbitrary value.
Also there is no any need to include header <cstdlib> because no one declaration from it is used.
As variable i is not used outside the loop it is better to make it a local variable of the loop.
So I would rewrite the program the following way
#include <iostream>
using namespace std;
int main()
{
const int N = 5;
int theSum = 0;
for ( int i = 0; i < N; i++ ) theSum += i + 1;
cout << "The sum of first " << N << " natural numbers is " << theSum << endl;
return 0;
}