[ create a new paste ] login | about

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

C++, pasted on May 18:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#define N 5

int main(int argc, char* argv[])
{
	int** A = new int*[N];
	for (int t1 = 0; t1 < N; t1++)
	{
		A[t1] = new int[N];
		for (int t2 = 0; t2 < N; t2++)
		{
			A[t1][t2] = rand() % (N-1) + 1;
			printf("%d ",A[t1][t2]);
		}

		printf("\n");
	}

	printf("\n");

	double* B = new double[N];
	for (int i = 0; i < N; i++)
	{
		double avg = 0.00;
		for (int j = 0; j < N; j++)
		{
			printf("%d ",A[i][j]);
			if (i % 2 != 0)	avg+=(double)A[i][j] / N;
		}

		if (avg > 0 && i % 2 != 0) B[i] = avg;
		if (i % 2 != 0) printf("\t avg = %4.2f\n",B[i]);
		else printf("\n");
	}

	printf("\n");

	double mul = 1;
	for (int t = 0; t < N; t++) 
		if (B[t] > 0) mul*=B[t];

	printf("mul = %4.2f\n",mul);

	int max = 0;
	for (int z = 0; z < N; z++)
		max = (B[z] > B[max] && B[z] > 0) ? z : max;

	printf("max = %d B[%d] = %4.2f\n",max,max,B[max]);

	int min = 0;
	for (int r = 0; r < N; r++)
		min = (A[max][r] < A[max][min]) ? r : min;

	printf("min = %d max = %d A[%d][%d] = %d\n",min,max,min,max,A[max][min]);

	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
4 3 2 4 2 
4 3 1 2 2 
3 4 3 4 4 
3 1 3 1 1 
4 1 4 2 3 

4 3 2 4 2 
4 3 1 2 2 	 avg = 2.40
3 4 3 4 4 
3 1 3 1 1 	 avg = 1.80
4 1 4 2 3 

mul = 4.32
max = 1 B[1] = 2.40
min = 2 max = 1 A[2][1] = 1


Create a new paste based on this one


Comments: