[ create a new paste ] login | about

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

C++, pasted on Apr 1:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#include <iostream>
#include <iomanip>

#define N 5
#define M 6

using namespace std;

void swap(int& n1, int& n2);

int main(int argc, char* argv[])
{
	float** L = new float*[N];
	memset((void*)L, 0x00, N * 4);

	for (int z1 = 0; z1 < N; z1++)
	{
		L[z1] = new float[M];
		for (int z2 = 0; z2 < M; z2++)
		{
			int sign = rand() % 2 + 1;
			L[z1][z2] = ((float)rand() / RAND_MAX) * ((sign > 1) ? (-1) : 1);
			cout << std::fixed << std::setw(4) << std::setprecision(2) << L[z1][z2] << " ";
		}

		cout << endl;
	}

	cout << endl;

	swap(L[0][0],L[N-1][M-1]);


	float sum = 0.00;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < M; j++)
			if (L[i][j] < 0) sum+=L[i][j];

	for (int t1 = 0; t1 < N; t1++)
	{
		for (int t2 = 0; t2 < M; t2++)
			cout << std::fixed << std::setw(4) << std::setprecision(2) << L[t1][t2] << " ";

		cout << endl;
	}

	cout << endl;

	cout << "Result = " << std::fixed << std::setw(4) << std::setprecision(2) << sum << endl;

	return 0;
}

void swap(int& n1, int& n2)
 { int _tn = n1; n1 = n2; n2 = _tn; }


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
-0.39 -0.80 -0.20 0.77 -0.55 0.63 
0.51 -0.92 0.72 0.61 -0.24 -0.80 
0.40 0.11 -0.22 -0.84 0.30 -0.52 
-0.97 -0.77 -0.77 -0.89 0.35 0.92 
-0.95 -0.09 -0.66 0.35 0.02 0.06 

0.06 -0.80 -0.20 0.77 -0.55 0.63 
0.51 -0.92 0.72 0.61 -0.24 -0.80 
0.40 0.11 -0.22 -0.84 0.30 -0.52 
-0.97 -0.77 -0.77 -0.89 0.35 0.92 
-0.95 -0.09 -0.66 0.35 0.02 -0.39 

Result = -10.59


Create a new paste based on this one


Comments: