Array Help Please

Pages: 123
Does that help you? :)
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
#include <iostream>
#include <sstream>
using namespace std;




int main() {

    int numRows = 0;
    int numCols = 0;
    int index = 0;
    int j = 0;

    int matrix[1000][1000];

    stringstream ss;
    string str = "2 8 4";

    cout << "enter row " << numRows << " for the matrix: ";
    getline(cin, str);
    ss.str(str);

    while(ss >> matrix[numRows][numCols])++numCols; ++numRows;
    cout << "The program has determined the matrix will have " << numCols;
    cout << " columns.";
    cout << endl << endl;

    while(1)
    {
        cout << "enter row " << numRows << " for the matrix: ";
        ss.str("");
        ss.clear();
        getline(cin, str);
        ss.str(str);
        index = 0;
        

        while((ss >> matrix[numRows][index])) ++index;
        ++numRows;

        if(index == 0) break;

    }


    cout << "The program has determined the matrix will have " << numRows << " rows. ";
    cout << "The size : " << numCols << " x " << numRows << endl << endl;




        for (index = 0; index < numRows; index++)
        {
            for (int j = 0; j < numCols; j++)
            {
                cout << matrix[index][j];
            }

        }



}



OUTPUT:

1
2
3
4
5
6
7
8
9
10
enter row 0 for the matrix: 9 9 9
The program has determined the matrix will have 3 columns.

enter row 1 for the matrix: 7 7 7
enter row 2 for the matrix: 2 2 2
enter row 3 for the matrix: 
The program has determined the matrix will have 4 rows. The size : 3 x 4

999777222000
Process finished with exit code 0


FIXED input error but

Still adding unnecessary zeros any tips to get rid of them
Last edited on
Hi,

numRows -1 ?
This is the logic error I fixed some minutes ago.

1
2
++numRows;
        if(index == 0) break;


Should be :
 
        if(index == 0) break; ++numRows;
Does that help? :)
Thank you for putting in the time and care to help me solve my problem.
I am going to learn from the way you helped me. You have done an outstanding job.
Thank you again! You are amazing!

:)

Last edited on
You are welcome :)
Glad it helped :)
closed account (48T7M4Gy)
Cheers :)
one last quetions! lol

clMatrix.cpp

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
#include "Header.h"


/**************************************************************
 * First Matrix - These function blocks will prompt the user to
 *                input the values of the first matrix and
 *                output the entire matrix.
 **************************************************************/


void clMatrix::promptForFirstMatrix()
{
    int numRows = 0;
    int numCols = 0;

    int index = 0;
    int j = 0;

    stringstream ss;
    string str = "2 8 4";

    cout << "enter row " << numRows << " for the matrix: ";
    getline(cin, str);
    ss.str(str);

    while(ss >> firstMatrix[numRows][numCols])++numCols; ++numRows;
    cout << "The program has determined the matrix will have " << numCols;
    cout << " columns.";
    cout << endl << endl;

    while(1)
    {
        cout << "enter row " << numRows << " for the matrix: ";
        ss.str("");
        ss.clear();
        getline(cin, str);
        ss.str(str);
        index = 0;


        while((ss >> firstMatrix[numRows][index])) ++index;
        ++numRows;

        if(index == 0) break;

    }


    cout << "The matrix will have the size :  " ;
    cout << numCols << " x " << numRows -1 << endl << endl;
}

void clMatrix::outputFirstMatrix()
{
    cout << endl;
    int numRows = 0;
    int numCols = 0;

    for (int index = 0; index < numRows -1; index++)
    {
        for (int j = 0; j < numCols; j++)
        {
            cout << firstMatrix[index][j];
        }

    }
}



Header.H


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 <iomanip>
#include <sstream>
#include <string>


using namespace std;


enum {
    ADDITION = 1,
    SUBTRACTION = 2,
    MULTIPLY = 3,
};

class clMatrix {

public:
        void promptForFirstMatrix();
        void outputFirstMatrix();
        


private:


        int firstMatrix[1000][1000];
      

};



main.cpp

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
#include "Header.h"


int main() {

    clMatrix ioMatrix;

    int userChoice;

    const string firstHeader  = "\n--------------------\n"
                                "   FIRST MATRIX         "
                                "\n--------------------\n";

    const string secondHeader = "\n--------------------\n"
                                "   SECOND MATRIX        "
                                "\n--------------------\n";

    const string newHeader    = "\n--------------------\n"
                                "   NEW MATRIX           "
                                "\n--------------------\n";


   



    //First Matrix
    ioMatrix.promptForFirstMatrix();
    cout << firstHeader;
    ioMatrix.outputFirstMatrix();




-How can i link
1
2
int numRows = 0;
int numCols = 0;


in promptForFirstmatrix
and in outputFirstMatrix

so they share the same value. Maybe by pointer or referance? do i need to pass in the variable? ive tried but its not working any tips?

?

Im kind of new to classes.
In class clMatrix, add a default constructor (public) and two members (private) :
1
2
3
4
5
clMatrix() : numCols(0), numRows(0) {}

private : 
int numRows;
int numCols;


And in void clMatrix:: promptForFirstMatrix() :
1
2
int numRows = 0;
int numCols = 0;

Last edited on
And in void clMatrix:: outputFirstMatrix() :
1
2
int numRows = 0; 
int numCols = 0;


And that's it! :)
Last edited on
I am not near my computer now I needed some time to correct my posts :)
You didn't really fix the logic error I have pointed out. So that you don't have to manually write numRows - 1 everywhere - it would be a bad practice if you repeat using numRows - 1 in your code.

> This is the logic error I fixed some minutes ago.

1
2
++numRows;
        if(index == 0) break;


Should be :
 
        if(index == 0) break; ++numRows;
So your problem is totally solved now?

Have a nice day :)
Topic archived. No new replies allowed.
Pages: 123