Use Jagged Array In c - c

How to Insert and then print data from jagged array in below code ?
int *jagged[5];
jagged[0] = malloc(sizeof(int) * 10);

You can insert by adding a second subscript for the nested array's index.
int i;
for (i = 0; i < 10; ++i)
jagged[0][i] = some_value;
and print like
int i;
for (i = 0; i < 10; ++i)
printf("%d\n", jagged[0][i]);
Keep in mind that you need to keep track of each nested array's length on your own. Depending on your needs, you might do something like
int jagged_lengths[] = {10, 5, 4, 0, 3};
int i, j;
// Write some data
for (i = 0; i < 5; ++i) {
jagged[i] = malloc(sizeof(int) * jagged_lengths[i]);
for (j = 0; j < jagged_lengths[i]; ++j)
jagged[i][j] = some_value;
}
// Read back the data
for (i = 0; i < 5; ++i)
for (j = 0; j < jagged_lengths[i]; ++j)
printf("%d\n", jagged[i][j]);

first of all, why not define your array as a multi dimentional array? unless you want the size of each member different, you don't need to use malloc for each member, simply do:
int jagged[5][10];
as for iterating, you can do something like:
int i,j;
for (i = 0; i < 5; i++)
for (j = 0; j < 10; j++)
jagged[i][j] = i*j; //or any value you want
for (i = 0; i < 5; i++)
for (j = 0; j < 10; j++)
printf ("%d,%d: %d\n", i, j, jagged[i][j]);

Related

Invalid write when trying to add a value in the cell of the matrix

I'm trying to add values inside a matrix.
For the first row everyting goes well.
However, when I'm trying to access the second row (if there's one), I get an invalid write.
Here is the the different version I wrote :
mat[i][j]
*(*(mat + i) + j)
mat[i * N + j]
int** matrice(int N, int M){
int **mat = (int **)malloc(N * sizeof(int*));
for(int i = 0; i < N; i++) mat[i] = (int *)malloc(M * sizeof(int));
for (int i = 0; i < N; i++){
for (int j = 0; i < M; j++){
//mat[i][j] = i+j;
}
}
return mat;
}
You have an error in your nested for loop that uses j as index, however, you compare with i:
for (int j = 0; i < M; j++)
should be
for (int j = 0; j < M; j++)

Repeat each element in array "i" times in C

This has to be done in C.
I'm Looking for a way to take an array with elements such as:
a = {1,2,3}
and given a variable such as i, if i = 3, each element copied into a new array i times like so:
b = {1,1,1,2,2,2,3,3,3,}
this is what I have so far:
for(i = 0; i < size_S-inter_seq; i++) //size of new array - times copied
{
for(j = 0; j < size_X; j++) //size of old array
{
for(k = 0; k < inter_seq; k++) //times to be copied
{
b[i+k] = a[j];
}
}
}
thanks
The order does matter.
Thanks.
This code should do the trick:
int a[] = {1,2,3};
int i = 3;
int len = (sizeof a)/(sizeof a[0]);
int b[i*len];
for(int k=0; k<len; k++)
for(int j=0; j<i; j++)
b[k*i + j] = a[k];

% symbols keep printing when trying to print out a char 2D array in C

I'm new to C and I'm just trying to print out a two 2 array.
This bug has been annoying me all day and I'm not really sure whats going on.
#include<stdio.h>
void run(int);
main()
{
run(5);
return 0;
}
//Have to make it a character array as it needs to
//store numbers AND commas.
run(int x)
{
int size = 2*x -1;
char array[size][size];
int i = 0;
int j = 0;
for( i; i < size; i++){
for(j; j< size; j++){
array[i][j] = '1';
}
}
int k = 0;
int l = 0;
for( k; k < size; k++){
for(l; l< size; l++){
printf( "%c" , array[l][k]);
}
printf("%\n", "");
}
}
This is the output I get:
1%
%
%
%
%
%
%
%
%
You code has several mistakes:
The biggest problem is that your not initializing your loop counters where you should:
for(i; i < size; i++){
for(j; j < size; j++){
With that, i & j are left as they were prior to the for statement. The first section of these statements does nothing at all. While that's harmless for i (since it's initialized to 0 before the for), that's devastating for j, which never goes back to 0. Your code should be:
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
The same issue exists with k & l, and the same fix should be applied:
for(k = 0; k < size; k++){
for(l = 0; l < size; l++){
Next, you're "rotating" access in your array. When you fill the array with values, you have i in your outer loop and j in the inner loop, and you use them as [i][j]:
array[i][j] = '1';
Think of that as Out & In --> [Out][In].
When you print the array, you "rotate" that, k is outer & l is inner, and you use them as [l][k]:
printf("%c", array[l][k]);
That's like doing [In][Out].
While that's not a problem with all values being identical ('1'), and the matrix being square (width == height), it won't work with other values or dimensions, and is confusing.
Last, you're attempt to print a new line is wrong. You have a % specifier, but your not really using any valid character after that, and you don't need that anyway, just print:
printf("\n");
So, all together, here's what the code should be:
run(int x)
{
int size = 2*x -1;
char array[size][size];
int i,j;
for(i = 0; i < size; i++){
for(j = 0; j < size; j++){
array[i][j] = '1';
}
}
int k, l;
for(k = 0; k < size; k++){
for(l = 0; l < size; l++){
printf("%c", array[k][l]);
}
printf("\n");
}
}
(And as a side note, k & l are not really required, you can simply reuse i & j)

Filling 3d array using for loop in C programming

I'm working on my class project and I'm currently stuck at the most basic one. Basically I have to fill the stack of boxes using loops and 3d array. The stack is 4 width, 4 length and 3 height and I have to fill boxes with 100 items each.
void main(){
int boxShleve[3][4][4];
int i, j, k;
for (i=0; i<3; ++i){
for (j=0; j<4; ++j){
for (k=0; k<4; ++k){
boxShleve[3][4][4] = 100;
}
}
}
printf("%d", boxShleve[3][4][4]);
}
This is the broken piece of my work... How do I make each array has 100 element in it?
This is what you meant to do:
int main()
{
int boxShleve[3][4][4];
int i, j, k;
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
for (k = 0; k < 4; ++k)
boxShleve[i][j][k] = 100;
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
for (k = 0; k < 4; k++)
printf("%d ", boxShleve[i][j][k]);
return 0;
}
The reason you need the nested loops is to use the i, j and k as indexes to access the array. So you have to use them.
Same for printing the values.
A faster way to do this if you are using GCC is as follows.
int boxShleve[3][4][4] = {
{[0 ... 2] = 100 },
{[0 ... 3] = 100 },
{[0 ... 3] = 100 } };
#include <stdio.h>
int main(){
int boxShleve[3][4][4];
size_t size = sizeof(boxShleve)/sizeof(int);
int *p = &boxShleve[0][0][0];
while(size--)
*p++ = 100;
printf("%d\n", boxShleve[2][3][3]);//last element,
return 0;
}

array of pointers to bit arrays

I want to make an array of pointers to bit arrays. I make this func2 to test the pointers, but I get a seg fault when I try to acess an elemeny of the bit array outside the function. What am I doing wrong?
int func2(int i, int* bit_array){
int j;
for(j = 0; j< i; j++)
bit_array[j] = malloc(sizeof(int) * i);
for(j = 0; j< i; j++)
bit_array[j] = 0;
return 1;
}
int main(){
int** bit_root;
bit_root = malloc(sizeof(int *) * 5);
func2(5, bit_root);
int n;
for(n = 0; n < 5; n++)
printf("%d ", bit_root[0][n]); //error
printf("\n");
return 0;
}
You are sending the array incorrect to the function func2. func2 need to be:
int func2(int i, int** bit_array){
int j,k;
for(j = 0; j< i; j++)
bit_array[j] = malloc(sizeof(int) * i);
for(j = 0; j< i; j++)
for(k = 0; k< i; k++)
bit_array[j][k] = 0;
return 1;
}
int main(){
int** bit_root;
bit_root = malloc(sizeof(int *) * 5);
func2(5, bit_root);
int n;
for(n = 0; n < 5; n++)
printf("%d ", bit_root[0][n]); //error
printf("\n");
return 0;
}
In the lines below you allocate memory for array of int for each element of bit_array and assign pointers to int arrays to bit_array elements:
for(j = 0; j< i; j++)
bit_array[j] = malloc(sizeof(int) * i);
But here you assign zeroes to bit_array elements. Thus you rewrite pointers to zero as if you didn't allocate memore at all:
for(j = 0; j< i; j++)
bit_array[j] = 0;
To fix it replace the this last block this a following code:
int k;
for(j = 0; j< i; j++)
for(k = 0; k < i; k++)
bit_array[j][k] = 0;
Here in the first loop you iterate through the array of pointers to int arrays (bit_array[j]) and in the inner loop you iterate through the array of ints (bit_array[j][k]). These changes requires changing of func2 definition - second parameter must be pointer to pointer to int instead of just a pointer. It helps you to get rid from warnings of compiler.
To see what is going on clearly you can use following code:
int k, *int_array = NULL;
for(j = 0; j< i; j++)
{
int_array = bit_array[j]; // get the pointer to int array
for(k = 0; k < i; k++)
int_array[k] = 0; // assign values to int array
}
And don't forget to free all these memory for both inner arrays and bit_array.

Resources