vector: entering weights

Write your question here.


Program goal:
User enters five numbers. Store the numbers in a vector of doubles. Output the vector's numbers on one line, each number followed by one space. (2 pts)

My issue:
Input and output additional numbers in the beginning.'

example:

Input -------------
236.00 89.50 142.00 166.30 93.00

Desired Output-------------
Enter weight 1:
236.0
Enter weight 2:
89.5
Enter weight 3:
142.0
Enter weight 4:
166.3
Enter weight 5:
93.0
You entered: 236.00 89.50 142.00 166.30 93.00

Total weight: 726.80
Average weight: 145.36
Max weight: 236.00

Current Output------------
Enter weight 1:
0.0
Enter weight 2:
0.0
Enter weight 3:
0.0
Enter weight 4:
0.0
Enter weight 5:
236.0
Enter weight 6:
89.5
Enter weight 7:
142.0
Enter weight 8:
166.3
Enter weight 9:
93.0
You entered: 0.00 0.00 0.00 0.00 0.00 236.00 89.50 142.00 166.30 93.00

Total weight: 726.80
Average weight is: 145.36
Max weight: 236.00
-----

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
56
57
#include <iostream>
#include <iomanip>             
#include <vector>
using namespace std;

int main() {
   const int inputNum = 5;
   vector<double>input(inputNum);
   double currInput;
   int i;
   int j;
   int l;
   double totalWeight;
   double averageWeight;
   double maxWeight;
   
   for (i = 0; i < inputNum; ++i){
      cin >> currInput;
      input.push_back(currInput);
   }
   
   cout << fixed << setprecision(1); 

   for (i = 1; i < input.size(); ++i){
      cout << "Enter weight " << i << ":\n" << input.at(i) << endl;
   }
   
   cout << "You entered: ";
   
   cout << fixed << setprecision(2); 
   
   for (i = 0; i < input.size(); ++i){
      cout << input.at(i) << " ";
    }
   cout << endl << endl;
   
   
   totalWeight = 0;
   for (j = 0; j < input.size(); ++j){
      totalWeight += input.at(j);
   }
   cout << "Total weight: " << totalWeight << endl;
   
   averageWeight = totalWeight/ 5;
   
   cout << "Average weight is: " << averageWeight << endl;
   
   maxWeight = input.at(0);
   for (l = 0; l < input.size(); ++l){
      if ( input.at(l) > maxWeight){
         maxWeight = input.at(l);
      }
   }
   cout << "Max weight: " <<  maxWeight << endl; 
   
   return 0;
}
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
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int main() {
    const int inputNum = 5;
    vector<double>input; //(inputNum); // this prefills the vector hence the 0's
    double currInput;
//    int i; not a good idea for loops to be initialized this way
//    int j;
//    int l; l is easily confused with the number '1'
    double totalWeight;
    double averageWeight;
    double maxWeight;
    
    for (int i = 0; i < inputNum; ++i){
        cin >> currInput;
        input.push_back(currInput);
    }
    
    cout << fixed << setprecision(1);
    
    for (int i = 0; i < input.size(); ++i)
    {
        cout << "You entered at " << i << ": " << input.at(i) << endl;
    }
    
    
    totalWeight = 0;
    for (int j = 0; j < input.size(); ++j){
        totalWeight += input.at(j);
    }
    cout << "Total weight: " << totalWeight << endl;
    
    averageWeight = totalWeight/ 5;
    
    cout << "Average weight is: " << averageWeight << endl;
    
    maxWeight = input.at(0);
    for (int l = 0; l < input.size(); ++l){
        if ( input.at(l) > maxWeight){
            maxWeight = input.at(l);
        }
    }
    cout << "Max weight: " <<  maxWeight << endl;
    
    return 0;
}



1 2 3 4 5 
You entered at 0: 1.0
You entered at 1: 2.0
You entered at 2: 3.0
You entered at 3: 4.0
You entered at 4: 5.0
Total weight: 15.0
Average weight is: 3.0
Max weight: 5.0
Program ended with exit code: 0
Last edited on
OP. L8 defines a vector that has 5 elements.

L19 - pushes a new value to the back of the vector - ie after the existing 5 elements.

Also, total weight can max weight can be calculated within the input loop.

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

int main() {
    constexpr size_t inputNum {5};
    std::vector<double>input;
    double totalWeight {};
    double maxWeight {};

    std::cout << "Please enter " << inputNum << " weights\n";

    for (size_t i {}; i < inputNum; ++i) {
        double currInput {};

        std::cout << "Enter weight " << i + 1 << ": ";
        std::cin >> currInput;
        input.push_back(currInput);
        totalWeight += currInput;
        if (currInput > maxWeight)
            maxWeight = currInput;
    }

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

    std::cout << "\nYou entered ";

    for (const auto& n : input)
        std::cout << n << ' ';

    std::cout << "\n\nTotal weight: " << totalWeight << '\n';
    std::cout << "Average weight is: " << totalWeight / inputNum << '\n';
    std::cout << "Max weight: " << maxWeight << '\n';
}

Topic archived. No new replies allowed.