Array

Im hoping someone can help me out here and check my code for me i ran it through the cengage checks but i get everything right but failed to declare and initialize the alpha array to a size of 50 elements. Please help.

Here is the question and my code

Write a C++ program that declares an array alpha of 50 components of type double. Initialize the array so that the first 25 components are equal to the square of the index variable (the position that element will occupy), and the last 25 components are equal to three times the index variable. Output the array so that 10 elements per line are printed.

For the number at index 5, the value would be 25, which is 5 squared. The 25th index would hold a value of 75, which is 3 * 25.

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 arr[50]; 
    
    for(int i = 0; i < 50; i++)
    {
            if (i < 25)
            {
               arr[i] = i*i; 
            }
            else
            {
                arr[i] = 3*i;
            }
    }
    
    for (int i = 0; i <50; i++)
    {
        cout << arr[i] << " ";
        if((i+1) %10 == 0) 
        {
                 cout << endl; 
        }
    }
    
    return 0;
}
So I'm guessing the array is supposed to be called "alpha", which you did not do, and it should be initialized, which you did not do.
All you need to do is change line 12 to
(i+1)*(i+1)
and then your output is as follows;
1 4 9 16 25 36 49 64 81 100 
121 144 169 196 225 256 289 324 361 400 
441 484 529 576 625 75 78 81 84 87 
90 93 96 99 102 105 108 111 114 117 
120 123 126 129 132 135 138 141 144 147 


Here is an example of the exact same thing, just done a little different;

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 (void) {
  double alpha [50];
  
  for (int x = 0; x < 50; x++) {
    double value;
    
    if (x < 25)
      value = (x+1)*(x+1);
    else
      value = x*3;
      
    if (!(x%10))
      cout << "\n\t";
    else if (!(x%25))
      cout << "\n-----\n\t";
        
    cout << " [" << setw (2) << x+1 << "]" << setw (4) << value;
    alpha [x] = value;  
  }
  
  cout << "\n\n" << endl;
  return EXIT_SUCCESS;
}


The result is;


	 [ 1]   1 [ 2]   4 [ 3]   9 [ 4]  16 [ 5]  25 [ 6]  36 [ 7]  49 [ 8]  64 [ 9]  81 [10] 100
	 [11] 121 [12] 144 [13] 169 [14] 196 [15] 225 [16] 256 [17] 289 [18] 324 [19] 361 [20] 400
	 [21] 441 [22] 484 [23] 529 [24] 576 [25] 625
-----
	 [26]  75 [27]  78 [28]  81 [29]  84 [30]  87
	 [31]  90 [32]  93 [33]  96 [34]  99 [35] 102 [36] 105 [37] 108 [38] 111 [39] 114 [40] 117
	 [41] 120 [42] 123 [43] 126 [44] 129 [45] 132 [46] 135 [47] 138 [48] 141 [49] 144 [50] 147


I added a break at 25th element just for a little more clarity, but otherwise your code is perfectly legitimate except for the error at line 12.
Topic archived. No new replies allowed.