Hello I have to do exercise in which i cant use "[]" , and in which i can use malloc(), free(), realloc() and calloc(). I need to make an table which for example for:
Input:
4
5
1 2 2 2 5
4
2 2 2 2
7
1 2 3 2 2 3 5
4
2 2 3 2
Gives:
1 2 2 2 5
2 2 2 2
1 2 3 2 2 3 5
2 2 3 2
So its an some of array [x][y]
I was trying to do this here is my code:
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 <stdio.h>
#include <stdlib.h>
using namespace std;
main()
{
int column;
int n;
int *p;
int line;
int value;
scanf("%d",&column);
for (int i = 0 ; i < column ; i ++)
{
scanf("%d",&n);
p = (int*) malloc(sizeof(int)*n);
for (int j = 0 ; j < n ; j++)
{
scanf("%d",&value);
*(p+j) = value;
}
}
}
|
but its too hard for me, and i couldnt find clear explanation of making arrays with using malloc, and making it free...
Could somebody show me the example how it works? i dont know what is wrong in my code i dont know too that im doing it good or i need to do [][]array with using int** tab = (int**) malloc(x * sizeof(int));
I was trying to make first giving number of columns first for, then giving number of lines (and there is problem cause i dont know how to write it, and then remember it and do next line to make table like in example.
I have to do this for tommorow, im searching for it in books all day and cant get it. On internet i cant find simmilar problem which would explain me it.
Sorry for my English, and thanks for help.