Whats wrong?

Hello. I need to write program, which would find the lowest element of matrix, find the line in which this element is and count average of this line. So whats wrong with my program. Thanks for the help.

Calculations must be declared as fuction, intput and output should be in the main function.

Thanks for the help.

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
 #include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void calc(int n, int m, int a, int x[][10], double avrg, double sum, int min, int k);
int main(){
    int n, m;
    double x[10][10];
    int a;
    double avrg;
    ifstream is;
    is.open("Duom.txt");
    if(is.fail()){
                cout << "File not found" << endl;
                return -1;
                }
                is>>m>>n;
                cout << "Matrix measurements:" << setw(5) << n << setw(5) << m << endl;
                for (int i=0; i<n; i++){
                    for (int j=0; j<n; j++){
                        is>>x[i][j];
                        cout << "Matrix elemnt:(" << i+1 << ":" << j+1 << ")" << x[i][j] << endl;
                        }
                        }
                        is.close();
                        cout << "Matrix:" << endl;
                        for (int i=0; i<n; i++){
                            for (int j=0; j<m; j++){
                                cout << setiosflags(ios::fixed)<<setiosflags(ios::showpoint)<<setprecision(2)<< setw(12)<< x[i][j];
                                }
                                cout<<endl;
                                }
                                ofstream rez;
                                rez.open("rez.txt");
                                rez<<"The biggest element is in" << a << "line"<< endl;
                                if(avrg>0){
                                          rez<< a << "Line average is" << avrg << endl;
                                          }
                                          else
                                         {
                                          rez<<"Positive elements in" << a << "arent found" << endl;
                                          system("pause");
                                          return 0;
                                          }
                                          }
                                          
                                          void calc(int n, int m, int a, int x[][10], double avrg, double sum, int min, int k){
                                               min=x[0][0];
                                               for (int i=0; i<n; i++){
                                                   for (int j=0; j<m; j++){
                                                       if(min>x[i][j]){
                                                                       min=x[i][j];
                                                                       a=j;
                                                                       }
                                                                       }
                                                                       }
                                                                       for (int i=0; i<n; i++){
                                                                           for (int j=0; j<m; j++){
                                                                               if(a==j&&x[i][j]>0){
                                                                                                   k++;
                                                                                                   sum+=x[i][j];
                                                                                                   }
                                                                                                   }
                                                                                                   
                                                                                                   
                                                                                                   avrg=sum/k;
                                                                               
                                                                               
                                                                               }
                                                                       
So whats wrong with my program.

Your indentation for a start. What's the story with that indentation. How can you tell whether the scopes are correct?

I copied the code into an editor and corrected so I could see the code. What you posted is impossible.

You never call calc() to do the computation for you. So, in fact, you code does nothing but read Duon.txt and write rez.txt.

calc() takes too many parameters. It doesn't return any values it calculates.

The final double loop in calc() is unnecessary, as the point of the first double loop is to identify the row. So the second loop should just check that row, not the whole matrix again.

EDIT: Your code formatted.
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
76
77
78
79
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void calc(int n, int m, int a, int x[][10], double avrg, double sum, int min, int k);

int main(){
    int n, m;
    double x[10][10];
    int a;
    double avrg;

    ifstream is;
    is.open("Duom.txt");
    if (is.fail()){
        cout << "File not found" << endl;
        return -1;
    }

    is>>m>>n;
    cout << "Matrix measurements:" << setw(5) << n << setw(5) << m << endl;
    for (int i=0; i<n; i++){
        for (int j=0; j<n; j++){
            is>>x[i][j];
            cout << "Matrix elemnt:(" << i+1 << ":" << j+1 << ")" << x[i][j] << endl;
        }
    }
    is.close();

    cout << "Matrix:" << endl;
    for (int i=0; i<n; i++){
        for (int j=0; j<m; j++){
            cout << setiosflags(ios::fixed)
                <<setiosflags(ios::showpoint)
                <<setprecision(2)<< setw(12)
                << x[i][j];
        }
        cout<<endl;
    }

    ofstream rez;
    rez.open("rez.txt");
    rez<<"The biggest element is in" << a << "line"<< endl;
    if (avrg>0){
        rez<< a << "Line average is" << avrg << endl;
    }
    else
    {
        rez<<"Positive elements in" << a << "arent found" << endl;
        system("pause");
        return 0;
    }
}

void calc(int n, int m, int a, int x[][10], double avrg, double sum, int min, int k){
    min=x[0][0];

    for (int i=0; i<n; i++){
        for (int j=0; j<m; j++){
            if (min>x[i][j]){
                min=x[i][j];
                a=j;
            }
        }
    }

    for (int i=0; i<n; i++){
        for (int j=0; j<m; j++){
            if (a==j&&x[i][j]>0){
                k++;
                sum+=x[i][j];
            }
        }

        avrg=sum/k;
    }
}
Last edited on
Topic archived. No new replies allowed.