Atof or atoi

Hi guys,
i've a problem. I don't know how to use atof or atoi in a bidimensional array.
Basically i need lo make a list with names and family name, with three notes.
Like:

Julia Anderson 3.2 3.8 4.3

Thank 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
  #include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int fila, columna;
int matriz[10][10];

cout<<"Ingrese el numero de filas: ";
cin>>fila;
cout<<"Ingrese el numero de columnas: ";
cin>>columna;

//Ingreso de valores

for(int i=0; i<fila; i++){
    for (int n=0; n<columna; n++)
        {
        cout<<"Ingrese el valor de ["<<i<<"]["<<n<<"]: ";
        cin>>matriz[i][n];
        }
        }

//Muestra la matriz

cout<<"Matriz hecha: "<<endl;
for (int i=0; i<fila; i++){
    for(int n=0; n<columna; n++)
        {cout<<matriz[i][n]<<" ";
        }
        cout<<endl;
        }
}
Are you sure you need atoi() and/or atof()?
Those functions are used to convert a character string to an int or double. Example
1
2
    char str[] = "123";
    int n = atoi(str);


I don't see where you plan to use that here.
Yes, I mean. I only have to convert the names and last names to an int. And that int will be zero.
So i can divide the notes.
If you want something like this: Julia Anderson 3.2 3.8 4.3, i assume you there are five pieces of information, first-name, last-name and then three floating-point numbers.
That would seem to fit a structure like this:
1
2
3
4
5
6
7
struct Item {
    string fname;
    string lname;
    double a;
    double b;
    double c;
};


and then define an array like this,
 
    Item matriz[10]
;

Last edited on
Oh, yes. That would be better.
I'm gonna see if this work
Last edited on
Well, I'm still not entirely sure what it is you are supposed to do. Please be patient with me as I'm feeling a bit lost here.

If you use an array, all the items must be of the same type - that is, all are strings, or all are integers. If you want to preserve all of the information, then using string for everything would seem most suitable. If you use int for everything you won't be able to store any names.

Unless of course used two completely separate arrays, one for the text, the other for the numbers.
Well, i think i only have to use one array for everything.
Then the notes will be like (3.2+3.8+4.3)/3 =3.7

And i'll put that into another array:
Name Final note
Julia Anderson 3.7
This is a "find the student's average grade" homework.

By "notes" lexie21 means grades. (In Spanish one calls your grade a "note".)

Using the struct is a good idea:

1
2
3
4
5
6
7
8
9
10
11
12
struct Estudiante
{
    string apellido;
    string nombre;
    double notas[10];  // se saque 10 notas cada escolar
};

int main()
{
    Estudiante muchachos[10];

    ...

Now to find the average student grade, you only need a function that finds the average of ten numbers in a one-dimensional array:

1
2
3
4
double promedio( double notas[10] )
{
    ...
}

This makes it easy to find the average of a student's grade:

1
2
3
4
5
6
    for (int n = ...; ...; ...)
    {
        cout << muchachos[n].nombre << " "
             << muchachos[n].apellido
             << " saco un " << promedio( muchachos[n].notas ) << ".\n";
    }

You might also want to write functions to get the name and grades of a student from the user.

Hope this helps.
Topic archived. No new replies allowed.