Output is not correct. What is wrong with my code?

Write a C++ program that declares an array alpha that has 50 double components. Write a function that stores the square of the index for the first 25 components and half of the index for the last 25 components. Write a function that prints the contents of the array with 10 elements per line.

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
#include <iostream>

using namespace std;

int main()
{
   double alpha[50];
   int i;

    for(i = 0; i < 50; i++)
    {
        if(i < 25)
        {
            alpha[i] = i * i;
        }
        else
        {
            alpha[i] = i / 2.0;
        }

        cout << setw(5) << alpha[i] << " ";

        if(i % 10 == 0)
        {
            cout << endl;
        }
    }

    return 0;
}



The output looks like this:
1
2
3
4
5
6
0
1 4 9 16 25 36 49 64 81 100
121 144 169 196 225 256 289 324 361 400
441 484 529 576 12.5 13 13.5 14 14.5 15
15.5 16 16.5 17 17.5 18 18.5 19 19.5 20
20.5 21 21.5 22 22.5 23 23.5 24 24.5
Last edited on
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
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
   double alpha[50];
   int i;

    for(i = 0; i < 50; i++)
    {
        if(i < 25)
        {
            alpha[i] = i * i;
        }
        else
        {
            alpha[i] = i / 2.0;
        }

        cout << setw(5) << alpha[i] << " ";

        if((i+1) % 10 == 0)
        {
            cout << endl;
        }
    }

    getch();
    return 0;
}


As it is, I suspect the instructions intended you to write two separate functions as well as main; you've written one function, main, that does everything.

Why do you have conio.h, fstream and string in there?
Last edited on
Ok, I will look at it again.
Topic archived. No new replies allowed.