How to fill an array using for loop.

Hey, I came across a problem that I can't think of how to solve. I have n = 3 which is how many arrays there are and then I have k = 5 which tells me how many elements there are in the array. So my text file is :
3 5
4 7 3 5 8
8 9 7 8 6
5 4 6 7 6

This is what I have so far. Not much. The only thing I know is I need to use 2 loops. Or maybe the second is supposed to be a while?

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
#include <iostream>
#include <fstream>
#include <iomanip>
//
using namespace std;
//
const char duom[] = "d.txt";
const char rez[] = "r.txt";
const int size = 50;
//
void skaityti(int &k, int &n, int K[], int N[]);
//
int main()
{
    int n, k, K[size], N[size];
    skaityti(k, n, K, N);
    return 0;
}
//
void skaityti(int &n, int &k, int K[], int N[])
{
    ifstream in(duom);
    in >> n >> k;
    for(int i = 0; i != n; i++)
    {
        for(int i1 = 0; i1 =! k; i1++)
        {
           in >> K[i1];
           in >> N[k];
        }
    }
}
Last edited on
EDIT :
Managed to get all the numbers printed but still can't set them to 3 separate arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
void skaityti(int &n, int &k, int K[], int N[])
{
    ifstream in(duom);
    in >> n >> k;
    for(int i = 0; i != n; i++)
    {
        for(int i1 = 0; i1 != k; i1++)
        {
            in >> N[k];
            cout << N[k];
        }
    }
}
What you’re trying to achieve is commonly known as bidimensional array.
If you are attending some course, your teacher should have explained them.

To get a bidimensional array, you can use different syntaxes:
1
2
3
4
int myarr[10][10];    // array of 10 arrays of 10 ints
int *myarr[10]; // array of 10 pointers to int
int **myarr;    // pointer to pointer to int
int (*myarr)[10];   // pointer to array of 10 ints 


When you pass them to a function, you need to chose the correspondent syntax.
Ex.:
declaration             function prototype         function call
------------------   --------------------------    ---------------
int myarr[10][10];   void func(int myarr[][10]);   func(myarr);
int *myarr[10];      void func(int *myarr[10]);    func(myarr);
...

To cut a long story short, your question admits multiple answers: please provide further details.
Topic archived. No new replies allowed.