Need Explanation for C++ output [closed] - c++

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 2 years ago.
Improve this question
#include <iostream>
int main()
{
int i = 100,sum=0;
for(int i =0; i!=10;++i)
sum+=i;
std::cout<<i<<" "<<sum<<std::endl;
return 0 ;
}
I'm a beginner in C++ , the output of the code is 100 45 . I understand 100 as its block scope but why 45?

As sum is declared in the outer scope and not redefined inside your loop, the loop is operating on the outer sum which means it's value ends up equivalent to the cumulative value of the loop scoped i which would be:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
The i variable is instead redefined in the loop scope (int i=0;), therefore while in the loop block it goes from 0 to 9, but once out of the loop the i variable is taken into account is the one with 100 assigned.
Next time, if you have the tools, I'd recommend stepping through the loop with a debugger and having a look at what the variables and values are doing.

You display i and sum right after it.
At the start of your loop i=100 and as you declare another i in the scope of your loop, when the code goes out of the scope of your loop, it display the value of the first i, that is 100 and sum = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45.

Related

Check for primality number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
The community is reviewing whether to reopen this question as of 2 days ago.
Improve this question
I had come across a code solution to check whether any number if prime or not.
int isPrime(int N){
if(N<=1){
return 0;
}
if(N==2 || N==3){
return 1;
}
if(N%2==0 or N%3==0){
return 0;
}
for(int i=5;i*i<=N;i=i+6){
if(N%i==0 || N%(i+2) ==0){
return 0;
}
}
return 1;
}
Can anyone explain, why we are increasing i=i+6 and condition N%(i+2)==0??
We are checking N%i==0 then why we are checking it for i+2?
Starting from 4 every second number is not prime. Starting from 6 every third number is not prime. Now consider what is left
4 5 6 7 8 9 10 11 12 13 14 15 16 17
2+x*2 x x x x x x x
3+y*3 x x x x
others 5 7 11 13 17 19 ...
The pattern continues and has a length of 2*3 = 6.

Any other way to write a loop for multiplication table to take run time values? [closed]

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 3 years ago.
Improve this question
To print multiplication tables, I'm using a for loop which has fixed values up to 10th times table only. I need multiplication table as run time values. Is there an alternate loop to take the values at run time? Here is the code which i tried:
scanf(&num);
for(i=num;i<=num;i++)
for (j=1;j<=10;j++)
This is not a Python For loop
In python,you can use the Range function to have a nice multiplication table.
def table_choice(my_choice=None):
for a in range(10):
if my_choice != None:
print('{0} x {1} = {2}'.format(my_choice, a, my_choice * a))
else:
for b in range(10):
print('{0} x {1} = {2}'.format(a, b, a * b))
table_choice(my_choice = 7)
OUTPUT:
7 x 0 = 0
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
In case you execute table_choice() you will get the full table
See the Range documentation in : https://docs.python.org/3/library/functions.html#func-range

getting WA in uva 10954 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
lets add some flavor of ingenuity to it.Addition operation requires cost now, and the cost is the summation of those two to be added. So,to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways1 + 2 = 3, cost = 31 + 3 = 4, cost = 42 + 3 = 5, cost = 53 + 3 = 6, cost = 62 + 4 = 6, cost = 61 + 5 = 6, cost = 6Total = 9Total = 10Total = 11I hope you have understood already your mission, to add a set of integers so that the cost is minimal.Input Each test case will start with a positive number,N(2N5000) followed by N positive integers(all are less than 100000). Input is terminated by a case where the value of N is zero. This case should not be processed.Output For each case print the minimum total cost of addition in a single line.
Sample Input
3
1 2 3
4
1 2 3 4
0
Sample Output
9
19
i tried to sort the given array and then took another array for cumsum (CS) and summed all element of CS except cs[0].. i am getting WA for this approach, please explain
int n,i,hold=0;
while(1)
{
cin>>n;
if(n==0){break;}
int arr[n],cs[n];
for(i=0;i<n;i++) cin>>arr[i];
sort(arr,arr+i);
cs[0]=arr[0];
for(i=1;i<n;i++){cs[i]=arr[i]+cs[i-1]; }
cs[0]=0;
int sum=0;
for(i=1;i<n;i++){sum+=cs[i]; }
cout<<sum<<endl;
sum=0;
}
input:
9
66 85 52 22 44 1 59 88 67
0
my out:
1822
expected result(udebug):
1454
getting WA
Your idea is wrong to solve this problem.
after taking all the elements on a data structure you should repeat this 3 points:
1)sort.
2)sum 1st two value,and remove 1st two value from the data structure
3)add the sum to the cost and data structure.
you can use priority_queue as the data structure.
Use min heap and add 2 smallest element. Example:
1 2 3 -> 3 3 -> 6.
1 2 3 4 -> 3 3 4 -> 4 6 -> 10.
Hope it helps.

Nested for() in c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
No matter how I see this problem, I keep getting the answer to be 10. When I run the program, the answer gives me 20. I am lost as to how this should be solved. Could anyone help me with much detail? Thanks! ps. first programming class in c++.
int n = 0;
for (int i = 1; i <= 5; i++)
for (int j = 0; j < i; j++)
n = n + j;
Let's see n after each iteration of i.
n = prev value of n + sum of 0 to i-1;
i=1 , n= 0
i=2 , n= 0+0+1=1
i=3, n= 1+0+1+2 =4
i=4, n= 4+0+1+2+3=10
i=5, n= 10+0+1+2+3+4 =20
When i == 1 you add 0 to n, so n == 0.
When i == 2 you add 0 and 1 to n, so n == 0 + 0 + 1 == 1.
When i == 3 you add 0, 1, 2 to n, so n == 1 + 0 + 1 + 2 == 4.
When i == 4 you add 0, 1, 2, 3 to n, so n == 4 + 0 + 1 + 2 + 3 == 10.
When i == 5 you add 0, 1, 2, 3, 4 to n, so n = 10 + 0 + 1 + 2 + 3 + 4 == 20
If you only got 10, you either missed the last iteration of the outer loop (because 10 is the result after 4 iterations), or you forgot that you're adding on to what you accumulated in the previous iterations (since the last iteration adds 10 to the total).

select n number of element from an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There is given array of size > n , we need to select n number of element from an array.
For example : array contain 112 element and n = 50, so select 50 number such that distance between every two selected number is more or less equal (equal distance is not possible of course except for size%n == 0 ).
If anyone suggest any idea that would work .
Example :
array = 1 2 3 4 5
n = 1
output : 1 or any another number depending on proposed algo.
n = 2
output : 1 3 or 2 4 or 1 4...
n = 3
output : 1 3 5
n = 4
output : 1 3 4 5 or 1 2 4 5
n = 5 :
output 1 2 3 4 5
Basically in case of n=1,2,4 there are more then one possible combination so I need to devise an algo which would pick numbers in uequally distributed manner.
One approach would be dividing the number of elements by the number of desired elements in the selection in floating point, and using rounding to determine the index:
double dist = ((double)size) / n;
int *res = new int[n];
for (int i = 0 ; i != n ; i++) {
res[i] = orig[round(dist*i)];
}
For your example of 112 and 50 the value of dist would be 2.24 and the sequence of indexes selected from the array would be
0 0
1 2
2 4
3 7
4 9
5 11
......
45 101
46 103
47 105
48 108
49 110