Trying to apply conditionals.

I'm trying to create conditionals to make my program error free, and in the calculation function I seem to fail completely while applying them. I don't know any other, better way to apply them, but it seems this just doesn't work. If anyone doesn't understand what my program wants to do due to Lithuanian comments and couts, I'll edit it in English.

My goal is to, after the first error check in first function, that determines if the size entered in the txt file is more than 0, if not, it shows the error in ats.txt. Then go to the 2nd function, there check if the size is indeed as intended, is to check if the entered amount of columns and rows is correct, according to the size (it's a square matrix, so the size should be same as the amount of rows and columns), if that is incorrect it should also display an error-related message in the ats.txt file. So if both of those are correct, it should continue with the calculations and output everything into the ats.txt file.

Thank you in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <fstream>

void input(int& dydis, int matrix[100][100]);
void output(int& n, int& x, int& y);
void calc(int& dydis, int matrix[100][100], int& row, int& col, int& n, int& x, int& y);





int main()
{
    int matrix[100][100], dydis, y, x, row, col;
    int n = 1000000;

    // funkcijų kvietimai
    input(dydis, matrix);
    calc(dydis, matrix, row, col, n, x, y);
    output(n, x, y);

return 0;
}






void input(int& dydis, int matrix[100][100])
{
    std::ifstream fd("duomenys.txt"); //įvestis iš duomenys.txt
    fd >> dydis;
    if(dydis>0){
            for (int row = 0; row < dydis; row++)
            for (int col = 0; col < dydis; col++)
            fd >> matrix[row][col];
    }
    else{
        std::ofstream fr("ats.txt");
        fr << "Dydis negali buti maziau arba lygu vienam.";
    }
}


void calc(int& dydis, int matrix[100][100], int& row, int& col, int& n, int& x, int& y)
{
    while(dydis>0)
    {
        if(row==dydis-1 && col==dydis-1)
            {
        for (int row = 0; row < dydis; row++) { // surandami elementai po įstrižaine
        for (int col = 0; col < row; col++)
        if(n > matrix[row][col]){
        n = matrix[row][col]; //randamas mažiausias elementas

        x = col+1; // priskiriamos koordinatės
        y = row+1;
           }
        }
    }
    else{
    std::ofstream fr("ats.txt");
    fr << "Ivestas nepakankamas arba per didelis kiekis duomenu.";
    }
 }
}

void output(int& n, int& x, int& y)
{
    std::ofstream fr("ats.txt"); //isvestis į ats.txt
    fr << "Maziausias elementas po pagrindine istrizaine:\n" << n <<std::endl<<"Koordinatese:\n("<<x<<";"<<y<<")"<<std::endl;
}


Last edited on
I'm trying to create conditionals to make my program error free, and in the calculation function I seem to fail completely while applying them.

It often helps when asking questions to be more specific than "this doesn't work." What makes you think it doesn't work? What are the symptoms of the failure? Help others to help you.

One thing I noticed with a quick glance is that if the condition on line 48 is true, it will always be true, resulting in an infinite loop.
Last edited on
cire,
When I use this code specifically, it never actually calculates, even if enough data is put in into the data.txt. It just skips to the
1
2
3
4
  else{
    std::ofstream fr("ats.txt");
    fr << "Ivestas nepakankamas arba per didelis kiekis duomenu.";
    }


I can make it more consistent with do, while
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void calc(int& dydis, int matrix[100][100], int& row, int& col, int& n, int& x, int& y)
{

   do{
        for (int row = 0; row < dydis; row++) { // surandami elementai po įstrižaine
        for (int col = 0; col < row; col++)
        if(n > matrix[row][col]){
        n = matrix[row][col]; //randamas mažiausias elementas

        x = col+1; // priskiriamos koordinatės
        y = row+1;
           }
    }
   }
   while(row==dydis-1 && col==dydis-1);
}


However I am not experienced enough to know how to apply an "else" statement in this, to make it so if the "while" statement is false, it would output an error message to the .txt.

Also, to add to this, I'm not even sure how the data is even gathered from the .txt file, as I've just been taught this at school and it's still unclear.

I'm sorry for not being clear, I'm not very good at asking questions.
Last edited on
One more question, if I am doing this, when calling functions, should I also add conditionals there?

I realized that the functions will be called either way, if one or the other is true, and I am not too sure how to change that. Or am I misunderstanding?
Last edited on
In main line 14 you should initialize your variables before you pass them to your functions.
At the moment your program behaves totally random. It might also be helpful if you could tell us what the program is supposed to do.
Thomas1965,

The point of the program is to find the smallest element in an area under the main diagonal and find that element's coordinates in the array.

The duomenys.txt contents are as follows:

3
5 -4 4
7 5 8
5 1 8

So, the program finds that the smallest element is 1, in the coordinates 2;3.

However, due to my lack of knowledge how this information from duomenys.txt is applied (I think it's just line based or something, I don't know), I fail to create good conditionals, that would make this program error-free. So far I have initialized the values and will retry applying conditionals.
Last edited on
Here is a simple demo how to find the smallest element is 2d array.
Maybe you can adapt it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

using namespace std;

void find_smallest(int num_elems, int matrix[3][3], int& row, int& col, int& smallest)
{
  smallest = matrix[0][0];
  row = col = 0;

  for (int r = 0; r < num_elems; r++) 
  {
    for (int c = 0; c < num_elems; c++)
    {
      if(matrix[r][c] < smallest) // found a smaller value
      {
        smallest = matrix[r][c]; // store it
        row = r; // store row where we found it
        col = c; // store col where we found it
      }
    }
  }   
}

int main()
{
  int matrix[3][3] = 
  { 
    {5, -4, 4},
    {7, 5, 8},
    {5, 1, 8}
  };

  int smallest = 0, row = 0, col = 0;
   
  cout << "Searching for the minimum element in matrix....\n";
  find_smallest(3, matrix, row, col, smallest);
  cout << "Smallest element is " << smallest << "\nand is located at "
         << "Row " << row << " Col " << col << "\n\n";  
}

	
Topic archived. No new replies allowed.