Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Calculate an Array of Voltages Using Arrays for Currents and Resistances

% cat volts.cpp
// one-dimensional array program - calculate V = IR
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   const int MAX = 20;
   int i, NUM_VOLTS;

   double current[MAX], resistance[MAX], volts[MAX];

   cout << "Enter the number of volts calculations (<=20): ";
   cin >> NUM_VOLTS;

   for (i = 0; i < NUM_VOLTS; i++ )
   {
      cout << "Enter a current value:";
      cin >> current[i];

      cout << "Enter a resistance value:";
      cin >> resistance[i];

   }

   // calculate the volts

   for (i = 0; i < NUM_VOLTS; i++ )
   {
       volts[i] = current[i] * resistance[i];
   }

   // display results

   cout.setf(ios::fixed);
   cout.setf(ios::showpoint);
   cout.precision(1);

   cout << "\nCurrent     Resistance     Volts" << endl;

   for (i = 0; i< NUM_VOLTS; i++ )
   {
     cout << setw(6) << current[i] << setw(16) << resistance[i]
          << setw(10) << volts[i] << endl;
   }
}

% g++ volts.cpp
% a.out
Enter the number of volts calculations (<=20): 5
Enter a current value:2.3
Enter a resistance value:35.7
Enter a current value:4
Enter a resistance value:40
Enter a current value:7.9
Enter a resistance value:25.0
Enter a current value:5
Enter a resistance value:87
Enter a current value:2.5
Enter a resistance value:30.5

Current     Resistance     Volts
   2.3            35.7      82.1
   4.0            40.0     160.0
   7.9            25.0     197.5
   5.0            87.0     435.0
   2.5            30.5      76.2

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming