Array Problem

Hi,

I was wondering if anyone could help me? I'm having trouble with using a function to square values of an array. When I call the function, all I'm getting is zeros. Any help would be appreciated!

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//Function prototype
void getVrms(double voltsArray[24]);

int main()
{
//Variables defined
double vPeak = 0;
double vRMS = 0;
double conversion = 3.1415 / 180;
double arrayVoltage[24] = { 0.0 };

//Prompts user to enter the Peak Voltage they'd like to convert to RMS Voltage
cout << "Please enter the Peak Voltage you'd like to convert to RMS Voltage: ";
cin >> vPeak;
cout << endl;

//Loop generating voltages every 15 degrees based on Vpeak
for (int sub = 0; sub < 1; sub++)
{
for (int degree = 15; degree < 375; degree += 15)
{
arrayVoltage[sub] = vPeak * sin(degree * conversion);
cout << fixed << setprecision(3);
cout << arrayVoltage[sub] << "\t";
}cout << "\n" "\n";
}

getVrms(arrayVoltage);

system("pause");
return 0;
}//End of main functoin

//Function definition
void getVrms(double voltsArray[24])
{
double voltagesSquared[24] = { 0.0 };

for (int row = 0; row < 24; row++)
{
voltagesSquared[row] = pow(voltsArray[row], 2);
cout << fixed << setprecision(3);
cout << voltagesSquared[row] << "\t";
}cout << "\n" "\n";
}
Last edited on
closed account (SECMoG1T)
here is your problem

1
2
3
4
5
6
7
8
9
10
11
for (int sub = 0; sub < 1; sub++)///this loop runs only once when sub == 0.
    {
        for (int degree = 15; degree < 375; degree += 15)
        {
            arrayVoltage[sub] = vPeak * sin(degree * conversion); ///you're basically overwriting arayVoltage[0]; every time.
            cout << fixed << setprecision(3);
            cout << arrayVoltage[sub] << "\t";
        }
        cout << "\n" "\n";
    }
Any tips as to how I could fix the issue? I just want to display 24 values and square those values and display them.

Thanks in advance!
closed account (SECMoG1T)
replace those two loops with this:

1
2
3
4
5
6
7
//Loop generating voltages every 15 degrees based on Vpeak
    for (int degree = 15,index = 0;    degree < 375;    degree += 15, index++)
    {
        arrayVoltage[index] = vPeak * sin(degree * conversion);
        cout << fixed << setprecision(3);
        cout <<arrayVoltage[index] << "\t";
    }
Topic archived. No new replies allowed.