please help me

write a c++ program to read N numbers and to print the second largest number

Can you show us what you've got?
I am a student. I dont know how to program
I don't know how to program

Then I suggest you learn. We're not going to do your homework for you.
You can start here:
http://www.cplusplus.com/doc/
This is not my homework. I dont have a teacher since my studies are through distance education. I got some questions from previous years' question papers.
closed account (E0p9LyTq)
http://www.learncpp.com/
Is this a correct program to find the sum of squares from 1 to 100?

#include<iostream>

using namespace std;
int i, d, sum;
int main()

{
for(int i=1; i <= 100; ++i)
{
d = i * i;
sum += d;
}
cout << "Sum of squares from 1 to 100 is " << sum;
return 0;
}
.
Last edited on
Is this a correct program to find the sum of squares from 1 to 100?

You tell us if it works. Does it compile? Are you getting errors? Does it perform like you want to?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

// Reads numbers from user
void getInput (int (&N)[], int& NumberOfN)
{
    std::cout << "Enter numbers (type a non-numerical character when done): ";
    for (int Count = 0; Count < 100; Count++)
    {
        int Temp;
        std::cin >> Temp;
        if (cin.fail())
        {
            break;
        }
        else
        {
            N[Count] = Temp;
            NumberOfN++;
        }
    }
}

// Puts number into numerical sequence
void sortNumbersInOrder(int (&N)[], const int& NumberOfN)
{
    long long int Temp;
    for (int Adjacent{0}; Adjacent < NumberOfN; Adjacent++)
    {
        for (int Adjacent2{0}; Adjacent < (NumberOfN - 1); Adjacent2++)
        {
            if (N[Adjacent2] > N[Adjacent2 + 1])
            {
                Temp = N[Adjacent2];
                N[Adjacent2] = N[Adjacent2 + 1];
                N[Adjacent2 + 1] = Temp;
            }
        }
    }
}

// Gets the second largest number
int getSecondLargestNumber(const int (&N)[], const int& NumberOfN)
{
    return N[NumberOfN - 2];
}

int main()
{
    // Reads up to 100 numbers
    int N[100];

    // How many numbers there are
    int NumberOfN = 0;

    getInput(N, NumberOfN);

    sortNumbersInOrder(N, NumberOfN);

    int SecondLargestNumber = getSecondLargestNumber(N, NumberOfN);
    std::cout << "\n\n\nSecond largest number: " << SecondLargestNumber;

    return 0;
}
Last edited on
This is not my homework. I dont have a teacher since my studies are through distance education. I got some questions from previous years' question papers.

You do have a teacher that has assigned a task for you, and that teacher is you. Homework is something that you do by yourself in order to learn.

Is this a correct program to find the sum of squares from 1 to 100?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>

using namespace std;
int i, d, sum;

int main()
{
  for(int i=1; i <= 100; ++i)
  {
    d = i * i;
    sum += d;
  }
  cout << "Sum of squares from 1 to 100 is " << sum;
  return 0;
}

First, please post code with code tags and intuitive indentation. They make reading and commenting easier.
chicofeo wrote:
Does it compile? Are you getting errors? Does it perform like you want to?

These are essential questions. Syntactic errors prevent compilation.

However, a faulty program can produce seemingly correct results by a fluke. The compiler spits out both errors and warnings. The warnings do not stop compilation, but point to dubious constructs.

One can have logical errors that produce no warnings.

When you have both syntactically and logically correct program, you can still have style issues. There is more than one correct way to achieve the result, and some may have (debatable) advantage.

For example, the sum does not require a loop. See http://www.trans4mind.com/personal_development/mathematics/series/sumNaturalSquares.htm


On line 3 you have using namespace std; which is counter-productive. There is a reason why the standard library has been moved into namespace of its own.

The only place, where you do need the std is on line 13, where you can write std::cout. If you had cout in multiple places in main(), you could add using std::cout; into the main() to save a bit of typing.

The style of declaring multiple variables within one statement, like you do on line 4, may seem compact, but it is more clear to have separate statement for each, particularly if there are pointer types and/or initializers.

You do declare those three variables outside of any function. They are global variables. Globals are a bit tricky and restrictive and better used only when they are really needed. (The std::cout is a global variable ... )

You do declare variable i on line 4.
You do declare variable i on line 8.
Can you describe the lifetime and visibility of the two?
You do not need both.

You don't actually need the d either, because you can chain operators: sum += (i * i);



Now a big one: What is the value of sum before the loop starts?
Undefined. The sum must have a known value before the loop. It has to be set. Initialized.

Your program does not initialize the sum. That makes the program incorrect. It is rather unfortunate that in your environment the result appears to be correct. This time.
keskiverto (5678)

Thank you for guidance. This is my first program and I don't even understand some of your comments like

''Now a big one: What is the value of sum before the loop starts?
Undefined. The sum must have a known value before the loop. It has to be set. Initialized.''

Could you please write it correctly for me so that I can compare with mine and rectify my faulty program?
What does the int i=1; on line 8 do?
It declares that name i is a variable that has type int and initializes the i with value 1. The value of i is set to be 1.

What does your line 10 (d = i * i;) do?
It sets the value of d. Assigns a known value to d. (the i*i is known, because we know the value of i on each iteration).


However, your sum is is not initialized with a known value, nor is the value set elsewhere before the loop.

Within the loop you have sum += (i * i);
which is the same as sum = sum + (i * i);
The current value of sum (in bold) was never set, and therefore the new value of sum is undefined too.


You have to make sure that the sum has correct value before the loop starts.

Do you understand what is the correct value for sum at start?
Lets make a shorter serie:
1
2
3
4
for ( int i=1; i <= 1; ++i )
{
  sum += i * i;
}

We do know that 1*1 is equal to 1.
We do see that the loop computes sum + 1*1
Therefore, sum + 1*1 must equal 1.
Can you solve the sum from that equation?
The solution is the correct value.
Great and perfect explanation. Thank you.
Topic archived. No new replies allowed.