Multi-dimensional arrays
Nov 20, 2017 at 9:08am UTC
Hello, got stuck (again) and this time with arrays.
My task is:
In a text file there is given a multi-dimensional array, which is made n rows and m columns.
Find the positive elements that are under main istrižaine (doesn't translates but in an array istrizaine is a11;a22;a33 and so on) sum them and the negative numbers wich are above istrizaine multiply.
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
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
const int N=40;
int a[N][N];
cout << "Raskite teigiamu elementu, esanciu po pagrindine istrižaine, suma ir neigimu elementu, esanciu virš pagrindines istrižaines, sandauga." <<endl;
ifstream f("text.txt" );
if ( f.fail()){
cout<< "Nerastas failas" ;
}
int m,n, sum=0, sand=0;
f >> n >> m;
for (int i=0; i<n; i++){
for (int j=0; j<m; j++){
f >> a[i][j];
if (i>j&&a[i][j]>0){
//f >> a[i][j];
cout<<"\t" <<a[i][j]<<endl;
}
if (i<j&&a[i][j]<0){
cout <<"\t" << a[i][j]<<endl;
}
}
cout<<endl;
}
f.close();
cin.get();
return EXIT_SUCCESS;
}
Now my problem is I don't know how to add the positive numbers that I get and how to multiply the negatives ones.
Last edited on Nov 22, 2017 at 8:06pm UTC
Nov 20, 2017 at 9:22am UTC
The problem I have is that the program doesn't find the proper numbers.
The problem you have is that you read half the numbers twice (on lines 21 and 24)
- Open the file
- Read ALL the numbers
- Close the file
THEN start doing the processing once you have the whole array stored.
Nov 20, 2017 at 10:31am UTC
Thanks lastchance.
Topic archived. No new replies allowed.