for (loop) Array[issue] C++

Good Evening guys,

I am trying to figure out how to make this array work, ive tried endl; and break; in the for loop outside of the four loop and no matter what it wont let me submit past myArray[0]. also the tabular table is not communicating with my array and the product its just not working. i would appreciate any references or any ideas to help me solve it.




   Fill the array with decimal values!

Enter array element 0:
Enter array element 1:
Enter array element 2:
Enter array element 3:

The Array is now filled as follow...

subscript       Element  //depends on number user inputs
0                  _
1                  _
2                  _
3                  _

the Product of the array element is:000.000>

...............................................................................
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
//fourdecimal loop array  

#include<iostream>
#include<iomanip>
using namespace std;



int main()
{
    
    const int SIZE_ARRAY = 5;
    int myArray[ SIZE_ARRAY ];
    //title
{
        cout<<"\n\t  Fill The Array with Decimal values.\n\n";
}
    

    // initialize prompt for array [0-4]
    for ( int i = 0;i <= SIZE_ARRAY;i++ )
     
    {
    cout<<"Enter Array Element"<<i<<":";
    cin>> myArray[i];
    }
    cout<<endl;
//end for
    
//middle title before the tabular format
{
    cout<<"The table is now filled as follow..."<<endl;
}

{
    // output contents of array  in tabular format
    cout << "\n\nSubscript" << setw( 13 ) << "Element" << endl;
    for ( int i =0; i < SIZE_ARRAY; i++ )
        cout << setw( 5 ) << i << setw( 14 ) << myArray[i] << endl;
}

// attempting to calculate product of array[above]=total
double product = 0.0;
product = myArray[i];
double total= 0.0;
product= total;
    
cout << fixed << setprecision(3);
        cout<< "The Product of the Array elements is:"<<total<<endl;

    system("PAUSE");
    return 0;  
    
} // end main 

Last edited on
Does this solve your problems? I only made a couple of changes. See the comments in the code below.

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
//fourdecimal loop array  

#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    double product = 1.0;        //added this variable here
    const int SIZE_ARRAY = 5;
    int myArray[ SIZE_ARRAY ];
    //title
{
        cout<<"\n\t  Fill The Array with Decimal values.\n\n";
}
    

    // initialize prompt for array [0-4]
    for ( int i = 0;i < SIZE_ARRAY;i++ )        //changed this to be just  i < SIZE_ARRAY, not i <+ ...
     
    {
    cout<<"Enter Array Element"<<i<<":";
    cin>> myArray[i];
    product *= myArray[i];              //added this line here to calculate the product, I'm guessing this is how you want to calc product.
    }
    cout<<endl;
//end for
    
//middle title before the tabular format
{
    cout<<"The table is now filled as follow..."<<endl;
}

{
    // output contents of array  in tabular format
    cout << "\n\nSubscript" << setw( 13 ) << "Element" << endl;
    for ( int i =0; i < SIZE_ARRAY; i++ )
        cout << setw( 5 ) << i << setw( 14 ) << myArray[i] << endl;
}

// None of the commented out code below is needed

// attempting to calculate product of array[above]=total
//double product = 0.0;
//product = myArray[i];
//double total= 0.0;
//product= total;
    
cout << fixed << setprecision(3);
        cout<< "The Product of the Array elements is:"<<product<<endl;

    system("PAUSE");
    return 0;  
    
} // end main  
Topic archived. No new replies allowed.