Assign a multiple numbers to an array [closed] - c

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 last year.
Improve this question
I have a list of number : -1.5 , - 1 , -0,5 , ... , 100.
I want to assign them to an array.
E.g :
a[0] = -1.5;
a[1] = -1;
....
a[202] = 100;
Can you show me how to do it ?
Thanks.

If you want to initialize array with several values, you can use this:
int a[4] = {1, 2, 3, 4};
But it should not be used, if you have a lot of numbers. If you need to assign list of numbers, that you stated in question, you should describe them with mathmetical expression in cycle:
for(int i = 0; i < 203; i++)
{
a[i] = -1.5 + 0.5 * i;
}
If you want to assign array with list of values, that can't be described with mathematical expression, you should read them from file.

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.

How to select data randomly in 2-d array [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
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];

I need a variable that have a small range value [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
I have an input number that it's value is not always consistent, i mean the value change plus 1 or minus 1 and etc. So, i want to compare it with a const but have a small range value, for example a const int Dist that have value between 14 to 16. Is that possible to implement it on C programming? Please help me.
You can set constant for lower bound, and constant for upper bound and check if the value falls within the range.
Pseudocode:
int const LOWER_BOUND = 14;
int const UPPER_BOUND = 16;
if (input <= UPPER_BOUND && input >= LOWER_BOUND)
... logic here ...

How to find the maximum values in an array? [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 9 years ago.
Improve this question
Example
Say that I have an array:
Array = (9, 1, 9, 9);
how can I loop through the array and print the value and position of each maximum value?
so it will print
Value: 9 and Position: 0
Value: 9 and Position: 2
Value: 9 and Position: 3
I am having trouble creating an algorithm for it.
The simplest way I can think of is to go over the array once and find the max value, then go over the array again and print the message with the current index each time you get to an element which is equal to the max you have found.
1) Iterate through all values in array to find MAX value
2) Iterate again to print position if current value == MAX value
Here is a pseudo code for one pass algorithm : -
list maxs;
int maxvalue = arr[0];
maxs = new list();
maxs.append(0);
for(int i=1;i<arr.length;i++) {
if(maxvalue<arr[i]) {
maxvalue = arr[i];
maxs = new list();
maxs.append(i);
}
else if(maxvalue==arr[i]) {
maxs.append(i);
}
}
print(maxvalue,maxs);

how to call more than one element of array [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 using an array,i want to call 4 element from the array at the same time so i can use them in an equation ,any one knows how.
example
int a[10]={1,2,3,4,5,6,7,8};
I want 1234+15
how?
using C#:
1234 = 1*1000 + 2*100 + 3*10 + 4*1
double result = 0;
int ex = 3; // Math.Pow(10,3) = 1000.00
for(int i = 0; i < 4 0; i++)
{
result += a[i] * Math.Pow(10, ex);
ex--;
}
result += 15;

Resources