Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int i=0, j=0;
void main(){
int a[3][5]={1,2,{3,4,6,8},{5,8,9},10,{11,12,13,14},{21,22},23,9,8,7,6,5,4,3};//Array Initialisation
for(i=0; i<3; i++){
for(j=0; j<5; j++){
printf("\na[%d][%d]:%d\n", i, j, a[i][j]);//Array Printing
}
}
}
/*The above code initialises the array with some logic that I'm unable to understand. How are the set elements treated? Please explain */
You don't get the point of bi-dimensional array in C:
int A[2][3]
is the declaration of a bi-dimensional array of 6 integers elements with two rows and three columns. This is always true, the number in the first square brackets stands for rows, instead the number in the second square brackets stands for columns.
To initialize a bi-dimensional array you need to know these things:
int a[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};
As you can see there are three curly brackets (the rows), inside these three curly brackets there are 5 numbers (the columns).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What is the C code for mergeing two unsorted arrays without using a 3rd array.
Eg
array1={1,3,5,7}
array2={2,4,6}
Output should be
array1={1,2,3,4,5,6,7}.
This is the code i have written.But this doesnt work if the number of array elements are same in both arrays.kindly help me in fixing this bug.
void merge(int a[],int b[],int ele1,int ele2)
{
int i,j,k,ele3;
ele3=ele1+ele2;
for(i=1,k=0;k<ele2;i=i+2)
{
j=ele1;
while(j>=i)
{
a[j]=a[j-1];
j--;
}
a[j+1]=b[k];
k++;ele1++;
}
for(i=0;i<ele3;i++)
printf("%d ",a[i]);
}
main()
{
int a[]={1,3,5},b[]={2,4},ele1,ele2;
ele1=sizeof(a)/sizeof(a[0]); ele2=sizeof(b)/sizeof(b[0]);
merge(a,b,ele1,ele2);
}
It can be done easily if any one of the array is capable of holding all elements, Otherwise it can be done if any one of the array is dynamic array, In case of dynamic array we can change the size of the array by using realloc so, after that we merge it easily
The solution will be like [Not tested, Its likely answer]
void merge(int ar1[], int ar2[])
{
int len = ar1.length;
int totalLength= (ar1.length+ar2.length);
ar1= (int*)realloc(ar1,(totalLength)*sizeof(int));
for (int i=len,index=0; i<totalLength; i++)
{
ar1[i]=ar2[index];
index++;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get some data randomly from an 2-d array.
In my partial code given below where cluster_center and sample data both are 2-d array in double type. I want to assign some data randomly from sample_data array to cluster_center array.
for(int i= 0; i< 3; i++)
{
for(k=0; k<17; k++)
cluster_center[i][k] = //what will be???;
}
TIA :)
You could just generate two random numbers via rand and modulate it to ensure it doesn't exceed your 2-D array boundaries. Not sure exactly how random you need it to be though, as rand will favor lower numbers slightly according to the man page.
You'd assign the value like:
cluster_center[i][k] = sample_data[random_num1][random_num2];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
1.2^2+2.3^2+3.4^2+4.5^2+ need to calculate the sum up-to nth number with C programming.
But I can't find any way to solve the program.
When the user in put 4 as the value of n, the sum will be the total of 1.2^2+2.3^2+3.4^2+4.5^2.
Can anyone help me get the algorithm?
A simple for loop would do it:
int compute(int n) {
int i, sum=0;
for(i=1; i<=n; i++) {
int val = i*(i+1)*(i+1);
sum += val;
}
return sum;
}
for(int i=0; i<n; i++). This is a for loop.
Inside the loop, store the loop iterator i in a double variable.
Add 1.2 to it.
Multiply it by itself.
Do something with the result: print it, and/or add it to a sum variable etc.
Do not use the xor operator ^ for this.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I use a function that displays a block of memory's content pointed by a pointer.
But didn't get the desired output, i am new to this, please correct my if i am wrong.
when I input size =3, element = 1,2,3 , I got output = 1 only.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
void merge(int **arr1);
int main(void) {
int size1;
printf("Give me the size of first array\n");
scanf("%d", &size1);
int *arr1 = malloc(size1*sizeof(int));
int *p1=arr1;
printf("Give me the elements of first array\n");
int index1;
for(index1 = 0 ; index1<size1; index1++)
scanf("%d", p1++);
merge(&arr1);
return;
}
void merge(int **arr1) {
while(**arr1) //**arr1 is the content of the passed array, if there
// is an int in it, print that out and increment to next one
{
printf("%d", **arr1); // ** is the content and * is the address i think, right?
*arr1++;
}
}
Your merge() code expects the array to be terminated by zero. The calling code is not doing it, so the behavior is unspecified (I got segfault when I tried your code).
The other issue is that you should put parentheses around *arr1:
(*arr1)++;
When I run your code with this modification and enter zero for the last element, your code runs fine.