Why is my code not doing the task im asking?!

Calculate a massive A[n][m] elements which are randomized within the interval of [-5, 5], n and m, is inputted by user. Afterwards get all the positive numbers from each line and calculate the median.

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
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
randomize();
int a[20][20], n, m;
int i,j,sum=0, sum1=0;
   cout<<"Input n ";
   cin>>n;
   cout<<"Input m ";
   cin>>m;

   for(i=0; i<n; i++){
   	for(j=0; j<n; j++){
      a[i][j]=random(11)-5;
      cout<<a[i][j]<<" ";

      }
      cout<<"\n";
      }
   sum1=0;
      for(i=0; i<m; i++){
      for(j=0; j<n; j++)
      if (a[i]>0)

                sum=sum1+a[i][j];  }cout<<i+1<<". Line total: "<<sum1<<"\n";}



      getch();
        }


Its not doing the if im asking and doesn't calculate it as i need, so frustrating.
On line 28 you update sum when you should be updating sum1, I think. If you wan the sum of each row you will have to reset sum1 before you start on a new row.

The problem description asks for the median and not the sum.
right but before i can get to median i need sum of all positive numbers no?
Line 1 should be:
#include <iostream>

You need:
using namespace std;

Line 7 should be:
srand (time(NULL));
time() requires #include <ctime>
http://www.cplusplus.com/reference/cstdlib/srand/

Line 17 should be:
a[i][j]=(rand()%11)-5;

Line 28 - You have an extraneous close brace.









Note that median is not necessary the same as the average. I don't think the sum will help you find the median.
Last edited on
Topic archived. No new replies allowed.