nikoaha / portfolio

0 stars 0 forks source link

Laitteet (Systems) -course's labworks (C) #1

Closed nikoaha closed 6 years ago

nikoaha commented 6 years ago

This program multiplies or sums two given numbers and prints the result. This program is looped until the user quits.

/* START OF FILE */

#include "project.h"
#include <stdio.h>

int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    UART_1_Start();

    int chosenNumber = 0;
    float operandOne = 0;
    float operandTwo = 0;

    while (chosenNumber != 4) {
        printf("\nSelect operation:\n");
        printf("1) enter operands\n");
        printf("2) multiply operands\n");
        printf("3) add operands\n");
        printf("4) quit\n");

        scanf("%d", &chosenNumber);

        switch (chosenNumber) {
            case 1:
                printf("Enter operand 1: ");
                scanf("%f", &operandOne);
                printf("Enter operand 2: ");
                scanf("%f", &operandTwo);
                break;

            case 2:
                printf("Result = %.2f\n", operandOne*operandTwo);
                break;

            case 3:
                printf("Result = %.2f\n", operandOne+operandTwo);
                break;

            case 4:
                printf("You quit.\n");
                chosenNumber = 4;
                break;

            default:
                printf("Invalid choice.\n");
                break;
        }
    }

    for(;;)
    {
        /* Place your application code here. */
    }
}

/* [] END OF FILE */
nikoaha commented 6 years ago

This program takes in two floats and prints a relative number of the given numbers.

/* START OF FILE */

#include "project.h"
#include` <stdio.h>

void scaler(float *num1, float *num2)
{
    if (*num1 < *num2) {
        *num1 = *num1 / *num2;
        *num2 = 1.0;
    }
    if (*num1 >= *num2) {
        *num2 = *num2 / *num1;
        *num1 = 1.0;
    }
}

int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    UART_1_Start();

    float num1, num2;
    int b1 = 0;

    while (b1 == 0) {
        printf("Enter 1. number: \n");
        scanf("%f", &num1);

        if (num1 >= 0) {
            b1++;
        } else {
            printf("Number must be positive!\n");
        }
    }

    while (b1 == 1) {
        printf("Enter 2. number: \n");
        scanf("%f", &num2);

        if (num2 >= 0) {
            b1++;
        } else {
            printf("Number must be positive!\n");
        }
    }

    if (b1 == 2) {
        scaler(&num1, &num2);
        b1++;
    }
    printf("Scaled values are: \n");

    printf("1: %.5f\n", num1);
    printf("2: %.5f\n", num2);

    for(;;)
    {
        /* Place your application code here. */
    }
}

/* [] END OF FILE */
nikoaha commented 6 years ago

The user enters five float numbers in an array and the program prints the smallest given number.

/* START OF FILE */

#include "project.h"
#include <stdio.h>

int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    UART_1_Start();

    float numbers[5];
    int i = 0;
    float smallestNumber = 0;

    printf("\nEnter five numbers: \n");

    for (i=0 ; i<5 ; i++) {
        printf("Enter number %d: ", i+1);
        scanf("%f", &numbers[i]);
    }

    smallestNumber = numbers[0];

    for (i=0 ; i<5 ; i++) {
        if (numbers[i] < smallestNumber) {
            smallestNumber = numbers[i];
        }
    }

    printf("The smallest number you entered was: %.3f\n", smallestNumber);

    for(;;)
    {
        /* Place your application code here. */
    }
}

/* [] END OF FILE */