Help with for loop with vectors

I'm trying to make a program to help me with my material science homework. Pretty much I just want to calculate the normal stress given a set of values for load and elongation. For some reason my code isn't working and I can't figure out why.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>

using namespace std;
#include <vector>

int main()
{

    vector<float> load = {0,2.50,6.50,8.50,9.20,9.80,12.0,14.0,14.5,14.0,13.2};
    vector<float> elongation = {0,0.0009,0.0025,0.0040,0.0065,0.0098,0.0400,0.1200,0.25,0.35,0.47};
    const float area = 3.14*(.503)*.503/4;
    for (int i=0;i<=12;i++)
    {
        float strain = load[i]/area;
        cout << "Strain " << i << " = " << strain[i]
    }

    return 0;
}
strain has been declared as a float (line 14) yet you are trying to access it as if it were an array (line 15) with the subscript operator []. Try instead:
 
cout << "Strain " << i << " = " << strain << '\n';
Firstly, it looks like there are 11 items in vector<float> load.
You wont want to iterate into load[12], that's out of bounds. You can iterate through it like this
1
2
3
for (int i = 0; i < load.size(); ++i) {
  ...
}

ignoring any formatting differences between your style and mine.

You also have Strain[i], but Strain isn't an array/vector.

Even better is
1
2
3
4
5
6
7
int i = 0;
float strain = 0;
for (float & element : load) {
  strain = element / area;
  cout << "Strain " << i << " = " << strain << endl;
  i++;
}

Or maybe it isn't. Might depend on who you ask.
Hello @zachdr1

You have a number of problems - some C++ ... and some materials science.

C++ first:
Your loop will try to run 13 times: i=0,1,2,....12. However, your vector only has 11 elements. Change that 12 to a 10 in the for() statement. In fact it would probably be better as
for ( int i=0; i< load.size(); i++ )
which will automatically deal with the size of your vector.

What you are calling strain (but see below) is just a scalar, so doesn't have index i. Remove the
[i]
and replace it with
<< endl;

Make sure it ends in a semicolon.


Materials science:
What you are calling "strain" is actually "stress" - please correct this before your teacher sees it!
stress = force / area.
strain = elongation / original length.

Maybe you want to look at the units involved as well?



Last edited on
Further to the other replies:

Avoid using float, it's precision is easily exceeded.

From a style point of view, I always put digits before and after the decimal point - so 0.503 not .503 and 4.0 not 4

pi is a constant, so make it so - and you can do better than 2 dp :+)

Instead of magic numbers like 0.503 make it a const variable:

const double Diameter = 0.503;

Consider writing your results to std::vector<double> Stress

JayhawkZombie's idea of using a range based for loop is a really good one, however I learnt this forward reference version the other day from TwilightSpectre & JLBorges. It's pretty fancy and probably way ahead of what your teacher might expect, but there you go :+)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::vector<double> StressData;
// calc the values
// http://en.cppreference.com/w/cpp/language/range-for
for ( auto&& LoadValue : load) { // fwd reference with auto type deduction - could have double&& instead
  StressData.push_back(LoadValue / area);
}

// make a const ref so we can't change the values inside the for loop
const std::vector<double>& StressDataConst = StressData;

// print the values
for (auto&&  StressValue : StressDataConst) { // can't have const auto&&
     std::cout << StressValue << "\n";
}


Even better make the printing a function:

1
2
3
4
5
// best solution
void print(const std::vector<double>& StressData) {
       for (auto&&  StressValue : StressData) {
            std::cout << StressValue << "\n";
}


Having the function is a much better idea: programs should be made from functions; it's more consistent with the usual practise of passing arguments by const reference (as opposed to having to make a const reference like in the first example)

Some other links that JLBorges posted the other day:

http://en.cppreference.com/w/cpp/language/reference
http://en.cppreference.com/w/cpp/utility/forward
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm

Good Luck !!



Topic archived. No new replies allowed.