Arrays

How would I write an array that uses a constant measurement, increments over 50 components, but then prints the components in incrementing intervals of 5?

Ex: const double MEASUREMENT = 1.5; incrementing over 50 years.
Print newMeasurement for every 5 years until 50 years.
Hello Fayezilla,

Last night I saw this post. This morning I find that it has been reported. Unfortunately I do not know why.

In a different topic http://www.cplusplus.com/forum/beginner/271546/#msg1170794 you wrote:

Again I apologize for posting a question when I was unclear of the instructions.


So why have you not learned? This question is very confusing.

The word "components" may work, but it is not the proper term. When dealing with arrays, or something that has a base of an array, the more proper term is "element".

What you have posted is a bit unclear leaving a lot to guess work and interpretation. And my guess may not be what you want.

It should not matter what the setting is for loops and arrays should be covered early in class before you have to use them. Then the programs are built of of what you have learned.

It is easy to see that you need an array for this program. And if you do not want to hard code each element of the array you will need a for loop to do this for you.

I came up with this for a start. Sorry if I have done something that you have not learned about yet.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

int main()
{
    constexpr double MEASUREMENT{ 1.5 };
    constexpr int MAXSIZE{ 51 };

    double arr[MAXSIZE]{};

    std::cout << std::fixed << std::setprecision(2);

    for (int idx = 1; idx < MAXSIZE; idx++)
    {
        arr[idx] = arr[idx - 1] + MEASUREMENT;
    }

This is only part of the whole program, but it should give you an idea of where to start.

One big difference I did here was to make the array 1 larger than needed and not use element zero of the array except in this for loop. Your array would look something like this:

 0.  0.00
 1.  1.50
 2.  3.00
 3.  4.50
 4.  6.00
 5.  7.50
 6.  9.00
 7. 10.50
 8. 12.00
 9. 13.50
10. 15.00
11. 16.50
.
.
.
50. 75.00


Everything including the ". " is just a counting number to show the array elements.

By skipping element zero it makes the next for loop easier to work with. This is not the norm, but a special case.

Work up some code to finish the program and we can discus why it works or does not work.

Andy
const double MEASUREMENT = 1.5; incrementing over 50 years.
Print newMeasurement for every 5 years until 50 years.
This doesn't require an array because once you've computed the measurement for a year, you don't need to refer to it again. So are you required to use an array, or was that just how you planned to solve the problem?

In C++, "increment" has a specific meaning: "add one to something." Is that what you mean, or does the measurement increase by some other value?

Is Andy's output what you expect? If not, can you post a few lines of the output that you expect? Also the exact text of the problem would help. Sometimes there are small details that are easy to miss.
for loops can be written to do this.

for(int i = 1; i <= 50; i+=5) //1,6,11 ... is this what you want or do you want 1,5,10?
cout << i*1.5;

to get 1,5,10 there are various 'clever' ways but the simple one is
cout << 1.5
for(int i = 5; i <= 50; i+=5)
cout << i*1.5;

and you can use variables instead of actual numbers for all this of course, I used hard values just to show what is going on.

I advise printing the loop variable until you are happy with it (so you don't get some weird effect like the 1,6,11 series) and then finishing it up; this is critical for more complicated expressions for the loop variable (you can visualize +5 but it could have been i+= i or something hard to see)
Last edited on
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
#include <iostream>

int main()
{
   // set a constant of 50 years, make it unsigned (non-negative)
   const unsigned totalYears { 50 };

   // create an array of doubles to hold 50 years' worth of data
   double years[totalYears];

   // create create constant measurement figure
   const double MEASURMENT { 1.5 };

   // create a yearly summation variable, set to zero.
   double sum { 0.0 };

   // loop for 50 years, unsigned 'cuz can't be negative years. starts at zero
   for (unsigned loop { }; loop < totalYears; loop++)
   {
      // add the current sum for the year
      sum += MEASURMENT;

      // assign the current sum to the current year
      years[loop] = sum;

      // is this a 5th year increment?  If so, print
      // jigger the loop value to represent human readable year
      if ((loop + 1) % 5 == 0)
      {
         std::cout << "year " << loop + 1 << ": " << years[loop] << '\n';
      }
   }
}

A container really is not needed for this:
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
#include <iostream>

int main()
{
   // set a constant of 50 years, make it unsigned (non-negative)
   const unsigned totalYears { 50 };

   // create create constant measurement figure
   const double MEASURMENT { 1.5 };

   // create a yearly summation variable, set to zero.
   double sum { 0.0 };

   // loop for 50 years, unsigned 'cuz can't be negative years.
   for (unsigned loop { 1 }; loop < totalYears; loop++)
   {
      // add the current sum for the year
      sum += MEASURMENT;

      // is this a 5th year increment?  If so, print
      // jigger the loop value to represent human readable year
      if (loop % 5 == 0)
      {
         std::cout << "year " << loop << ": " << sum << '\n';
      }
   }
}

Thank you everyone. I am just learning about C++, this is my first class. I apologize again for any confusion in my questions.
This is what I have so far. The assignment requires an array, that is the chapter we are on.
My program only outputs the hard-coded cout statements.

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

using namespace std;

///Function prototype
void seaLevel(double list[], int YEARS, double& newLevel);
void printTable(double list[], int YEARS, double& newLevel);

///Global constant
    const double RISE = 1.5;

int main()
{

///Declare variables
    double total[50];
    double newLevel;
    int YEARS = 50;


    cout << "Year    Rise (in millimeters)" << endl;
    cout << "=============================" << endl;

    ///Call functions
    seaLevel(total, YEARS, newLevel);
    printTable(total, YEARS, newLevel);


    return 0;
}

void seaLevel(double list[], int YEARS, double& newLevel)
{
///Sea level is rising 1.5 mm per year.
    /// loop control, index of array
    int count;
    int interval = 5;

    /// initialize 50 years
    for (count = 2020; count < 50; count++)
        list[count] = RISE * interval;
}

void printTable(double list[], int YEARS, double& newLevel)
{
    ///print output table.
    for (int year = 2020; year < YEARS; year++)
    {
        cout << setw(10) << left << year + 5;
        cout << setw(10) << left << newLevel;
    }
    while (YEARS <= 50);
}


This is required output:

1
2
3
4
5
6
7
8
Year    Rise(in millimeters)
================
2020   1.5
2025     9
2030  16.6

.... and so on until
2070  76.9



I am probably still not understanding the what the assignments are asking so please bear with me.
for (count = 2020; count < 50; count++)
How often do you think this loop will be executed ?

for (int year = 2020; year < YEARS; year++)
How often do you think this loop will be executed ? if YEARS == 50 ?
Hello Fayezilla,

I think it is about time that you post the full requirements for the program, so everyone can stop guessing at what is needed and know what needs to be done.

I have taken your code and added some comments that should help:
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
#include <iostream>
#include <iomanip>

using namespace std;

///Function prototype
void seaLevel(double list[], int YEARS, double& newLevel);
void printTable(double list[], int YEARS, double& newLevel);

///Global constant
const double RISE{ 1.5 };
constexpr int YEARS{ 50 };  // <--- Changed and moved to here.

int main()
{
    ///Declare variables
    double total[50];  // <--- Should be initialized. Otherwise each element might contain "-9.2559631349317831e+64".
    double newLevel;  // <--- Should be initialized. And what is this for?

    std::cout << std::fixed << std::setprecision(2);  // <--- Added. only needs done once.

    cout << "Year    Rise (in millimeters)" << endl;
    cout << "=============================" << endl;

    ///Call functions
    seaLevel(total, YEARS, newLevel);    // <--- "newLevel" passed, but never changes its value.
    printTable(total, YEARS, newLevel);  // <--- "newLevel" passed, but its value has never changed and is never changed.


    return 0;
}

void seaLevel(double list[], int YEARS, double& newLevel)  // <--- Bad name. Looks more like it should be "loadArray". "newLevel" never used or changed.
{
    ///Sea level is rising 1.5 mm per year.
        /// loop control, index of array
    int count;  // <--- Should be defined in the for loop.
    int interval = 5;

    /// initialize 50 years
    for (count = 2020; count < 50; count++)  // <--- See Thomas1965's reply.
        list[count] = RISE * interval;
}

void printTable(double list[], int YEARS, double& newLevel)  // <--- "newLevel" used, but still contains a garbage value because it was never changed.
{
    ///print output table.
    for (int year = 2020, end = year + YEARS; year < end; year++)
    {
        cout << setw(10) << left << year + 5;
        cout << setw(10) << right << newLevel << '\n';  // <--- Added "right", removed "left".
    }
    //while (YEARS <= 50);  // <--- Endless while loop that does nothing, but does hang the program.
}

I made some changes to the "printTable" function to make it work. But it is still not right and needs some work.

It does produce this output now:

Year    Rise (in millimeters)
=============================
2025      -92559631349317830736831783200707727132248687965119994463780864.00
2026      -92559631349317830736831783200707727132248687965119994463780864.00
2027      -92559631349317830736831783200707727132248687965119994463780864.00
2028      -92559631349317830736831783200707727132248687965119994463780864.00
2029      -92559631349317830736831783200707727132248687965119994463780864.00
2030      -92559631349317830736831783200707727132248687965119994463780864.00
2031      -92559631349317830736831783200707727132248687965119994463780864.00
2032      -92559631349317830736831783200707727132248687965119994463780864.00
2033      -92559631349317830736831783200707727132248687965119994463780864.00
2034      -92559631349317830736831783200707727132248687965119994463780864.00
2035      -92559631349317830736831783200707727132248687965119994463780864.00


Not what you are looking for.

I can fix what you have done, but it will most likely not be correct until I understand better what the program should do.

Andy

Edit formatting.
Last edited on
Maybe this is what it's supposed to do???

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

using namespace std;

void seaLevel(double[], int, double);
void printTable(double[], int, int, int);

int main()
{
    int TIME_SPAN = 50;
    double level[TIME_SPAN];
    
    cout
    << "Year    Rise (in millimeters)\n"
    << "=============================\n";
    
    seaLevel(level, TIME_SPAN, 1.5);
    printTable(level, TIME_SPAN, 3, 2020);
    
    return 0;
}

void seaLevel(double lvl[], int no_yrs, double rise)
{
    double level = 0;
    for (int count = 0; count < no_yrs; count++)
    {
        level += rise; // CHANGE TO *= WITH START level = 1, not 0 ???
        lvl[count] = level;
    }
}

void printTable(double lvl[], int no_years, int freq, int start_yr)
{
    for (int year = 0; year < no_years; year += freq)
    {
        cout
        << setw(10) << left << start_yr + year
        << setw(10) << right << lvl[year] << '\n';
    }
}
Hello Fayezilla,

I was wondering about this:

Year    Rise(in millimeters)
================
2020   1.5
2025     9
2030  16.6

.... and so on until
2070  76.9


What is up with the ".6" and ".9"? Is that correct?

My output is:

              Rise
Year    (in millimeters)
=============================
2020          1.50
2025          9.00
2030         16.50
2035         24.00
2040         31.50
2045         39.00
2050         46.50
2055         54.00
2060         61.50
2065         69.00
2070         75.00


Since you are adding "1.5" you would only have ".00" or ".50".

Eventually this will help to better use the "iomanip" header file.
https://en.cppreference.com/w/cpp/header/iomanip

And if you shorten that to:
https://en.cppreference.com

You will find this a good place for help. That sight is kept updated whereas the site here is in dire need of updating.

Andy
Againtry, thank you that is exactly it.

I am sorry again for all of the confusion.
Handy Andy, thank you for keeping up with my question and working through it.
Topic archived. No new replies allowed.