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];
Related
I have taken two 2d arrays but the output is very different from expected.It should merge two 2d arrays into one 1d array.I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? arraySum[i][j] = array2[i][j] : arraySum[i][j] = array1[i-3][j];
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
Please try this code,To Merge two 2d arrays into one 1d array in C
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? (arraySum[i][j] = array2[i][j]) : (arraySum[i][j] = array1[i-3][j]);
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I hope this code will be usefull.
Thank you.
I believe you're trying to be too smart with ternary operator, you can do it simpler way:
if (i < 3)
arraySum[i][j] = array2[i][j];
else
arraySum[i][j] = array1[i-3][j];
Listen to your compiler it would've told you what was wrong if you've compiled with -Wall -Wextra.
And if you insist on using ternary then this would probably be clearer:
arraySum[i][j] = (i < 3) ? array2[i][j] : array1[i-3][j];
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)
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;
}
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.
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]);