Please explain

Give the output of the following program. 3
Please explain

#include<iostream.h>
void sumfn(int last)
{
auto int sum = 0;
static int sum2 = 0;
for( int i = last; i > 0; i-- )
sum += i;
sum2 += sum;
cout<<sum<<" "<<sum2<<endl;
}
void main( )
{
for( int i = 1; i < 7; i++)
sumfn(i);
}
We are not going to do your test for you. You have submitted about a dozen different programs that are poorly typed and/or have several errors in them. You're not using the code tags when you submit your code, making it a lot more difficult to read the code, and you haven't made any attempt of solving the problems yourself. If you have, you haven't shown us that you've made an attempt. You should probably learn some more C++ before continuing this class any further.
There are numerous problems with that code.
1)iostream.h is deprecated and not standard you should use iostream then you will need to use std:: in front of cout and endl.
2)main should be int main and not void main.
3)Why are you using auto int sum = 0; auto is used so you don't have to tell it a type. (type deduction)http://www.cplusplus.com/doc/tutorial/variables/.

Also I am a bit curious as to why you loop backwards for( int i = last; i > 0; i-- )


Also if you don't know the output you can always compile..(well try to then fix errors as mentioned above.) basically though static means that the variable has a lifetime until the program ends and is initialized only once. So this means that sum2 will increment each time. It would almost be like calling:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//in your main function...


int sum2 = 0; //doesn't reset each time
//lifetime is longer than the loops
for(int i = 1; i <7; ++i)
{
    int sum = 0;
    for(int j = last; j > 0; --j) //questionable
    {
        sum += j;
        sum2 += sum;
        std::cout << sum << ' '  << sum2 << std::endl;
    }
}
Last edited on
sorry for bothering you all... actually these questions are copied from Board exam papers. I didnt miss out anything. I am studying in 12th std. and these questions are from previous Board papers. I have nobody to ask for. Our school sir is also on leave.
I would suggest to run from those who teaches outdated non-standard non-working C-style C++.
iosteam.h does not exist by current standarts!
auto keyword changed its meaning
void main() is straight up illegal and always was!
hehe... these are questions from CBSE Board Exam.
Sorry for being a little harsh, but it looks like you're just asking us to solve the problems for you. The code they're giving you is seriously outdated, filled with a lot of errors, and doesn't seem to have any kind of formatting. I would strongly suggest that if you're going to attempt to learn the language, you learn more about the fundamentals of the language before just attempting to answer questions. I would highly recommend starting with the tutorial on this site as it would have answered most of the questions that you asked us, and will help you have a better understanding of C++.

Also, if you're going to continue to post questions that have code in them, please, please use the [code][/code] tags. This maintains the code's formatting, and helps with syntax highlighting.

Using giblit's example:
[code]//in your main function...


int sum2 = 0; //doesn't reset each time
//lifetime is longer than the loops
for(int i = 1; i <7; ++i)
{
int sum = 0;
for(int j = last; j > 0; --j) //questionable
{
sum += j;
sum2 += sum;
std::cout << sum << ' ' << sum2 << std::endl;
}
}[/code]

Becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//in your main function...


int sum2 = 0; //doesn't reset each time
//lifetime is longer than the loops
for(int i = 1; i <7; ++i)
{
    int sum = 0;
    for(int j = last; j > 0; --j) //questionable
    {
        sum += j;
        sum2 += sum;
        std::cout << sum << ' '  << sum2 << std::endl;
    }
}


See how much nicer that looks? To use the code tags, you can type them exactly how they're shown above ([code][/code]) or by clicking the "<>" button to the left of the reply window where it says "Format:". This will help the community out by allowing us to easily read your code and assist much faster and more helpful.
Last edited on
I agree with Volatile Pulse and the others, all ive seen is "explain please". I understand that you need help, but you are not helping yourself at all - you need to do what the guys suggested and learn c++ then you could do dry runs with ease.

Basically what your doing is getting us guys to answer the questions for you and at the end of it all you may have some paperwork to say you have passed and can do it, yet you wont be able to - not much use to you is it :)



Thank you all... Yes, definitely I will learn tutorial ... Thank you for helpful advices and suggestions. this forum us really useful for me.
Topic archived. No new replies allowed.