简体   繁体   中英

How to take inputs from array and input it into an equation in C++?

My goal is to try and create a program that takes in grades in percents and multiply it with their weight value (either in decimal form or percent form). The equation is basically:

Overall grade = (grade1*weightInDecimal1)+(grade2*weightInDecimal2)+(grade3*weightInDecimal3)+...

or

Overall grade = (grade1*weight%1)+(grade2*weight%2)+(grade3*weight%3)+...


Is there a way to store the inputs and then recall it later in the code? Or possibly a more efficient way?

I also want to try and make a dynamic array. I want to make a program that asks the user for how many assignments they have and makes an array based on that. That way it's not stuck at 4 assignments

#include <string>
#include <iostream>
using namespace std;
int main() {
    int numbers[4][2];
    for(int i=0;i<4;i++)
    {
       cout<<"Grade #"<<i<<endl;
       cin>>numbers[i][0];
       cout<<"Weight for grade #"<<i<<":"<<endl;
       cin>>numbers[i][1];
    }

    for (int i = 0; i<4; i++)
    {
        cout << "|" << numbers[i][0]*numbers[i][1]<< "|";
    }
    system ("PAUSE");
return 0;
}

This is what structs are for

#include <string>
#include <iostream>
#include <array>
using namespace std;

struct entry {
    int grade;
    int weight;
    int gradeWeight; //grade*weight 
};

int main() {
    array<entry,4> numbers;
    for(int i=0;i<numbers.max_size();i++)
    {
       cout<<"Grade #"<<i<<endl;
       cin>>numbers[i].grade;
       cout<<"Weight for grade #"<<i<<":"<<endl;
       cin>>numbers[i].weight;
    }

    for (int i = 0; i<numbers.max_size(); i++)
    {
        numbers[i].gradeWeight = numbers[i].grade*numbers[i].weight;
        cout << "|" << numbers[i].gradeWeight << "|";
    }
    system ("PAUSE");
    return 0;
}

This way you can also increase the amount of numbers by just increasing the array size.

There are many reasons to avoid using arrays (dynamic or otherwise). See for example Stroustrup's FAQ entry What's wrong with arrays? As Greg suggests in the comments you will most likely write better quality code if you use a container like std::vector .

If you can calculate or input the size of your container before allocating it you can (if you wish) pass that size to the constructor ...

auto syze{ 0 };
auto initialValue{ 0 };

// calculate or input syze
. . .

// dynamically allocate an "array"
// of ints and an "array" of floats
std::vector<int> grades(syze, initialValue);
std::vector<float> weights(syze, initialValue);

On the other hand if you prefer to use a container that dynamically grows to hold data as it arrives you can do that too...

// dynamically allocate an empty "array"
// of ints and an empty "array" of floats
std::vector<int> grades;
std::vector<float> weights;

while (...condition...)
{
    std::cin >> g;   // input a grade ...
    std::cin >> w;   // ... and a weight

    // grow the containers by adding 
    // one item at the end of each
    grades.emplace_back(g);
    weights.emplace_back(w);
}

Update

I should have pointed out how to calculate the result from the two vectors. You can calculate your overall grade with just one more line of code by using std::inner_product from the STL's <numeric> header. Note that in the code below the last argument is 0.0 (rather than just 0) so that std::inner_product returns a double rather than an int . That avoids any risk of float values being truncated to int (and avoids some pretty ugly warnings from the compiler).

auto overallGrade = std::inner_product(grades.begin(), grades.end(), weights.begin(), 0.0);

As pointed by others, if you ask the user for how many assignments they have, std::array is a wrong container because it's dimension is fixed at compile time.

As others, I discurage the use of direct memory allocation but the use of std::vector to manage it.

I just suggest the use of reserve() (method of std::vector ), when you know how many assignments.

The following is a full example using, instead a couple of std::vector of int , a single std::vector of std::pair<int, int>

#include <utility>
#include <vector>
#include <iostream>

int main()
 {
   int                              valG, valW;
   std::size_t                      dim;
   std::vector<std::pair<int, int>> vec; 

   std::cout << "How many grade/weight couples? ";
   std::cin  >> dim;

   vec.reserve(dim);

   for ( auto i = 0U ; i < dim ; ++i )
    {
      std::cout << "Grade #" << i << "? " << std::endl;
      std::cin  >> valG;
      std::cout << "Weight for grade #" << i << "? " << std::endl;
      std::cin  >> valW;

      vec.emplace_back(valG, valW);
    }

    for ( auto const & p : vec )
       std::cout << '|' << (p.first * p.second) << '|';

    std::cout << std::endl;

    return 0;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM