2d arrays

hey pals,

I am trying to write a code which will find :
1. sum, - for each vector of the matrix.
2. maximum and minimum values - for each vector of the matrix.
3. maximum and minimum sum - for all vectors of the matrix.


task 3 is still undone, the issue is that I can't get how to find which is the maximum and minimum sum of the vectors.

check the code below,
thanks

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>

using namespace std;

 int main()

 {

 srand(time(0));


 int M[100][100];
 int n,s=0,k=0,p=0;
 int max=INT_MIN, min=INT_MAX;

 cout<<"Enter n=" ;

 cin>>n;


 for(int i=0; i<n; i++)

 {

 M[i][0]=rand()%20+5;
 
 k=s;
 //p=s; if(p<s) cout<<"the smallest sum = "<<p<<endl;
 s=0;
 max=INT_MIN;
 min=INT_MAX;
 
 
 
 
 for(int j=1; j<=M[i][0]; j++)

 {

 M[i][j]=rand()%10;
 
 //sum of vectors
 
 s+=M[i][j];
 
 // max and min of each vector
 
 if (max<M[i][j])max=M[i][j];
 if (min>M[i][j])min=M[i][j];
 
 }
 
cout<<"V["<<i<<"]"<<" sum of vectors = "<<s;
cout<<"; max = "<<max<<" min = "<<min<<endl;
cout<<endl;

 }

 cout<<endl;
 
if(k>s){                                       // - ?
cout<<"the biggest sum is = "<<k<<endl;}       // - ?


 
 //display

 for(int i=0; i<n; i++)

 {

 cout<<"\n v"<<i<<"["<<M[i][0]<<"] : ";

 for(int j=1; j<= M[i][0]; j++)

 {

 cout<<setw(3)<<M[i][j];

 }

 }


 cout<<endl;
 
return 0;

 }
task 3 is still undone, the issue is that I can't get how to find which is the maximum and minimum sum of the vectors.


Have you considered a vector of minimum and maximum values for each vector?

By the way have you considered using std::vector instead of the arrays?

What happens if the user enters a value greater than 100 for 'n'?

You really should consider using more meaningful variable names, save single letter names for things like loop counters.

Lastly for now you should find and consistently use some kind of "indent style", it will make reading your program much easier.


Last edited on
M[i] is the vector
M[i][0] is the number of elements in each vector

So consider something like:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

constexpr int MAXARY = 60;

int main()
{
	srand(time(0));

	int M[MAXARY][MAXARY];	// Watch the stack size!
	int maxv = INT_MIN, minv = INT_MAX;
	int n;

	cout << "Enter n= ";
	cin >> n;

	if (n > MAXARY || n < 1)
		return (std::cout << "Invalid size\n"), 1;

	for (int i = 0; i < n; ++i) {
		M[i][0] = rand() % (MAXARY - 5) + 5;

		int s = 0;
		int mx = INT_MIN, mn = INT_MAX;

		for (int j = 1; j <= M[i][0]; ++j) {
			M[i][j] = rand() % 10;

			s += M[i][j];

			if (mx < M[i][j])
				mx = M[i][j];

			if (mn > M[i][j])
				mn = M[i][j];
		}

		cout << "V[" << i << "]" << " sum of vectors = " << s;
		cout << "; max = " << mx << " min = " << mn << endl;
		cout << endl;

		if (maxv < s)
			maxv = s;

		if (minv > s)
			minv = s;
	}

	cout << endl;
	cout << "the biggest sum is = " << maxv << endl;
	cout << "the smallest sum is = " << minv << endl;

	for (int i = 0; i < n; ++i) {
		cout << "\n v" << i << "[" << M[i][0] << "] : ";

		for (int j = 1; j <= M[i][0]; ++j)
			cout << setw(3) << M[i][j];
	}

	cout << endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
it would be easier if you took line 44, the random value, and put it into a variable. 
you can use [i][j] instead of tmp, but its visually cluttered and distracting. Up to you on that. 

tmp = rand()%20+5;
M[i][0]=tmp;

for( i ...)
{ 
   thisrowmax = -1; //anything will be bigger than this from your random
   thisrowmin = 100; //anything will be less than this from your random
...  etc
for(j...)
...   

if(tmp > thisrowmax)
 thisrowmax = tmp;
if(tmp < thisrowmin)
  thisrowmin = tmp;

} //end for j
save the min and max off somewhere by row # (i is row number)
and so on...

Last edited on
thank you guys for all the help, appreciate it
Topic archived. No new replies allowed.