[ create a new paste ] login | about

Link: http://codepad.org/jR6Q6DCj    [ raw code | output | fork ]

C++, pasted on Jun 12:
#include <iostream>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
 
const int n = 5;
const int m = 5;
void show_arr(ostream & os, int arr[n][m])
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            os << setw(3) << arr[i][j];
        os << '\n';
    }
}
int main()
{
    srand(unsigned (time(0)));
    ofstream fout;
    cout << "Enter the name of file: ";
    string name;
    getline(cin, name);
    fout.open(name.c_str());
    int arr[n][m];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (rand() % 2)
                arr[i][j] = rand() % 6;
            else
                arr[i][j] = -(rand() % 6);
 
    show_arr(cout, arr);
    show_arr(fout, arr);
 
    /*for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cout << setw(3) << arr[i][j];
        cout << '\n';
    }
    cout << '\n';*/
 
    int sum_odd(0);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (i % 2 || i == 1)
                if (j % 2 || j == 1)
                    sum_odd += arr[i][j];
    cout << "\nThe sum of odd numbers is " << sum_odd << '\n';
    fout << "\nThe sum of odd numbers is " << sum_odd << '\n';
 
    int first_i(0), first_j(0), last_i(0), last_j(0);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (arr[i][j] < 0)
            {
                first_i = i;
                first_j = j;
                i = n;
                j = m;
            }
 
    for (int i = n-1; i >= 0; i--)
        for (int j = m-1; j >= 0; j--)
            if (arr[i][j] < 0)
            {
                last_i = i;
                last_j = j;
                i = -1;
                j = -1;
            }
 
    int sum_between(0);
    for (int i = first_i; i < last_i + 1; i++)
        if (i == first_i)
            for (int j = first_j; j < m; j++)
                sum_between += arr[i][j];
        else if (i == last_i)
            for (int j = 0; j < last_j + 1; j++)
                sum_between += arr[i][j];
        else
            for (int j = 0; j < m; j++)
                sum_between += arr[i][j];
    cout << "The sum between first and last negative elements is " << sum_between << "\n\n";
    fout << "The sum between first and last negative elements is " << sum_between << "\n\n";
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (arr[i][j] < 0)
            {
                if (-(arr[i][j]) <= 1)
                    arr[i][j] = 0;
            }
            else
                if (arr[i][j] <= 1)
                    arr[i][j] = 0;
 
    show_arr(cout, arr);
    show_arr(fout, arr);
    /*for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cout << setw(3) << arr[i][j];
        cout << '\n';
    }
    cout << '\n';*/
    fout.close();
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Enter the name of file:  -1  0  2 -1  5
 -4  5  1  0  0
 -3 -2  4 -3  1
  5  3 -4  2 -2
 -2 -5  1 -3  1

The sum of odd numbers is 10
The sum between first and last negative elements is -1

  0  0  2  0  5
 -4  5  0  0  0
 -3 -2  4 -3  0
  5  3 -4  2 -2
 -2 -5  0 -3  0


Create a new paste based on this one


Comments: