2 matrices, one with string other with int

im trying to make a simple program to practice 2d arrays in c++.
the string array is simple name & lastname which i understand so far.

the int matrix consists of 1 simple number input (representing registry number) , and the next input is 2 numbers that go into a function , and the result of the function enters the array (and thats what im having problem with)

if you need more information about it then ask and ill try to answer, thanks
(and thats what im having problem with)
Okay, so what is the problem you're having? Show the relevant code you're having a problem with.
Hello ok304,

Yes. It is always best to post what code you have. And it is best to post a complete program that can be compiled and tested.

Having no idea what you have done it is difficult to guess at an answer.

Also it helps to post test input data and the expected output. Along with what you are actually getting.

Andy
my code so far far only saves and prints the first array with the names and last names, i dont have access to my code at the moment but the input /output looked like this:
enter 0.0 name1
enter 0.1 lastname1
enter 1.0 name2
enter1.1 lastname2

output:
name1 lastname1
name2 lastname2

i will post the code as soon as i can
this is my code so far
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
#include <iostream>

using namespace std;

float finalgrade(float a,float b)
{
    float result;
    cout<<"give grade: ";
    cin>>a;
    cout<<"give percentage of grade: ";
    cin>>b;
    result=(a*b)/100.00;
    return result;
}

int main()
{
    int n,m=2, i, j; 
    cout<< "arithmos mathiton= ";cin>>n;

    string namesurname[n][m]; //name and surname
    int reggrade[n][m]; //registry number and grade (the grade has to be the result that will come out of the function above)
    
    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
        {
           
            cout<<"enter name and then surname: ";
            cin>>namesurname[i][j];
        }

        cout<<endl<<endl;
        
    /*    for(i = 0; i < n; i++)
        for(j = 0; j < m; j++)
        {
           
            cout<<"give registry number and grades to enter in the function: ";
            cin>>reggrade[i][j];
        }

        cout<<endl<<endl;
        */
        
    for(i = 0; i < n; i++)
        {
        for(j = 0; j < m; j++)
            cout<<namesurname[i][j]<<" ";
            //cout<<reggrade[i][j]<<" ";
            cout<<endl;
        }

    return 0;
} 


my goal is to make the input look like this:
enter name and surname: name1 surname1
enter registry number and values for the function: 1 20 30
enter name and surname: name2 surname2
enter registry number and values for the function: 2 30 30
enter name and surname: name3 surname3
enter registry number and values for the function: 3 40 30

output:

name1 surname1 1 (result of function)
name2 surname2 2 (result of function2)
name2 surname3 3 (result of function3)
Last edited on
the problem i have is, i dont know how to put results of a function on the [j] of the 2d array while the [i] is still my input number, and how i will print the arrays next to each other
(for now xD)
You probably shouldn't input the a and b values inside finalgrade(). Those values should be read from the user inside main() and then passed to finalgrade():
1
2
3
4
5
6
float finalgrade(float a,float b)
{
    float result;
    result=(a*b)/100.00;  // ?? The math seems odd.
    return result;
}


Beware of creating variable-length arrays in C++. It isn't supported by the standard, although some compilers will allow it. Consider using vector<> instead.

That said, namesurname always has two members in the last index (the name and surname) and reggrade should only have 2 also (the registry number and result of calling finalgrade():
1
2
    string namesurname[n][2]; //name and surname
    int reggrade[n][2]; //registry number and grade 


As an aside, it would make more sense to store this stuff in a struct, but I understand you're doing this as practice for a2D array.

Now let's think about the input phase. For each pass through the for loop, you want the user to see:
enter name and then surname: <name> <surname>
enter registry number and grades to enter in the function: <registry number> <a> <b>

The key here is there is no inner loop:
1
2
3
4
5
6
7
8
9
    for(i = 0; i < n; i++) {
        cout<<"enter name and then surname: ";
        cin>>namesurname[i][0] >> namesurname[i][1];

        cout<<"enter registry number and grades to enter in the function: ";
        int a, b;
        cin >> reggrade[i][0] >> a >> b;
        reggrade[i][1] = finalgrade(a, b);  // Compute and store the final grade.
    }


Now the output. For each person, it appears that you want to print:
name surname registryNumber finalgrade

So:
1
2
3
4
    for(i = 0; i < n; i++) {
        cout << namesurname[i][0] << ' ' << namesurname[i][1]
             << ' ' << reggrade[i][0] << ' ' << reggrade[i][1] << '\n';
    }


When I put this all together, I get this run:
arithmos mathiton= 3
enter name and then surname: name1 surname1
enter registry number and grades to enter in the function: 1 20 30
enter name and then surname: name2 surname2
enter registry number and grades to enter in the function: 2 30 30
enter name and then surname: name3 surname3
enter registry number and grades to enter in the function: 3 40 30
name1 surname1 1 6
name2 surname2 2 9
name3 surname3 3 12

thanks! works great and it looks way easier than the version i thought :D
Consider using vector<> instead.
i dont know what vector is yet, i got into C++ 2 weeks ago and missed the array lesson so im trying to improvise from what i know from C but thanks.

result=(a*b)/100.00; // ?? The math seems odd.
yeah this was just an example to show you what i meant by
and the next input is 2 numbers that go into a function , and the result of the function enters the array (and thats what im having problem with)
.

i also didnt know you could do this
string namesurname[n][2]; //name and surname
int reggrade[n][2]; //registry number and grade
. ill try to make some more small programs like those and try to fit this way to get comfortable with it now
Topic archived. No new replies allowed.