2d matrix problem

[2D]Array (a) consists of 5 rows and 5 columns.
int a [ ][ ] ={{2,-5},{3,6,1,-4,2},{8},{9,2,1,2,9},{4,-6}};
a. Write a C++ code that stores the value 50 instead of any negative number that it finds in the second row of array(a)
b. findshowmanypositiveintegersarestoredin array a.

this is an assignment question i have, i tired to write the program but i never really figure it out no matter what
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
int main()
{
    int i,j;
    int a[5][5]={{2,-5},{3,6,1,-4,2},{8},{9,2,1,2,9},{4,-6}};
   

   
   
   system("pause");
    return 0;
}

this is what i know so far, the rest idk how to solve it
Hello samzavax,

You can start with this:
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
#include<iostream>
//#include<conio.h>  // <--- No longer used.
#include<string>

using namespace std;

int main()
{
    int i{}, j{};  // <--- ALWAYS initialize your variables. If "i" and "j" are to be used in for loops it is better to define them there.
    int a[5][5]  // <--- The (=) is not needed.
    {
        { 2, -5 },
        { 3, 6, 1, -4, 2 },
        { 8 },
        { 9, 2, 1, 2, 9 },
        { 4, -6 }
    };

// Todo

    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.

}

Under the Todo I would start with just printing out the array. When you have that much working it would be very easy to add to the code necessary todo what is required.

Andy

Edit:
Last edited on
Hello samzavax,

After a little bit I ended up with this:
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
#include<iostream>
#include <iomanip>
#include<string>
//#include<conio.h>  // <--- No longer used.

using namespace std;

int main()
{
    constexpr int MAXROW{ 5 }, MAXCOL{ 5 };

    //int i{}, j{};  // <--- ALWAYS initialize your variables. If "i" and "j" are to be used in for loops it is better to define them there.
    int numArray[MAXROW][MAXCOL]  // <--- The (=) is not needed.
    {
        { 2, -5 },
        { 3, 6, 1, -4, 2 },
        { 8 },
        { 9, 2, 1, 2, 9 },
        { 4, -6 }
    };

    // Todo



    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.

}

After adding to what you have I came up with this:

 Before changes.
   2  -5   0   0   0
   3   6   1  -4   2
   8   0   0   0   0
   9   2   1   2   9
   4  -6   0   0   0

 After changes.
   2  -5   0   0   0
   3   6   1  50   2
   8   0   0   0   0
   9   2   1   2   9
   4  -6   0   0   0


 Press Enter to continue:


This covers part a.

I have no idea what part b is????

Andy
thank you for your help, the thing is I didn't understand how did you make the 50 appear in the second row?
and for part b
they want me to show how many positive integers are in the array
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
#include <iostream>
#include <iomanip>
#include <string>
#include <iomanip>

using namespace std;

constexpr size_t MAXROW {5}, MAXCOL {5};

using Array = int[MAXROW][MAXCOL];

void display(const Array nums)
{
	for (size_t r = 0; r < MAXROW; ++r) {
		for (size_t c = 0; c < MAXCOL; ++c)
			std::cout << std::setw(6) << nums[r][c];

		std::cout << '\n';
	}

	std::cout << '\n';
}

void repneg(Array nums, size_t rp, int rep)
{
	for (size_t c = 0; c < MAXCOL; ++c)
		if (nums[rp][c] < 0)
			nums[rp][c] = rep;
}

size_t cntpos(const Array nums)
{
	size_t cnt {};

	for (size_t r = 0; r < MAXROW; ++r)
		for (size_t c = 0; c < MAXCOL; ++c)
			cnt += nums[r][c] > 0;

	return cnt;
}

int main()
{
	Array numArray
	{
		{ 2, -5 },
		{ 3, 6, 1, -4, 2 },
		{ 8 },
		{ 9, 2, 1, 2, 9 },
		{ 4, -6 }
	};

	std::cout << "At start\n";
	display(numArray);

	std::cout << "Replace neg in r2\n";
	repneg(numArray, 1, 50);
	display(numArray);

	std::cout << "There are " << cntpos(numArray) << " pos numbers\n";
}



At start
     2    -5     0     0     0
     3     6     1    -4     2
     8     0     0     0     0
     9     2     1     2     9
     4    -6     0     0     0

Replace neg in r2
     2    -5     0     0     0
     3     6     1    50     2
     8     0     0     0     0
     9     2     1     2     9
     4    -6     0     0     0

There are 13 pos numbers


Note that for b) the count is after a) is performed.
1
2
3
4
5
6
7
8
9
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
   int a[5][5]={{2,-5},{3,6,1,-4,2},{8},{9,2,1,2,9},{4,-6}};
   cout << "There are " << count_if( a[0], a[0] + 25, []( int x ){ return x > 0; } ) << " positive elements\n";
}


There are 12 positive elements
Hello samzavax,

Like I said first show me how you would print the array. Once you do that the rest is easy to modify that code to get what you need for the 2nd output.

Unless you have decided to use 1 of the 2 examples shown.

Andy
Topic archived. No new replies allowed.