Sort an array by using its first elements [closed] - c

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);
}

Related

Is it possible to have the intersection of two strings without using a loop 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 last year.
Improve this question
I would like to ask you if it was possible to get the characters common to two strings without having to resort to a loop on the character array. I wonder why this could greatly affect the total cost (asymptotically for n-> infinite) of algorithms such as eg. Charm or Eclat (just think that it would be like adding a new cycle to those already present). Thank you.
Specifically, the algorithm I am referring to is the following. As can be seen from the photo (line 6) it is necessary to obtain the intersection by iterating on the indices i and j, so I suppose it is necessary to iterate. I guess I get an O(m + n) best assuming the insert and search operations use O(1).
If your characters are byte-encoded (US-ASCII, KOI8-R, etc), you can create array, where your char is index, and iterate 1st string, and set "1" here. Thereafter, iterate 2nd string, and print only chars, presents in the array. See the example:
void print_intersection(const unsigned char *s1, const unsigned char *s2) {
unsigned char arr[0x100], c;
bzero(arr, sizeof(arr)); // cleanup
while(c = *s1++)
arr[c] = 1;
while(c = *s2++)
if(arr[c] != 0) {
putchar(c);
arr[c] = 0; // Disable print dups
}
}

How can I delete the first zero(s) in a string? (Without using atoi) [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
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}

Write functions that take a non empty array of doubles and its length as arguments and returns : [closed]

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

A function to count how many different characters are in a string [closed]

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've found this function on the internet and I find it to be very useful
but I am new to programming, and could someone please explain briefly what does it exactly do
#include <stdio.h>
int diffcount(char* s)
{
unsigned char seen[127];
int cnt=0,i;
for(i=0;i<127;i++)
seen[i]=0;
for(i=0;s[i];i++)
{
if(!seen[(int)s[i]])
{
cnt++;
seen[(int)s[i]]=1;
}
}return cnt;
}
int main(void) {
char string[20];
scanf("%s",string);
printf("Razlicitih znakova: %d\n", diffcount(string));
return 0;
}
First of all we init an empty array of zeros int seen[127];
"seen" array is used to find out whether char with code i has been met in the array s : if seen[i]==1 than (char)i was in the string s.
After that we make a loop through char* s and check if char s[i] has already been met by looking at the value of seen[s[i]]; and if it is false we put seen[s[i]]=true (because we met it) and increase our counter.
The result of the function is the value of variable cnt
This may also help:
each char has it's code between zero and 127. For example, (int)'a' = 97.
bool in the C is just the same as int, that's why we sometimes use 0 and 1 instead of true and false

put integer into 4 char array 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 9 years ago.
Improve this question
is there any way how to put integer sized 100 000 into 4 elements of char array? If I use sprintf or itoa, array has 6 elements. I tried to use this, but it didnt work. And is there any way how to put these 4 elements back to integer?
char *s;
int value = 100000;
*((int *)s)=value;
Note that:
int value = 100000;
char *s;
*((int *)s)=value;
dereferences uninitialized pointer s, which causes undefined behavior. You could do:
int value = 100000;
char s[4];
*((int *)&s[0])=value;
just note that this stores value in the memory block "occupied" by charr array (at memory level) unlike sprintf, which would print the value in a form of string (characters representing the number).

Resources