Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language array problem, please write the code, thank you
C language array problem, please write the code, thank you

The first question: #include?

//?Find the sum of the elements before the first odd number of the integer array?a?, n? is the length of the array< /p>

int?sum_before_first_odd_num(int*?a,?int?n)

{

int?i,?sum?=?0;

for?(i?=?0;?i?

{

if?(a[i]?&?1 ?=?0)?//?odd number

{

break;

}

sum?+=?a[ i];

}

return?sum;

}

int?main(int?argc,?char**? argv)

{

int?i,?n,?a[100];?//?Assume that?n?is maximum?100

printf ?("The size of the array you want to input:");

scanf?("%d",?&n);

for?(i?=?0;? i?

{

scanf?("%d",?&a[i]);

}

int?sum?=?sum_before_first_odd_num(a,?n);

printf?("The sum of the elements before the first odd number is: %d\n",?sum);< /p>

return?0;

}

Run:

Second question: #include?

#include?

//?Ascending comparison function

int?cmp_asc?(const?void*?a,?const?void*?b )

{

int?i1?=?*((int*)a);

int?i2?=?*((int*) b);

return?i1?-?i2;

}

//?Descending comparison function

int?cmp_desc? (const?void*?a,?const?void*?b)

{

int?i1?=?*((int*)a);

int?i2?=?*((int*)b);

return?i2?-?i1;

}

int? main(int?argc,?char**?argv)

{

int?i,?n,?a[100];?//?Assume?n?No More than?100

printf?("The size of the array you want to input:");

scanf?("%d",?&n);

for?(i?=?0;?i?

{

scanf?("%d",?&a[i]);

}

int?half?=?n?/?2;

qsort(a,?half?,?sizeof(a[0]) ,?cmp_asc);?//?The first half of the elements are sorted in ascending order

qsort(a?+?half,?n?-?half,?sizeof(a[0]),?cmp_desc);? //?The second half of the elements are sorted in descending order

printf?("\nThe first half of the sorted array:\n");

for?(i?=?0;? i?

{

printf?("%d?",?a[i]);

}

printf?("\nThe second half of the sorted array:\n");

for?(i?=?half;?i?

{

printf?("%d?",?a[i]);

}

printf?("\ n");

return?0;

}

Run:

Question 3: #include?

#include?

void?count_num_in_str(char*?s,?int*?a)

{

int?i;

for?(i?=?0;?i?

{

a[i]?=?0;

}

char*?c?=?s;

while?(*c ?=?'\0')

{

if?(*c?>=?'0'?&&?*c?<='9')?/ /?The current character is a numeric character

{

a[*c?-?'0']++;

}

c++;

}

}

int?main(int?argc,?char**?argv)

{< /p>

printf?("Please enter a string:\n");

char?s[100];?//?Assume that the longest input string is?100< /p>

gets(s);?//?Input

int?i,?a[10];

count_num_in_str(s,?a);

p>

for?(i?=?0;?i?

{

printf?("The number of %d? is ?%d?piece\n",?i,?a[i]);

}

return?0;

}

Run: