Use an array to calculate - C++ Data Type

C++ examples for Data Type:Array

Description

Use an array to calculate

Demo Code

#include <iostream> 
 
int main() //w  ww .  j  av a 2  s  .  c  om
{ 
    float goal[4]; 
    goal[0] = 0.9; 
    goal[1] = 0.7; 
    goal[2] = 0.5; 
    goal[3] = 0.25; 
    float weight, target; 
 
    std::cout << "Enter current weight: "; 
    std::cin >> weight; 
    std::cout << "\nEnter goal weight: "; 
    std::cin >> target; 
    std::cout << std::endl; 
  
    for (int i = 0; i < 4; i++) 
    { 
          float loss = (weight - target) * goal[i]; 
          std::cout << "Goal " << i << ": "; 
          std::cout << target + loss << std::endl; 
    } 
 
    return 0; 
}

Result


Related Tutorials