Solutions

Arrays

Exercise 1:

#include <iostream>
#include <vector>
using namespace std;

int main()
{    
    vector<double> data(7);
    cout << "Type in 7 numbers: " << endl;
    for (int i=0; i<7; i++)
        cin >> data[i];    
        
    for(int i=1; i<7; i++)
        {
        if (data[i] < 10)
            cout << data[i] << endl;
        }
}

Exercise 2:

#include <iostream>
#include <vector>
using namespace std;

int main()
{    
    vector<double> data(7);
    cout << "Type in 7 numbers: " << endl;
    for (int i=0; i<7; i++)
        cin >> data[i];    
    
    double min = data[0];
    for(int i=1; i<7; i++)
        {
        if (data[i] < min)
            min = data[i];
        }
        
    for(int i=1; i<7; i++)
        {
        if (data[i] != min)
            cout << data[i] << endl;
        }
}

Exercise 3:

#include <iostream>
#include <vector>
using namespace std;

int main()
{    
    vector<double> data(7);
    cout << "Type in 7 numbers: " << endl;
    for (int i=0; i<7; i++)
        cin >> data[i];    
    
    double min = data[0];
    for(int i=1; i<7; i++)
        {
        if (data[i] < min)
            min = data[i];
        }
    
    double sum = 0;
    for(int i=1; i<7; i++)
        {
        if (data[i] != min)
            sum += data[i];
        }
                 
    cout << "The sum of input values not equal to " << endl 
         << "the smallest input value is " << sum << endl;
}

Exercise 4:

#include <iostream>
#include <vector>
using namespace std;

int main()
{    
    cout << "Please type in a natural number n: " << endl;
    int n;
    cin >> n;
    vector<double> data(n);
    cout << "Type in " << n << " numbers: " << endl;
    for (int i=0; i<n; i++)
        cin >> data[i];    
    
    double sum = 0;
    for(int i=1; i<n; i++)
        {
        if (data[i] < data[n-1])
            sum += data[i];
        }
                 
    cout << "The sum of input values that are smaller " << endl 
         << "than the last value is " << sum << endl;
}

Exercise 5:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<double> inputValues(8);
    cout << "Type in 8 numbers: " << endl;
    for(int i=0; i<8; i++)
        cin >> inputValues[i];

    int minI = 0;
    for(int i=1; i<8; i++)
        {
        if (inputValues[i] < inputValues[minI])
            minI = i;
        }

    cout << "The smallest number is " << inputValues[minI] << endl;
    cout << "at position: " << minI+1 << endl;
}

Back to exercises