Tag Archives: #WSQ07

Homework 7: Lists

This program will ask the user for 10 numbers and will be stored in a vector. Then, the user will be able to see the total of the sum, average and standard deviation of those numbers.

A vector is very easy to use, you have to asign a type to the vector and give it a dimension. Something like this “type Vector[dimension]”. To asign a value inside the vector, you give the number of the row, e.g. “Vector[2] = Example”. You must begin from cero (first row) and end in “dimension-1” (last row). Now with for’s we can access to each row one by one.

Knowing this, we can make the program easily. This is my code…

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
  float numbers[10];
  float X;
  float SUM=0, AVG, SD, VAR=0;
  cout << "\n\n";

  for (int c=0; c<10; c++)
  {
    cout << "Number " << c+1 << "= ";
    cin >> X;
    numbers[c] = X;
    SUM += numbers[c];
  }

  AVG = SUM/10;

  for (int c=0; c<10 ; c++)
  {
    VAR+= pow ((numbers[c] - AVG), 2);
  }
  SD = sqrt(VAR/10);

  cout << "\n\nSum = " << SUM << endl;
  cout << "Average = " << AVG << endl;
  cout << "Standard deviation = " << SD << endl;

  return 0;
}