nikoaha / portfolio

0 stars 0 forks source link

Second year studies: programming projects (C/C++) #4

Closed nikoaha closed 5 years ago

nikoaha commented 6 years ago
// Niko Haapalainen 1706789
// C++ programming in Smart Systems, period 2, 2018
// Exercise 1

/**
The program asks the user to print the wanted amount 
of randomly generated numbers and eventually calculates
the mean of the generated numbers.
**/

/** START **/

#include <iostream>
#include <cstdlib>

using namespace std;

int mean(int *array, int n)
{
    int sum = 0;
    int thisMean = 0;

    // Finds the sum portion for the calculation
    for (int i = 0;i<n;i++)
    {
        sum = sum + array[i];
    }

    // formula is mean = (sum / quantity)
    thisMean = sum / n;

    return thisMean;
}

int main() {
    int size = 0;
    int thisMean = 0;

    // User input for the size
    cout << "Enter a fine amount of numbers [1-100]: ";
    cin >> size;

    // Allocates a block of memory of a 100 slots
    int *p = new int[100];

    // Fills the block of memory with random numbers ranging 0-99
    for (int j=0 ; j<size ; j++) {
        p[j] = rand() % 100; 
    }

    // ...and prints them
    for (int j = 0; j < size; j++) {
        cout << p[j] << "\n";
    }

    // Calls the 'mean' function 
    // Calculates and prints the result
    thisMean = mean(p, size);

    // Prints the result
    cout << "The mean of these numbers: " << thisMean;

    // Frees the data of the allocated memory space
    delete[] p;

    // To prevent the console from closing 
    system("pause");
    cout << endl;

    return 0;
}

/** END **/

OUTPUT: Enter a fine amount of numbers [1-100]: 5 41 67 34 0 69 The mean of these numbers: 42

nikoaha commented 6 years ago
// Exercise 5

/**
This program looks up to the weather.xml file
and prints the wanted inner text from inside the tags.

The wanted text for this program from the .xml file are...

                ...
            <suggested_pickup_period>60</suggested_pickup_period>
  this ->   <location>New York/John F. Kennedy Intl Airport, NY</location>
            <station_id>KJFK</station_id>
  this ->   <temp_c>25</temp_c>
                ...
**/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string find_field(string page, const char text[50])
{
    string string1 = page;
    string str = text;
    char leftover = '>';

    // cut </tag> and after
    size_t found1 = string1.rfind(str);
    if (found1 != std::string::npos) {
        string1.erase(found1-2, string1.length());
    }

    // cut <tag> and before
    size_t found2 = string1.rfind(str);
    if (found2 != std::string::npos) {
        string1.erase(0, found2 + str.length() + 1);
    }

    // if leftovers occur, cut them too
    size_t found3 = string1.rfind(leftover);
    if (found3 != std::string::npos) {
        string1.erase(0, found3+1);
    }

    return string1;
}

int main() {
    string page, line, location, temperature;
    ifstream inputFile("weather.xml");

    while (getline (inputFile, line)) {
        page.append(line);
        line.erase();
    }

    location = find_field(page, "location");
    temperature = find_field(page, "temp_c");

    cout << "Location: " << location << endl;
    cout << "Temperature: " << temperature << endl; 

    system("pause");
}

OUTPUT: Location: New York/John F. Kennedy Intl Airport, NY Temperature: 25