Array

I am in a C++ programming class, but I am completely lost when it comes to compiling and executing programs, please help!

Write a program that declares an array of 50 integers. Initialize the array so that the first 25 elements are equal to the square of the index variable, and the last 25 elements are equal to three times the index variable. (For example, alpha[5]=25, alpha[31]=93.)
Output the array so that 10 elements per line are printed.

Thanks for your help!
Don't worry about compiling and running the program just yet, try actually writing the program first. ;)
Please don't just copy and paste this as your own. If you are confused about how parts of this work, please ask. The point is that you learn :)

Solution:

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

using namespace std;

int main()
{
    int numbers[50];

    for(int i=0; i < 50; i++)
    {
        numbers[i]= i*i;
        if(i >25)
        {
            numbers[i]= 3*i;
        }
    }

    for(int i=0; i < 50; i++)
    {
        if(i%10 == 0)
        {
            cout << endl;
        }
        cout << setw(3) << numbers[i] << " ";
    }

}
Please don't do people's homework for them. Try to get him/her to attempt a solution, so that we can help them with any problems they encounter. That way, they are much more likely to actually learn something.
Last edited on
Topic archived. No new replies allowed.