Lab 12 – 2017

Test 2 will have 2 tasks:

  1. Array task (6p)
  2. Code the algorithm (9p)

You should be able to split code into functions.

Array task

Task 1

Massiiv B on 4x4 2-dimensionaalne massiiv, mille väärtused sisestatakse algväärtustamise kaudu.
Leia elementide korrutised, kus elementideks on peadiagonaali kohal olevad negatiivsed elemendid massiivis B. Kuva saadud tulemused ekraanil.
Korduste programmeerimisel kasutada for lauset.


The matrix B is a 4x4  2-dimensional array with the  values initialized on its definition. 
Find a product of negative elements  above the main diagonal  in matrix B   and output it on the screen.  For loop use while sentence.

Task 2

Kirjuta programm, mis leiab maatriksi A[n][m]   tulpade  negatiivsete elementide summad(üks summa iga tulba kohta) ja väljastab need ekraanile. Maatriks tuleb sisestada algväärtustamise kaudu, võttes n=4 ja m=3.
NB! Korduse programmeerimisel kasutada while… lauset!

Write a program, which will find sum of columns in array A[n][m] (one sum per column) and outputs the results on screen.
Array values must be entered by initialization, where the n = 4 and m = 3.
NB! For loops, use while statement!

 

Code the algorithm

Task 1

Task 2:

Task 3:

Task 4:

Split into functions:

#include <stdio.h> // standard input output

#define n 3
#define m 3

int main(void) {
    int i, j;
    float arv = 1;
    int B[n][m] = {
        {-1, -2, 9},
        {-2, -4, 7},
        {-5, -7, -8}
    };

    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            if ((j < i) && (B[i][j] < 0)) {
                arv = B[i][j] * arv;
            }
        }
    }
    printf("product of negative elements under the main diagonal %5.2f\n", arv);
    printf("push enter");
    getchar();
    
    return 0;
}