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.
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 9 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).
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 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 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
Write functions that take a non empty array of doubles and its length as arguments and returns :
a) the sum of the items
b)the index of the maximum value
c) a boolean which indicates if the numbers are in strictly increasing order
Clue 1:
You can return a structure with 3 variables inside, something like this:
struct Resu{
double sum;
int max;
bool order;
};
Clue 2:
Resu homework(double d[],int l){
..
.. your code must be here
..
}
Clue 3:
Resu homework(double d[],int l){
Resu result;
..
.. your magic must be here
..
return result;
}
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 7 years ago.
Improve this question
Let's say i have a pointer-pointer-char array that looks like this:
2-abc
5.5-aaa
10-acdc
3-(the text here doesn't matter)
I need to sort the array in ascending order, acording to the number in each string. I know that the number ends with "-". The numbers can also have decimal points and are in the range of <0;INT_MAX>. Any ideas?
Use qsort with a comparison function that uses strtod to convert the initial portion of the string to a double value. Be careful to return an integer <0, ==0 or >0 depending of whether the converted values are a<b, a==b or a>b.
You need to put some work into this assignment, but it should fit in a single page of code.
Assuming the array is an array of pointers to strings, here is a comparison function you can use:
#include <stdlib.h>
int mycmp(const void *a, const void *b) {
double aa = strtod(*(const char **)a, NULL);
double bb = strtod(*(const char **)b, NULL);
return (bb < aa) - (aa < bb);
}