How to select data randomly in 2-d array [closed] - arrays

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];

Related

How does the code that prints array elements works? [closed]

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.

When to declarate a variable issue [closed]

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
Can someone explain me the most simple way the meaning of this syntax of C?
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(number[i]<number[j])
{
aux=number[i];
number[i]=number[j];
number[j]=aux;
}
}
}
I just trying to figure out I know is an iteration or a loop but specifically aux is a var. Why i need to follow this i'm trying to get pos and negs, into an array but this part i'm stuck is there another way ?
I just need to figure this syntax.
This looks like Bubble Sort. aux is a temporary variable used for exchanging the values of number[i] and number[j]. You can't do
number[i] = number[j];
number[j] = number[i];
to exchange the two, as both would be equal to number[j] this way. So you need a temporary variable.

Array Initialisation with set of elements [closed]

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).

Program to find sum up-to .....nth number [closed]

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.

1D string Array comparison with 2D string Array in C [closed]

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 8 years ago.
Improve this question
I am working on mobile communication Simulator based on C language. I have two different IPs which are stored as Strings in different format.
For User it is XYZ[20]
For Access Gateway, it is ABC[No. of Users][20]
Now, I need to compare both for one algorithm. but I am confused that how should I do them as both are different arrays. Is there any way to compare both 1D with 2D array ?
#include <string.h>
...
for ( int i = 0; i < num_users; i++ )
{
if ( strcmp( ABC[i], XYZ ) == 0 )
{
// strings are equal
}
else
{
// strings are not equal
}
}
ABC[i] is the i'th string stored in ABC.

Resources